标签:i++ htm span range cstring html 联通 ora math
割点
给出一个nn个点,mm条边的无向图,求图的割点。
输入格式:
第一行输入n,mn,m
下面mm行每行输入x,yx,y表示xx到yy有一条边
输出格式:
第一行输出割点个数
第二行按照节点编号从小到大输出节点,用空格隔开
对于全部数据,n \le 20000n≤20000,m \le 100000m≤100000
点的编号均大于00小于等于nn。
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);
}
}
}
真的,不喜欢Tarjan。
标签:i++ htm span range cstring html 联通 ora math
原文地址:https://www.cnblogs.com/xiongchongwen/p/11148767.html