0
点赞
收藏
分享

微信扫一扫

【模板】割点(割顶) Tarjan

题目背景

割点

题目描述

给出一个【模板】割点(割顶) Tarjan_#define

输入输出格式

输入格式:

第一行输入【模板】割点(割顶) Tarjan_#define_02

下面【模板】割点(割顶) Tarjan_i++_03

输出格式:

第一行输出割点个数

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

输入输出样例

输入样例#1:

复制

6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6

输出样例#1: 复制

1 
5

说明

对于全部数据,【模板】割点(割顶) Tarjan_#include_04

点的编号均大于【模板】割点(割顶) Tarjan_#define_05

tarjan图不一定联通。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#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 pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair 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;
}

 

 

EPFL - Fighting

举报

相关推荐

0 条评论