-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF_Graph_Without_Long_Directed_Paths.cpp
More file actions
58 lines (50 loc) · 1.17 KB
/
F_Graph_Without_Long_Directed_Paths.cpp
File metadata and controls
58 lines (50 loc) · 1.17 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
#include <bits/stdc++.h>
using namespace std;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n + 1);
vector<int> from(m), to(m);
for(int i=0;i<m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
from[i] = u;
to[i] = v;
}
vector<int> color(n + 1, -1);
function<bool(int)> dfs = [&](int node) {
for(auto &j : adj[node]) {
if(color[j] == -1) {
color[j] = (1 ^ color[node]);
if(!dfs(j)) return false;
}
else if(color[j] == color[node]) {
return false;
}
}
return true;
};
for(int i=1;i<=n;i++) {
if(color[i] == -1) {
color[i] = 0;
if(!dfs(i)) {
cout << "NO\n";
return 0;
}
}
}
cout << "YES\n";
for(int i=0;i<m;i++) {
if(color[from[i]] == 0 && color[to[i]] == 1) {
cout << 1;
}
else cout << 0;
}
cout << "\n";
return 0;
}