aboutsummaryrefslogtreecommitdiff
path: root/19.5/open/gold/fcolor.cpp
blob: 5a3879db0ed223cb08b885c6b3adc38b0253912a (plain)
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
#include <bits/stdc++.h>
using namespace std;

int cnt = 0, c[200002], p[200002], ans[200002];
bitset<200002> vis;
vector<int> G[200002], H[200002];

void dfs(int u) {
	vis[u] = 1;
	for (auto& v : G[u]) {
		if (!vis[v]) dfs(v);
		if (c[v]) c[u] = max(p[c[v]], c[u]);
	}
	for (auto& v : H[u]) {
		if (!vis[v]) dfs(v);
		if (c[u]) c[v] = max(p[c[u]], c[v]);
	}
	if (!c[u]) c[u] = ++cnt;
	for (auto& v : G[u]) if (c[v]) p[c[v]] = max(c[u], p[c[v]]);
	for (auto& v : H[u]) if (c[v]) p[c[u]] = max(c[v], p[c[u]]);
}

int main() {
	ifstream cin("fcolor.in");
	ofstream cout("fcolor.out");

	int N, M; cin >> N >> M;
	while (M--) {
		int a, b; cin >> a >> b;
		G[b].push_back(a), H[a].push_back(b);
	}
	for (int u = 1; u <= N; ++u) dfs(u);
	vis.reset();
	for (int u = 1; u <= N; ++u) dfs(u);
	cnt = 0;
	for (int u = 1; u <= N; ++u) if (!ans[c[u]]) ans[c[u]] = ++cnt;
	for (int u = 1; u <= N; ++u) cout << ans[c[u]] << '\n';
}