以下是两题的链接:
https://vjudge.net/problem/POJ-3177
https://vjudge.net/problem/POJ-3352
这两题其实是差不多的 3177有重边得去重
下面贴上我的代码
#include<iostream>
#include<cstring>
#include<stack>
#include<stdio.h>
using namespace std;
const int maxn = 1e5 + 10;
int head[maxn];
int dfn[maxn];
int low[maxn];
int colour[maxn];
int dv[maxn];
int indu[maxn];
int dist[maxn];
int tot;
int dex;
int cnt = 0;
int cnt1;
int n, m;
int ans;
struct node{
int to;
int next;
int from;
node() {}
node(int a, int b, int c) : to(a), next(b), from(c) {}
}edge[maxn], edge1[maxn];
void edgeadd(int a, int b){
for(int i = head[a]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(v == b)
return;
}
edge[tot] = node(b, head[a], a);
head[a] = tot++;
edge[tot] = node(a, head[b], b);
head[b] = tot++;
}
void init(){
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(indu, 0, sizeof(indu));
tot = 0;
dex = 0;
ans = 0;
cnt1 = 0;
}
stack<int> s;
void tarjan(int u, int rt){
dfn[u] = low[u] = ++dex;
//cout << u << " " << rt << endl;
s.push(u);
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != rt){
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]){
while(1){
int now = s.top();
colour[now] = u;
s.pop();
if(now == u) break;
}
}
}
int main(){
ios::sync_with_stdio(false);
while(cin >> n >> m){
init();
int x, y;
for(int i = 1; i <= m; i++){
cin >> x >> y;
edgeadd(x, y);
}
for(int i = 1; i <= n; i++){
if(!dfn[i])
tarjan(i, i);
}
for (int i = 1; i <= n; i++) {
for (int j = head[i]; j != -1; j = edge[j].next) {
int o, g;
o = colour[edge[j].from];
g = colour[edge[j].to];
if(o != g){
indu[g]++;
indu[o]++;
}
}
}
for(int i = 1; i <= n; i++){
if(indu[i] == 2){
cnt++;
}
}
cout << (cnt + 1) / 2 << endl;
}
return 0;
}