Tarjan 割点,桥

时间:2022-06-15 23:04:56
/*         ggg   ggg
ggggggg ggggggg
ggggggggggggggggggg
ggggggggggggggg
ggggggggggg
ggggggg
ggg
g
*/
/* gyt
Live up to every day */ #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = +;
const ll maxm = 1e7;
const int mod = 1e9 + ;
const int INF = 0x3f3f3f;
const ll inf = 1e15 + ;
const db eps = 1e-;
#define N 201
vector<int>G[N];
int n,m,low[N],dfn[N];
bool is_cut[N];
int father[N];
int tim=; void init() {
memset(dfn, -, sizeof(dfn));
memset(father, , sizeof(father));
memset(low, -, sizeof(low));
memset(is_cut, , sizeof(is_cut));
}
void Tarjan(int i, int Father) {
father[i] = Father;
dfn[i] = low[i] = tim++;
for (int j = ; j < G[i].size(); j++) {
int k = G[i][j];
if (dfn[k]==-) {
Tarjan(k, i);
low[i] = min(low[i], low[k]);
}
else if(Father != k) {
low[i] = min(low[i], dfn[k]);
}
}
}
void cnt() {
int rootson=;
Tarjan(, );
for(int i=;i<=n;++i){
int v=father[i];
if(v==)
rootson++;/*统计根节点子树的个数,根节点的子树个数>=2,就是割点*/
else{
if(low[i]>=dfn[v])/*割点的条件*/
is_cut[v]=true;
}
}
if(rootson>)
is_cut[]=true;
for(int i=;i<=n;++i)
if(is_cut[i])
printf("%d\n",i);
for(int i=;i<=n;++i){
int v=father[i];
if(v>&&low[i]>dfn[v])/*桥的条件*/
printf("%d,%d\n",v,i);
}
}
void solve() {
init();
// scanf("%d%d", &n, &m);
cin >> n >> m;
for (int i = ; i < N; i++) G[i].clear();
for (int i = ; i < m; i++) {
int a, b; scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
int rootson=;
cnt();
}
int main() {
int t = ;
//scanf("%d", &t);
while(t--)
solve();
return ;
}