码迷,mamicode.com
首页 > 其他好文 > 详细

P3388 【模板】割点(割顶)

时间:2019-11-09 13:32:05      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:ios   点击   背景   输入格式   https   sample   RKE   amp   space   

如有乱码,请点击

 

题目背景

割点

题目描述

给出一个n个点,m条边的无向图,求图的割点。

输入格式

第一行输入n,m

下面m行每行输入x,y表示xy有一条边

输出格式

第一行输出割点个数

第二行按照节点编号从小到大输出节点,用空格隔开

输入输出样例

输入 #1
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6
输出 #1
1 
5

说明/提示

对于全部数据,n20000,m100000

点的编号均大于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);
		}
	} 
}

  

 

P3388 【模板】割点(割顶)

标签:ios   点击   背景   输入格式   https   sample   RKE   amp   space   

原文地址:https://www.cnblogs.com/xiongchongwen/p/11825014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!