【模板】割点(割顶) Tarjan

题目背景

割点

题目描述

给出一个nnn个点,mmm条边的无向图,求图的割点。

输入输出格式

输入格式:

第一行输入n,mn,mn,m

下面mmm行每行输入x,yx,yx,y表示xxx到yyy有一条边

输出格式:

第一行输出割点个数

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

输入输出样例

输入样例#1: 复制
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6
输出样例#1: 复制
1 
5

说明

对于全部数据,n≤20000n \le 20000n20000,m≤100000m \le 100000m100000

点的编号均大于000小于等于nnn。

tarjan图不一定联通。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

struct node {
	int to,nxt;
}edge[maxn<<1];
int n, m;
int idx, cnt, tot;
int head[maxn], dnf[maxn], low[maxn];
bool cut[maxn];

void addedge(int u, int v) {
	edge[++cnt].to = v;
	edge[cnt].nxt= head[u]; head[u] = cnt;
}
void tarjan(int u, int fa) {
	dnf[u] = low[u] = ++idx;
	int ch = 0;
	for (int i = head[u]; i; i = edge[i].nxt) {
		int to = edge[i].to;
		if (!dnf[to]) {
			tarjan(to, fa);
			low[u] = min(low[u], low[to]);
			if (low[to] >= dnf[u] && u != fa) {
				cut[u] = true;
			}
			if (u == fa)ch++;
		}
		low[u] = min(low[u], dnf[to]);
	}
	if (ch >= 2 && u == fa)cut[u] = true;
}

int main()
{
	//ios::sync_with_stdio(0);
	rdint(n); rdint(m);
	for (int i = 0; i < m; i++) {
		int a, b;
		rdint(a); rdint(b);
		addedge(a, b); addedge(b, a);

	}
	for (int i = 1; i <= n; i++) {
		if (dnf[i] == 0)tarjan(i, i);
	}
	for (int i = 1; i <= n; i++) {
		if (cut[i])tot++;
	}
	cout << tot << endl;
	for (int i = 1; i <= n; i++) {
		if (cut[i])cout << i << ' ';
	}
	return 0;
}

 

 

posted @ 2018-12-21 10:20  NKDEWSM  阅读(258)  评论(0编辑  收藏  举报