forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.cpp
98 lines (79 loc) · 1.92 KB
/
birthday.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
int total = 2;
void dfs(vector<vector<int>>& adj, vector<int>& up, vector<int>& down, int parent, int start) {
for(auto next : adj[start]) {
// If unseen, set to max
if(up[next] == -1) {
up[next] = total;
down[next] = total;
total++;
dfs(adj, up, down, start, next);
down[start] = min(down[start], down[next]);
}
// If seen, set best loop back
else {
if(next != parent) {
down[start] = min(down[start], down[next]);
}
}
}
}
bool findbridge(vector<vector<int>>& adj) {
int n = adj.size();
vector<int> up(n, -1);
vector<int> down(n);
up[0] = 1;
down[0] = 1;
dfs(adj, up, down, 0, 0);
for(int i = 1; i < n; i++) {
if(up[i] == down[i]) {
return true;
}
}
return false;
}
int find(vector<int>& d, int a) {
if(d[a] == -1) {
return a;
}
d[a] = find(d, d[a]);
return d[a];
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) {
return;
}
d[a] = b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
while(cin >> n && cin >> m && !(n == 0 && m == 0)) {
vector<vector<int>> adj(n);
vector<int> d(n, -1);
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
adj[n1].push_back(n2);
adj[n2].push_back(n1);
join(d, n1, n2);
}
// Check connected
bool connected = true;
for(int i = 1; i < n; i++) {
if(find(d, i) != find(d, 0)) {
connected = false;
}
}
if(!connected || findbridge(adj)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
}