QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#484483#906. 强连通分量rzh123WA 1ms5652kbC++20972b2024-07-19 19:35:132024-07-19 19:35:17

Judging History

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

  • [2024-07-19 19:35:17]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5652kb
  • [2024-07-19 19:35:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N=5e5+5;
int n,m,ec,eh[N],dft,dfn[N],low[N];
struct{int nxt,to;}e[N<<1];
vector<vector<int> > scc;
inline void adde(int u,int v){
	e[++ec]={eh[u],v},eh[u]=ec;
}
void tarj(int u){
	static stack<int> stk;
	static bool ins[N];
	dfn[u]=low[u]=++dft,stk.emplace(u),ins[u]=true;
	for(int i{eh[u]};i;i=e[i].nxt){
		int v{e[i].to};
		if(!dfn[v]) tarj(v),low[u]=min(low[u],low[v]);
		else if(ins[v]) low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u]){
		scc.emplace_back();
		while(1){
			int t{stk.top()}; stk.pop(),ins[t]=false;
			scc.back().emplace_back(t);
			if(t==u) break;
		}
	}
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	for(int i{1};i<=m;++i){
		int u,v; cin>>u>>v;
		adde(u,v);
	}
	for(int i{0};i<n;++i) if(!dfn[i]) tarj(i);
	cout<<scc.size()<<'\n';
	for(const auto &a:scc){
		cout<<a.size();
		for(int i:a) cout<<' '<<i;
		cout<<'\n';
	}
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5652kb

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)