样例不过求调
查看原帖
样例不过求调
793625
Pink_Cut_Tree楼主2024/8/6 20:31
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,u,v;
vector<int>G[N],ans[N];
int dfn[N],cnt,low[N],tot;
int d[N];
stack<int>s;
void tarjan(int u,int fa){
	dfn[u]=low[u]=++cnt;
	s.push(u);
	if(!d[u]){
		ans[++tot].push_back(u); return;
	}
	for(int v:G[u]){
		if(dfn[v]){
			low[u]=min(low[u],dfn[v]);
		}
		else if(v!=fa){
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u]){
				tot++;
				int x;
				do{
					x=s.top();
					ans[tot].push_back(x);
					s.pop();
				}
				while(x!=v);
				ans[tot].push_back(u);
			}
		}
	}
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	while(m--){
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
		d[u]++,d[v]++;
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i]){
			tarjan(i,i);
		}
	}
	cout<<tot<<"\n";
	for(int i=1;i<=tot;i++){
		if(G[i].size()){
			cout<<G[i].size()<<" ";
			for(int j=0;j<G[i].size();j++){
				cout<<G[i][j]<<" ";
			}
			cout<<"\n";
		}
	}
return 0;
}

感觉跟题解基本快一样了但就是错

2024/8/6 20:31
加载中...