QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#918422 | #906. 强连通分量 | jbd | WA | 4ms | 33120kb | C++14 | 1.5kb | 2025-02-27 20:30:38 | 2025-02-27 20:30:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define fi first
#define se second
#define ps push_back
#define mk make_pair
const int N=1e6+10,inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;
inline ll read(){
char c=getchar();ll x=0;bool f=0;
while(!isdigit(c))f=c=='-'?1:0,c=getchar();
while(isdigit(c))x=(x<<1)+(x<<3)+(c^48),c=getchar();
return f?-x:x;
}
struct jj{
int to,next;
}bi[N<<1];
int n,m,hd[N],cnt,tot,num,dfn[N],low[N],q[N],top,in[N];
inline void add(int x,int y){bi[++cnt]={y,hd[x]},hd[x]=cnt;}
vector<int> v[N];
bool vis[N];
inline void tarjan(int x,int f){
dfn[x]=low[x]=++tot;vis[x]=1;q[++top]=x;
for(int i=hd[x];i;i=bi[i].next){
int j=bi[i].to;
if(!dfn[j]){
tarjan(j,x);low[x]=min(low[x],low[j]);
}
else if(vis[j])low[x]=min(low[x],dfn[j]);
}
if(dfn[x]==low[x]){
int p;++num;
do{
p=q[top--];v[num].ps(p);vis[p]=0;
}while(p!=x);
}
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
n=read(),m=read();
for(int i=1,x,y;i<=m;++i){
x=read()+1,y=read()+1;add(x,y);
++in[y];
}
for(int i=1;i<=n;++i){
if(!in[i])tarjan(i,0);
}
for(int i=1;i<=n;++i)
if(!dfn[i])tarjan(i,0);
cout<<num<<'\n';
for(int i=1;i<=num;++i){
cout<<v[i].size()<<' ';
for(auto j:v[i])
cout<<j-1<<' ';
cout<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 33120kb
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)