QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123226 | #906. 强连通分量 | DrGilbert# | WA | 1ms | 9532kb | C++20 | 936b | 2023-07-11 21:25:16 | 2023-07-11 21:25:18 |
Judging History
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=1;i<=num;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: 0
Wrong Answer
time: 1ms
memory: 9532kb
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)