-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path886. Possible Bipartition.cpp
74 lines (48 loc) · 1.11 KB
/
886. Possible Bipartition.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
class Solution {
public:
bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
vector <int> graph[2000 + 10];
int color[2000+10];
bool visit[2000 + 10];
//memset(visit, 0, sizeof visit);
for (auto it : dislikes) {
vector <int> cur = it;
int x, y;
x = cur[0];
y = cur[1];
graph[x].push_back(y);
graph[y].push_back(x);
}
for (int i = 1; i <= N; i++)color[i] = 0;
//bool ans = true;
for (int tt = 1; tt <= N; tt++) {
if (color[tt] == 0) {
stack <int> stk;
color[tt] = 10;
stk.push(tt);
while (!stk.empty()) {
int x = stk.top();
stk.pop();
//cout << x << " : " << color[x] << endl;
for (int i = 0; i < graph[x].size(); i++) {
int child = graph[x][i];
if (color[child] == 0) {
if (color[x] == 10) {
color[child] = 20;
}
else {
color[child] = 10;
}
stk.push(child);
}
else if (color[x] == color[child]) {
return false;
//cout << "child ar parent same pawa geche \n";
}
}
}
}
}
return true;
}
};