-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1707.cpp
More file actions
113 lines (96 loc) · 2.25 KB
/
1707.cpp
File metadata and controls
113 lines (96 loc) · 2.25 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>
#include <cstring>
#define MAX_SIZE 20000+1
#define RED 1
#define BLUE 2
using namespace std;
int K, V, E;
vector<int> graph[MAX_SIZE];
int visited[MAX_SIZE];
void bfs(int start);
bool isBipartiteGraph();
void bfs(int start) {
queue<int> q;
int color = RED;
visited[start] = color;
q.push(start);
while (!q.empty()) {
int now = q.front();
q.pop();
if (visited[now] == RED) {
color = BLUE;
}
else if (visited[now] == BLUE) {
color = RED;
}
int gsize = graph[now].size();
for (int i = 0; i < gsize; i++) {
int next = graph[now][i];
if (!visited[next]) {
visited[next] = color;
q.push(next);
}
}
}
}
void dfs(int start) {
if (!visited[start]) {
visited[start] = RED;
}
int gsize = graph[start].size();
for (int i = 0; i < gsize; i++) {
int next = graph[start][i];
if (!visited[next]) {
if (visited[start] == RED) {
visited[next] = BLUE;
}
else if (visited[start] == BLUE) {
visited[next] = RED;
}
dfs(next);
}
}
}
bool isBipartiteGraph() {
for (int i = 1; i <= V; i++) {
int gsize = graph[i].size();
for (int j = 0; j < gsize; j++) {
int next = graph[i][j];
if (visited[i] == visited[next]) {
return 0;
}
}
}
return 1;
}
int main() {
scanf("%d", &K);
while (K--) {
scanf("%d %d", &V, &E);
for (int i = 0; i < E; i++) {
int f, s;
scanf("%d %d", &f, &s);
graph[f].push_back(s);
graph[s].push_back(f);
}
for (int i = 1; i <= V; i++) {
if (!visited[i]) {
bfs(i);
}
}
if (isBipartiteGraph()) {
printf("YES\n");
}
else {
printf("NO\n");
}
for (int i = 0; i <= V; i++) {
graph[i].clear();
}
memset(visited, false, sizeof(visited));
}
return 0;
}