-
Notifications
You must be signed in to change notification settings - Fork 5
/
Bipartite.cpp
67 lines (63 loc) · 1.29 KB
/
Bipartite.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int flag;
void dfs(int i,int visited[],int group[], vector <int> graph[],int grp) {
int v;
group[i]=grp;
visited[i]=1;
for(int j=0;j<graph[i].size();j++) {
v=graph[i][j];
if(!visited[v]) {
dfs(v,visited,group,graph,!grp);
}
else {
if(group[v]==0 && grp==0) {
flag=0;
return;
}
else if(group[v]==1 && grp==1) {
flag=0;
return;
}
//else do nothing
}
}
}
void solve() {
flag=1;
int n,e,u,v;
scanf("%d %d",&n,&e);
vector <int> graph[n+1];
int visited[n+1];
int group[n+1];
for(int i=1;i<=n;i++) {
visited[i]=0;
group[i]=-1;
}
for(int i=0;i<e;i++) {
scanf("%d %d",&u,&v);
graph[u].push_back(v);
graph[v].push_back(u);
}
int grp=0;
for(int i=1;i<=n;i++) {
if(flag==0) {
break;
}
if(!visited[i]) {
dfs(i,visited,group,graph,grp);
}
}
if(flag) printf("Yes\n");
else printf("No\n");
}
int main() {
int q;
cin>>q;
while(q--) solve();
return 0;
}