-
Notifications
You must be signed in to change notification settings - Fork 3
/
a.cpp
58 lines (55 loc) · 1.18 KB
/
a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
using Mat = vector<vector<int>>;
Mat adj;
int t;
vector<int> tin, low;
Solution(Mat&& adj) : adj(move(adj)) {}
bool dfs(int i, int p) {
low[i] = tin[i] = t++;
for (int j : adj[i]) {
if (j == p) continue;
if (tin[j]) low[i] = min(low[i], tin[j]);
else {
if (dfs(j, i)) return true;
if (low[j] > tin[i]) return true;
low[i] = min(low[i], low[j]);
}
}
#ifdef DEBUG
printf("low[%d]=%d\n",i,low[i]);
#endif
return false;
}
bool hasBridge() {
t = 1;
tin.assign(adj.size(), 0);
low.assign(adj.size(), INT_MAX);
bool seen = false;
if (dfs(0, -1)) return true;
for (int i = 0; i < adj.size(); ++i) {
if (!tin[i]) return true;
}
return false;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int p, c;
while (cin >> p >> c) {
if (!p || !c) break;
vector<vector<int>> adj(p);
while (c--) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
Solution s(move(adj));
printf("%s\n", s.hasBridge() ? "Yes" : "No");
}
return 0;
}