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
| #include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 5010;
int n, m;
int h[N], e[N], ne[N], idx;
int d[N];
int color[N];
int q[N], hh, tt = -1;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool toposort() {
for (int i = 1; i <= n; i++) {
if (!d[i]) q[++tt] = i;
}
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (--d[j] == 0) q[++tt] = j;
}
}
return tt == n - 1;
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b), d[b]++;
if (a < b) color[i]= 2;
else color[i] = 1;
}
if (toposort()) {
puts("1");
for (int i = 0; i < m; i++) printf("%d ", 1);
} else {
puts("2");
for (int i = 0; i < m; i++) printf("%d ", color[i]);
}
return 0;
}
|