QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#482103#906. 强连通分量ShiRoZeTsuWA 230ms50656kbC++141.5kb2024-07-17 17:16:262024-07-17 17:16:27

Judging History

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

  • [2024-07-17 17:16:27]
  • 评测
  • 测评结果:WA
  • 用时:230ms
  • 内存:50656kb
  • [2024-07-17 17:16:26]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const int maxn = 5e5 + 5;
const int maxm = 5e5 + 5;

struct edge { int to, nxt; } e[maxm<<1];
int tot = 1, head[maxn];
inline void add(int u, int v) {
	e[++tot].to = v;
	e[tot].nxt = head[u];
	head[u] = tot;
}

struct Edge { int u, v; } E[maxm];

int n, m, cnt, top, dfncnt;
int low[maxn], dfn[maxn], stk[maxn], scc[maxn], in[maxn];
bool vis[maxn];
vector<int> s[maxn];

void getscc(int u) {
	low[u] = dfn[u] = ++dfncnt;
	stk[++top] = u;
	vis[u] = true;
	for(int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if(!dfn[v]) { getscc(v); low[u] = min(low[u], low[v]); }
		else if(vis[v]) low[u] = min(low[u], low[v]);
	}
	vis[u] = false;
	if(dfn[u] == low[u]) {
		int x; cnt++;
		while((x = stk[top--])) {
			s[cnt].push_back(x);
			scc[x] = cnt;
			if(x == u) break;
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	for(int i = 1, u, v; i <= m; i++) {
		scanf("%d %d", &u, &v);
		add(u+1, v+1);
		E[i] = { u+1, v+1 };
	}
	for(int i = 1; i <= n; i++)
		if(!dfn[i]) getscc(i);

	queue<int> q;
    for(int i = 1; i <= m; i++) if(scc[E[i].u] != scc[E[i].v]) in[scc[E[i].v]]++;
	for(int i = 1; i <= cnt; i++) if(!in[i]) q.push(i);

	printf("%d\n", cnt);
	while(!q.empty()) {
		int u = q.front(); q.pop();
		printf("%u", s[u].size());
		for(int x : s[u]) printf(" %d", x-1);
		putchar('\n');
		for(int x : s[u]) for(int i = head[x]; i; i = e[i].nxt) {
			int v = scc[e[i].to];
			if(u == v) continue;
			in[v]--;
			if(!in[v]) q.push(v);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 230ms
memory: 50656kb

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:

499600
1 3
1 4
1 5
1 6
1 7
1 10
1 13
1 14
1 17
1 19
1 20
1 21
1 24
1 25
1 26
1 27
1 31
1 32
1 34
1 37
1 39
1 42
1 43
1 46
1 47
1 59
1 60
1 67
1 68
1 76
1 80
1 85
1 86
1 88
1 89
1 94
1 107
1 111
1 113
1 114
1 119
1 120
1 121
1 123
1 129
1 130
1 132
1 133
1 135
1 139
1 143
1 145
1 146
1 147
1 148
1 14...

result:

wrong output format Unexpected end of file - int32 expected