forked from varnitsingh/OJ-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph Without Long Directed Paths.cpp
101 lines (83 loc) · 1.87 KB
/
Graph Without Long Directed Paths.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
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
// Problem: F. Graph Without Long Directed Paths
// Contest: Codeforces - Codeforces Round #550 (Div. 3)
// URL: https://codeforces.com/contest/1144/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define ll long long int
#define vi vector<int>
#define vii vector<int, int>
#define vc vector<char>
#define vl vector<ll>
#define mod 1000000007
#define INF 1000000009
using namespace std;
vi adj[200005];
bool color[200005];
bool vis[200005];
bool dfs(int node, bool col) {
vis[node] = true;
for(int child : adj[node]) {
if(!vis[child]) {
color[child] = col^1;
bool res = dfs(child, col^1);
if(res == false) return false;
}
else {
if(color[child] == color[node]) {
return false;
}
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
// cin >> t;
while(t--) {
int n, m;
cin >> n >> m;
int a, b;
vector<pair<int, int> > A;
for(int i = 1; i <= m; i++) {
cin >> a >> b;
A.push_back({a, b});
adj[a].PB(b);
adj[b].PB(a);
}
bool flag = true;
for(int i = 0; i <= n; i++) {
bool res = dfs(i, 0);
if(res == false) {
flag = false;
break;
}
}
if(flag) {
cout << "YES\n";
for(int i = 0; i < (int)A.size(); i++) {
if(color[A[i].first] == 0) {
cout << "1";
}
else {
cout << "0";
}
}
cout << "\n";
}
else {
cout << "NO\n";
}
}
return 0;
}