QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472813 | #906. 强连通分量 | Harry27182# | WA | 4ms | 21444kb | C++14 | 991b | 2024-07-11 19:31:28 | 2024-07-11 19:31:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct edge{int v,nxt;}e[500005];
int n,m,cnt,tot,dfn[500005],low[500005],col[500005],num,h[500005],vis[500005],u,v;
vector<int>g[500005];stack<int>st;
void add(int u,int v){e[++cnt]={v,h[u]};h[u]=cnt;}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;vis[u]=1;st.push(u);
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
num++;
while(!st.empty())
{
int x=st.top();st.pop();vis[x]=0;
col[x]=num;g[num].emplace_back(x);
if(x==u)break;
}
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>u>>v;
add(u,v);
}
for(int i=0;i<n;i++)if(!dfn[i])tarjan(i);
cout<<num<<'\n';
for(int i=1;i<=num;i++)
{
cout<<g[i].size()<<' ';
for(int j=0;j<g[i].size();j++)cout<<g[i][j]<<' ';
cout<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 21444kb
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)