QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640826 | #906. 强连通分量 | Zhou_JK | WA | 3ms | 33376kb | C++23 | 1.7kb | 2024-10-14 16:19:11 | 2024-10-14 16:19:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct Trajan_SCC
{
static constexpr int N=500005;
int n;
Trajan_SCC(int _n):n(_n){}
vector<int>G[N];
int dfn[N],low[N],Index;
bool ins[N];
stack<int>s;
int bel[N],tot;
vector<int>block[N];
void dfs(int u)
{
dfn[u]=low[u]=++Index;
ins[u]=true;
s.push(u);
for(int v:G[u])
{
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
tot++;
block[tot].clear();
while(!s.empty()&&s.top()!=u)
{
int v=s.top();
s.pop();
ins[v]=false;
bel[v]=tot;
block[tot].push_back(v);
}
s.pop();
ins[u]=false;
bel[u]=tot;
block[tot].push_back(u);
}
return;
}
void add_edge(int u,int v)
{
G[u].emplace_back(v);
return;
}
void tarjan()
{
for(int i=1;i<=n;i++)
if(!dfn[i]) dfs(i);
return;
}
};
int main()
{
int n,m;
cin>>n>>m;
Trajan_SCC scc(n);
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
u++,v++;
scc.add_edge(u,v);
}
scc.tarjan();
int tot=scc.tot;
cout<<tot<<"\n";
for(int i=1;i<=tot;i++)
{
int l=scc.block[i].size();
cout<<l;
for(int u:scc.block[i])
cout<<" "<<u-1;
cout<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 33376kb
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)