QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#103034#6329. Colorful Graphw4p3rWA 1ms13316kbC++202.9kb2023-05-04 01:55:412023-05-04 01:55:42

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-04 01:55:42]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:13316kb
  • [2023-05-04 01:55:41]
  • 提交

answer

#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e - 6
#define FOR(i, a, b) for(int i = a;i <= b;i ++)
#define REP(i, a, b) for(int i = a;i >= b;i --)
#define pa pair<int, int>
#define fr first
#define sd second
#define pb push_back
#define db double
#define MEM(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch = getchar();
	ll s = 0, w = 1;
	while (ch < '0' || ch > '9') {if (ch == '-')w = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {s = s * 10 + ch - '0'; ch = getchar();}
	return s * w;
}
int n, m;
#define N 200010
#define M 200010
struct edge
{
	int nxt, to, fl;
} e[M << 1];
int h[N], cur[N], cnt = 1;
int dep[N];
int S, T;
int fa[N], to[N];
inline void add(int x, int y, int fl)
{
	e[++cnt] = (edge) {h[x], y, fl}; h[x] = cnt;
	e[++cnt] = (edge) {h[y], x, +0}; h[y] = cnt;
}
int bfs()
{
	queue<int>Q; FOR(i, 1, T)dep[i] = 0, cur[i] = h[i];
	dep[S] = 1; Q.push(S);
	while (!Q.empty())
	{
		int x = Q.front(); Q.pop();
		for (int i = h[x]; i; i = e[i].nxt)if (e[i].fl && !dep[e[i].to])
			{
				int y = e[i].to; Q.push(y); dep[y] = dep[x] + 1;
				if (y == T)return 1;
			}
	}
	return 0;
}
int dfs(int x, int flow)
{
	if (x == T)return flow;
	int res = 0;
	for (int &i = cur[x]; i; i = e[i].nxt)if (e[i].fl && dep[e[i].to] == dep[x] + 1)
		{
			int y = e[i].to, fl = dfs(y, min(flow - res, e[i].fl));
			e[i].fl -= fl;
			e[i ^ 1].fl += fl;
			res += fl;
			if (res == flow)return res;
		}
	if (!res) dep[x] = 0;
	return res;
}
void Dinic()
{
	while (bfs()) {dfs(S, inf);}
}
int find(int x) {return (x == fa[x] ? x : fa[x] = find(fa[x]));}
int dfn[N], low[N], st[N], inst[N], top;
int Ti, num, bel[N];
vector<int>g[N];
void dfs(int x)
{
	dfn[x] = low[x] = ++Ti;
	st[++top] = x; inst[x] = 1;
	for (int y : g[x])
	{
		if (dfn[y]) {if (inst[y])low[x] = min(low[x], low[y]);}
		else {dfs(y); low[x] = min(low[x], low[y]);}
	}
	if (dfn[x] == low[x])
	{
		++num;
		while (st[top] != x)
		{
			bel[st[top]] = num;
			inst[st[top--]] = 0;
		}
		bel[st[top]] = num;
		inst[st[top--]] = 0;
	}
}
int main()
{
	n = read(), m = read();
	FOR(i, 1, m)
	{
		int x = read(), y = read();
		g[x].pb(y);
	}
	FOR(i, 1, n)if (!dfn[i])dfs(i);
	// cerr << num << endl;
	FOR(x, 1, n)for (int y : g[x])if (bel[x] != bel[y])
		{
			add(bel[x], bel[y] + num, 1);
		}
	S = 2 * num + 1, T = S + 1;

	FOR(i, 1, num)
	{
		add(S, i, 1);
		add(i + num, T, 1);
	}
	Dinic();
	
	FOR(x, 1, num)
	{
		for (int i = h[x]; i; i = e[i].nxt)
		{
			int y = e[i].to;
			if (num < y && y <= 2 * num && (!e[i].fl))
			{
				to[x] = y - num;
			}
		}
	}
	FOR(i, 1, num)fa[i] = i;
	FOR(i, 1, num)if (to[i])fa[find(i)] = find(to[i]);
	vector<int>c(n + 1, 0);
	int tot = 0;
	FOR(i, 1, n)
	{
		int x = find(bel[i]);
		if (!c[x])c[x] = ++tot;
		cout << c[x] << ' ';
	}
	cout << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13316kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 1 2 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 0ms
memory: 10284kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

1 1 2 1 1 

result:

ok AC

Test #3:

score: 0
Accepted
time: 1ms
memory: 10436kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

1 1 1 1 2 1 3 4 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 11768kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

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 58 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1...

result:

wrong answer Integer 1751 violates the range [1, 1750]