__frj @ 2022-07-15 19:33:25
RT,这份代码将
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define ull unsigned long long
#define gc getchar
using namespace std;
int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
const int N = 3000007;
int cnt[N],root,n,m,tot,head[N],nxt[N],to[N],dfn[N],low[N],vis[N];
void add(int x,int y){
to[++tot] = y , nxt[tot] = head[x] , head[x] = tot;
}
int cur = 0;
void dfs(int x,int in_edge){
dfn[x] = low[x] = ++cur;
for(int i=head[x];i;i=nxt[i]){
int y = to[i];
//if(i == (in_edge ^ 1)) continue;
//if(dfn[y]) low[x] = min(low[x],dfn[y]); //
if(!dfn[y]){
dfs(y,i);
low[x] = min(low[x],low[y]);
if(dfn[x] < low[y]){
vis[i] = vis[i^1] = 1;
}
}
else if(i != (in_edge ^ 1)){
low[x] = min(low[x],dfn[y]);
}
}
}
int count_dcc = 0;
vector<int> a[500007];
void sfd(int x){
cnt[x] = 1;
a[count_dcc].push_back(x);
for(int i=head[x];i;i=nxt[i]){
int y = to[i];
if(cnt[y] || vis[i]) continue;
sfd(y);
}
}
int main(){
int n = read() , m =read();
tot = 1;
for(int i=1;i<=m;i++){
int u = read() , v = read();
add(u,v),add(v,u);
}
for(int i=1;i<=n;i++){
if(!dfn[i]) dfs(i,0);
}
for(int i=1;i<=n;i++){
if(!cnt[i]){
count_dcc++;
sfd(i);
}
}
cout << count_dcc<<endl;
for(int i=1;i<=count_dcc;i++){
cout<<a[i].size()<<" ";
for(int j=0;j<a[i].size();j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by Powerless233 @ 2022-07-15 19:59:49
仔细阅读数据范围,并考虑双向边
by 郑朝曦zzx @ 2022-07-15 20:01:30
@__frj
边数不是
by __frj @ 2022-07-16 08:16:34
@Powerless233 @郑朝曦zzx 感谢