-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmid62ttt.cpp
More file actions
72 lines (60 loc) · 1.36 KB
/
mid62ttt.cpp
File metadata and controls
72 lines (60 loc) · 1.36 KB
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
#include <iostream>
#include <vector>
#include <list>
using namespace std;
vector<int> seen, color;
bool isBipartite(int s, vector<int> *adj, pair<int,int> p){
list<int> Q;
Q.push_back(s);
seen[s] = true;
color[s] = 0;
while(!Q.empty()) {
int u = Q.front();
Q.pop_front();
for(int i : adj[u]) {
//cout << "u: " << u + 1 << ", i: " << i + 1 << "\n";
if((u == p.first && i == p.second) || (i == p.first && u == p.second)) continue;
if(!seen[i]) {
Q.push_back(i);
seen[i] = true;
color[i] = !color[u];
}
else if(color[i] == color[u]) return false;
}
}
return true;
}
int main(){
int n, m;
cin >> n >> m;
vector<int> adj[n];
list<pair<int,int>> p;
seen.resize(n,0);
color.resize(n,-1);
int u, v;
for(int i = 0; i < m; i++) {
cin >> u >> v;
u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
p.push_back({u, v});
}
//do
for(pair<int,int> pa : p){
//cout << "If have no " << pa.first + 1 << " " << pa.second + 1 << " pair\n";
bool bipartite = true;
for(int i = 0; i < n; i++){
if(!seen[i]) bipartite &= isBipartite(i, adj, pa);
}
if(bipartite){
cout << pa.first + 1 << " " << pa.second + 1;
break;
}
//reset
for(int i = 0; i < n; i++){
color[i] = -1;
seen[i] = 0;
}
}
return 0;
}