-
Notifications
You must be signed in to change notification settings - Fork 0
/
1341.cpp
80 lines (75 loc) · 1.48 KB
/
1341.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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<vector<int>> g;
vector<int> DFN;
vector<int> LOW;
stack<int> st;
vector<bool> inStack;
vector<bool> vis;
vector<int> sccid;
int timestamp = 0;
int scccnt = 0;
void tarjan(int u) {
DFN[u] = LOW[u] = ++timestamp;
vis[u] = true;
st.push(u);
inStack[u] = true;
for (auto i = g[u].begin(); i < g[u].end(); ++i) {
if (!vis[*i]) {
tarjan(*i);
LOW[u] = min(LOW[u], LOW[*i]);
}
else if (inStack[*i]) {
LOW[u] = min(LOW[u], LOW[*i]);
}
}
int v;
if (DFN[u] == LOW[u]) {
scccnt++;
do {
v = st.top();
st.pop();
inStack[v] = false;
sccid[v] = scccnt;
//cout << v << '.';
} while (u != v);
//cout << '\n';
}
}
int main() {
int m, n, i, j, a, b;
cin >> n >> m;
g.resize(n * 2 + 1);
inStack.resize(n * 2 + 1);
DFN.resize(n * 2 + 1);
LOW.resize(n * 2 + 1);
sccid.resize(n * 2 + 1);
vis.resize(n * 2 + 1);
// 2i-1: i=1 2i: i=0
for (int tmp = 0; tmp < m; ++tmp) {
cin >> i >> a >> j >> b;
g[i * 2 - 1 + a].push_back(j * 2 - b);
g[j * 2 - 1 + b].push_back(i * 2 - a);
}
for (int i = 1; i <= n * 2; ++i) {
if (!vis[i]) tarjan(i);
}
for (int i = 1; i <= n; ++i) {
if (sccid[i * 2] == sccid[i * 2 - 1]) {
cout << "No";
return 0;
}
}
cout << "Yes\n";
for (int i = 1; i <= n; ++i) {
if (sccid[i * 2] < sccid[i * 2 - 1]) {
cout << "0 ";
}
else {
cout << "1 ";
}
}
return 0;
}