QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#123229#906. 强连通分量DrGilbert#TL 2ms9580kbC++20936b2023-07-11 21:26:312023-07-11 21:26:38

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-11 21:26:38]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9580kb
  • [2023-07-11 21:26:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int dfn[N],low[N],stk[N],ins[N];
int col[N],top,tot,num;
vector<vector<int>> G,scc;
void tarjan(int x){
	stk[++top]=x;ins[x]=1;
	dfn[x]=low[x]=++tot;
	for (int v:G[x]){
		if (!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]);
		else if (ins[v]) low[x]=min(low[x],dfn[v]);
	}if (dfn[x]==low[x]){
		int k;num++;
		do{
			k=stk[top--];col[k]=num;ins[k]=0;
		}while (k!=x);
	}return;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr);cout.tie(nullptr);
	int n,m;cin>>n>>m;G.resize(n+10);
	for (int i=1;i<=m;i++){
		int u,v;cin>>u>>v;u++;v++;
		G[u].emplace_back(v);
	}for (int i=1;i<=n;i++){
		if (!dfn[i]) tarjan(i);
	}scc.resize(num+10);
	for (int i=1;i<=n;i++){
		scc[col[i]].emplace_back(i);
	}cout<<num<<endl;
	for (int i=num;i>=1;i--){
		cout<<scc[i].size()<<' ';
		for (int x:scc[i]) cout<<x-1<<' ';
		cout<<endl;
	}return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9580kb

input:

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

output:

4
1 5 
2 1 4 
1 2 
2 0 3 

result:

ok OK

Test #2:

score: -100
Time Limit Exceeded

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:

499590
1 499999 
1 499995 
1 499994 
1 499989 
1 499988 
1 499987 
1 499983 
1 499978 
1 499975 
1 499972 
1 499969 
1 499965 
1 499964 
1 499954 
1 499949 
1 499947 
1 499942 
1 499939 
1 499932 
1 499928 
1 499926 
1 499925 
1 499924 
1 499919 
1 499918 
1 499915 
1 499910 
1 499909 
1 499907 
1 4...

result: