QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#482101#906. 强连通分量ShiRoZeTsuWA 0ms26120kbC++141.5kb2024-07-17 17:15:552024-07-17 17:15:55

Judging History

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

  • [2024-07-17 17:15:55]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:26120kb
  • [2024-07-17 17:15:55]
  • 提交

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);
		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;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 26120kb

input:

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

output:

4
2 4 1
2 5 2
1 6
1 3

result:

wrong answer Integer 6 violates the range [0, 5]