QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#481526#906. 强连通分量wanglianda#TL 0ms48980kbC++141.6kb2024-07-17 08:14:422024-07-17 08:14:42

Judging History

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

  • [2024-07-17 08:14:42]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:48980kb
  • [2024-07-17 08:14:42]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=5e5;
ll read(){
	ll S=0,F=1;char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')F*=-1;
		ch=getchar();
	}while(isdigit(ch)){
		S=(S<<3)+(S<<1)+(ch^48);
		ch=getchar();
	}return S*F;
}ll n,m,u,v,lts,tp,sta[N+10],vis[N+10],dfn[N+10],low[N+10],cnt,in[N+10];
ll buc[N+10],ind[N+10];queue<ll >que;
vector<ll >vs[N+10],to[N+10],tl[N+10];
void dfs(ll id){
	low[id]=dfn[id]=++cnt;
	sta[++tp]=id;vis[id]=1;
	for(ll i=0;i<to[id].size();i++){
		if(!dfn[to[id][i]]){
			dfs(to[id][i]);
			low[id]=min(low[id],low[to[id][i]]);
		}else if(vis[to[id][i]])low[id]=min(low[id],dfn[to[id][i]]);
	}if(low[id]==dfn[id]){
		lts++;do{
			vs[lts].push_back(sta[tp--]);
			vis[sta[tp+1]]=0;in[sta[tp+1]]=lts;
		}while(sta[tp+1]!=id);
	}return;
}int main(){
	n=read();m=read();
	while(m--){
		u=read()+1;v=read()+1;
//		swap(u,v);
		to[u].push_back(v);
	}for(ll i=1;i<=n;i++)if(!dfn[i])dfs(i);
	printf("%lld\n",lts);
	for(ll i=1;i<=lts;i++){
		for(ll j=1;j<=lts;j++)buc[j]=0;
		for(ll J=0;J<vs[i].size();J++){ll j=vs[i][J];
			for(ll k=0;k<to[j].size();k++)if(!buc[in[to[j][k]]]&&in[to[j][k]]!=i){
				buc[in[to[j][k]]]++;
				tl[i].push_back(in[to[j][k]]);
				ind[in[to[j][k]]]++;
			}
		}
	}for(ll i=1;i<=lts;i++)if(!ind[i])que.push(i);
	while(!que.empty()){
		ll x=que.front();que.pop();		
		printf("%lld ",vs[x].size());
		for(ll j=0;j<vs[x].size();j++)printf("%lld ",vs[x][j]-1);
		putchar('\n');for(ll j=0;j<tl[x].size();j++){
			ind[tl[x][j]]--;
			if(!ind[tl[x][j]])que.push(tl[x][j]);
		}
	}return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
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:


result: