QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#92683#906. 强连通分量wzh2021#RE 4ms20428kbC++141.5kb2023-03-30 20:52:082023-03-30 20:52:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-30 20:52:11]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:20428kb
  • [2023-03-30 20:52:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pr = pair <int, int>;
#define _1 first
#define _2 second
const int N = 200010;
bool vis[N];
int deg[N], que[N];
int n, m, tot, top, cnt;
int col[N], dfn[N], low[N], stk[N];
vector <int> vec[N], scc[N], vec2[N];
void tarjan(int x) {
	dfn[x] = low[x] = ++tot;
	stk[++top] = x;
	vis[x] = 1;
	for (int y : vec[x])
		if (!dfn[y]) {
			tarjan(y);
			low[x] = min(low[x], low[y]);
		} else if (vis[y])
			low[x] = min(low[x], dfn[y]);
	if (dfn[x] == low[x]) {
		++cnt;
		for (; stk[top] ^ x; --top) {
			col[stk[top]] = cnt;
			vis[stk[top]] = 0;
			scc[cnt].emplace_back(stk[top]);
		}
		col[x] = cnt;
		vis[x] = 0;
		scc[cnt].emplace_back(x);
		--top;
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		++u;
		++v;
		vec[u].emplace_back(v);
	}
	for (int i = 1; i <= n; ++i)
		if (!dfn[i])
			tarjan(i);
	printf("%d\n", cnt);
	for (int i = 1; i <= n; ++i)
		for (int j : vec[i])
			if (col[i] ^ col[j]) {
				vec2[col[i]].emplace_back(col[j]);
				++deg[col[j]];
			}
	tot = 0;
	for (int i = 1; i <= cnt; ++i)
		if (!deg[i])
			que[++tot] = i;
	for (int i = 1; i <= cnt; ++i) {
		int x = que[i];
		for (int y : vec2[x])
			if (!--deg[y])
				que[++tot] = y;
	}
	for (int i = 1; i <= cnt; ++i) {
		printf("%llu ", scc[que[i]].size());
		for (int j : scc[que[i]])
			printf("%d ", j - 1);
		puts("");
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 20428kb

input:

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

output:

4
2 3 0 
2 4 1 
1 5 
1 2 

result:

ok OK

Test #2:

score: -100
Runtime Error

input:

500000 500000
389812 410922
255712 339244
274009 248522
347288 199231
235313 301629
469588 84917
364188 449407
392348 436920
26220 198288
162188 468965
274222 92196
463222 408478
231663 467768
382681 38083
412497 160479
280851 268689
101149 25450
62271 9177
38892 268598
273853 250782
191939 89247
40...

output:


result: