【luogu P3388 割点(割顶)】 模板

题目链接:https://www.luogu.org/problemnew/show/P3388

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 500000 + 10;
ll n, m, ans;
ll dfn[maxn], low[maxn], tim;
bool iscut[maxn];
struct edge{
	ll from, to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
	e[++cnt].from = u;
	e[cnt].next = head[u];
	e[cnt].to = v;
	head[u] = cnt;
}
void tarjan(ll x, ll fa)
{
	ll child = 0;
	dfn[x] = low[x] = ++tim;
	for(ll i = head[x]; i != -1; i = e[i].next)
	{
		ll v = e[i].to;
		if(!dfn[v])
		{
			tarjan(v,fa);
			low[x] = min(low[x], low[v]);
			if(low[v] >= dfn[x] && x != fa) iscut[x] = 1;
			if(x == fa) child++;
		}
		low[x] = min(low[x], dfn[v]);
	}
	if(x == fa && child >= 2) iscut[fa] = 1;
}
int main()
{
	freopen("testdata.in","r",stdin);
	freopen("qwq.txt","w",stdout);
	memset(head, -1, sizeof(head));
	scanf("%lld%lld",&n,&m);
	for(ll i = 1; i <= m; i++)
	{
		ll u, v;
		scanf("%lld%lld",&u,&v);
		add(u,v),add(v,u);
	}
	for(ll i = 1; i <= n; i++)
		if(!dfn[i]) tarjan(i,i);
	for(ll i = 1; i <= n; i++)
		if(iscut[i] == 1) ans++;
	printf("%lld\n",ans);
	for(ll i = 1; i <= n; i++)
		if(iscut[i] == 1)printf("%lld ",i);
	return 0;
}
posted @ 2018-07-26 16:28  Misaka_Azusa  阅读(232)  评论(0编辑  收藏  举报
Live2D