标签:ios 点击 背景 输入格式 https sample RKE amp space
割点
给出一个n个点,m条边的无向图,求图的割点。
第一行输入n,m
下面m行每行输入x,y表示x到y有一条边
第一行输出割点个数
第二行按照节点编号从小到大输出节点,用空格隔开
6 7 1 2 1 3 1 4 2 5 3 5 4 5 5 6
1 5
对于全部数据,n≤20000,m≤100000
点的编号均大于0小于等于n。
tarjan图不一定联通。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n,m,ans,x,y,a[1000001],nxt[1000001],head[1000001],dfn[1000001],low[1000001],cnt,k;
bool cut[1000001],bst[1000001];
void add(int x,int y){
a[++k]=y; nxt[k]=head[x]; head[x]=k;
}
void tarjan(int u,int mr){
int rc=0;
dfn[u]=low[u]=++cnt;
for (int p=head[u];p;p=nxt[p]){
int v=a[p];
if (!dfn[v]){
tarjan(v,mr);
low[u]=min(low[u],low[v]);
if (low[v]>=dfn[u]&&u!=mr) cut[u]=true;
if (u==mr) rc++;
}
low[u]=min(low[u],dfn[v]);
}
if (u==mr&&rc>=2) cut[mr]=true;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++){
if(!dfn[i]){tarjan(i,i);}
}
for(int i=1;i<=n;i++){
if(cut[i]){ans++;}
}
printf("%d\n",ans);
for(int i=1;i<=n;i++){
if(cut[i]){
printf("%d ",i);
}
}
}
标签:ios 点击 背景 输入格式 https sample RKE amp space
原文地址:https://www.cnblogs.com/xiongchongwen/p/11825014.html