QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#342312#906. 强连通分量shuopihua#WA 2ms21788kbC++141.2kb2024-03-01 10:32:022024-03-01 10:32:14

Judging History

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

  • [2024-03-01 10:32:14]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:21788kb
  • [2024-03-01 10:32:02]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int n,m,tm,top,cnt;
int h[500005];
int bl[500005];
int dfn[500005];
int low[500005];
int stk[500005];
bool vis[500005];
vector <int> g[500005];

inline void in(int &n){
	n=0;
	char c=getchar();
	while(c<'0' || c>'9') c=getchar();
	while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
	return ;
}

inline void tarjan(int u){
	dfn[u]=low[u]=++tm;
	vis[u]=1;
	stk[++top]=u;
	for(int v:g[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vis[v]) low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u]){
		cnt++;
		while(stk[top]!=u){
			bl[stk[top]]=cnt;
			vis[stk[top]]=0;
			top--;
		}
		bl[u]=cnt;
		vis[u]=0;
		top--;
	}
	return ;
}

int main(){
	in(n),in(m);
	for(int i=1;i<=m;i++){
		int u,v;
		in(u),in(v);
		g[u].emplace_back(v);
	}
	for(int i=0;i<n;i++) if(!dfn[i]) tarjan(i);
	for(int i=0;i<n;i++) g[i].clear();
	printf("%d\n",cnt);
	for(int i=0;i<n;i++) g[bl[i]].emplace_back(i);
	for(int i=1;i<=cnt;i++){
		printf("%d ",(int)g[i].size());
		for(int v:g[i]) printf("%d ",v);
		puts("");
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 21788kb

input:

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

output:

4
2 0 3 
1 2 
2 1 4 
1 5 

result:

wrong answer 5 is later than 2, but there is an edge (5, 2)