割点模板
// 2割点.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://oj.daimayuan.top/course/23/problem/984
给一个n个点m条边的无向连通图,从小到大输出所有割点的编号。
输入格式
第一行包含两个数n,m(1≤n≤105,1≤m≤2×105)。
接下来m行,每行两个整数u,v表示一条无向边,没有自环,但可能有重边。
输出格式
首先输出一个整数,表示割点个数。接下来一行,若干个从小到大的数,表示割点的编号。
样例输入
7 8
1 2
2 3
3 4
2 5
4 5
5 6
5 7
5 7
样例输出
2
2 5
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int SIZE = 100010;
int head[SIZE], ver[SIZE * 4], Next[SIZE * 4];
int dfn[SIZE], low[SIZE], stack[SIZE];
int n, m, tot, num, root;
bool cut[SIZE];
void add(int x, int y) {
ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}
void tarjan(int x) {
dfn[x] = low[x] = ++num;
int flag = 0;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
flag++;
if (x != root || flag > 1) cut[x] = true;
}
}
else {
low[x] = min(low[x], dfn[y]);
}
}
}
int main()
{
cin >> n >> m;
tot = 1;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if (x == y)continue;
add(x, y); add(y, x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) root = i, tarjan(i);
vector<int> ans;
for (int i = 1; i <= n; i++){
if (cut[i]) {
ans.push_back(i);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
// 割点.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://oj.daimayuan.top/course/23/problem/984
给一个n个点m条边的无向连通图,从小到大输出所有割点的编号。
输入格式
第一行包含两个数n,m(1≤n≤105,1≤m≤2×105)。
接下来m行,每行两个整数u,v表示一条无向边,没有自环,但可能有重边。
输出格式
首先输出一个整数,表示割点个数。接下来一行,若干个从小到大的数,表示割点的编号。
样例输入
7 8
1 2
2 3
3 4
2 5
4 5
5 6
5 7
5 7
样例输出
2
2 5
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100010, M = 400010;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int stk[N];
int n, m, root;
bool cut[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++timestamp;
int flag = 0;
for (int i = h[u]; i != -1; i = ne[i]) {
int k = e[i];
if (!dfn[k]) {
tarjan(k);
low[u] = min(low[u], low[k]);
if (low[k] >= dfn[u]) {
flag++;
if (u != root || flag > 1) cut[u] = true;
}
}
else {
low[u] = min(low[u], dfn[k]);
}
}
}
int main()
{
memset(h, -1, sizeof h);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (x == y) continue;
add(x, y); add(y, x);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) root = i, tarjan(i);
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
if (cut[i]) {
ans.push_back(i);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

