QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#918649 | #906. 强连通分量 | paul2008# | WA | 0ms | 10068kb | C++20 | 1.0kb | 2025-02-27 21:49:54 | 2025-02-27 21:50:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int head[N], nex[N*2], to[N*2], indx=1;
void add(int x,int y)
{
indx++;
nex[indx]=head[x];
to[indx]=y;
head[x]=indx;
}
int dfsn[N], low[N], stk[N], top, cnt, SCC;
vector <int> vec[N];
bool instack[N];
void tarjan(int x)
{
dfsn[x]=low[x]=++cnt;
instack[x]=true, stk[++top]=x;
for(int i=head[x];i;i=nex[i])
{
int y=to[i];
if(!dfsn[y])
tarjan(y), low[x]=min(low[x],low[y]);
else if(instack[y])
low[x]=min(low[x],dfsn[y]);
}
if(dfsn[x]==low[x])
{
SCC++;
int y=0;
do
{
y=stk[top--];
instack[y]=false;
vec[SCC].push_back(y);
}
while(x!=y);
}
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
add(x,y);
}
for(int i=0;i<n;i++)
if(!dfsn[i])
tarjan(i);
for(int i=SCC;i>=1;i--)
{
printf("%d ",vec[i].size());
for(auto x:vec[i])
printf("%d ",x);
printf("\n");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 10068kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
1 5 2 4 1 1 2 2 3 0
result:
wrong answer twice used vertex 1