QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#472813#906. 强连通分量Harry27182#WA 4ms21444kbC++14991b2024-07-11 19:31:282024-07-11 19:31:28

Judging History

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

  • [2024-07-11 19:31:28]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:21444kb
  • [2024-07-11 19:31:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
struct edge{int v,nxt;}e[500005];
int n,m,cnt,tot,dfn[500005],low[500005],col[500005],num,h[500005],vis[500005],u,v;
vector<int>g[500005];stack<int>st;
void add(int u,int v){e[++cnt]={v,h[u]};h[u]=cnt;}
void tarjan(int u)
{
	dfn[u]=low[u]=++tot;vis[u]=1;st.push(u);
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].v;
		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])
	{
		num++;
		while(!st.empty())
		{
			int x=st.top();st.pop();vis[x]=0;
			col[x]=num;g[num].emplace_back(x);
			if(x==u)break;
		}
	}
}
int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>u>>v;
		add(u,v);
	}
	for(int i=0;i<n;i++)if(!dfn[i])tarjan(i);
	cout<<num<<'\n';
	for(int i=1;i<=num;i++)
	{
		cout<<g[i].size()<<' ';
		for(int j=0;j<g[i].size();j++)cout<<g[i][j]<<' ';
		cout<<'\n'; 
	}
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 21444kb

input:

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

output:

4
2 3 0 
1 2 
2 4 1 
1 5 

result:

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