题目
题目链接:http://poj.org/problem?id=1144
题目来源:学习割点最基础的题目。
简要题意:求割点个数。
数据范围: N<100
题解
可以参考我的另一篇文章:
http://blog.csdn.net/xc19952007/article/details/48349055最裸的求割点的题目,求出割点直接扫一遍就行了。
实现
套上最基本的模板就行了,一个dfs解决人生问题。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 100+5;
const int M = 1e5+5;
struct Edge {
int to, nxt;
Edge(int to, int nxt) : to(to), nxt(nxt){}
Edge() {}
};
int head[N];
int dep[N];
int low[N];
bool res[N];
Edge e[M];
void addEdge(int from, int to, int no) {
e[no] = Edge(to, head[from]);
head[from] = no;
}
void dfs(int x, int p, int d) {
low[x] = dep[x] = d;
int cnt = 0, nxt;
for (int i = head[x]; ~i; i = e[i].nxt) {
nxt = e[i].to;
if (!dep[nxt]) {
cnt++;
dfs(nxt, x, d+1);
low[x] = min(low[x], low[nxt]);
res[x] |= x==1 ? cnt > 1 : dep[x] <= low[nxt];
} else if (p != nxt) {
low[x] = min(low[x], dep[nxt]);
}
}
}
int main() {
int n, u, v;
while (scanf("%d", &n) == 1 && n) {
memset(head, -1, sizeof head);
memset(res, 0, sizeof res);
memset(low, 0, sizeof low);
memset(dep, 0, sizeof dep);
int cnt = 0;
while (scanf("%d", &u) == 1 && u) {
while (scanf("%d", &v) == 1) {
addEdge(u, v, cnt++);
addEdge(v, u, cnt++);
if (getchar() == '\n') break;
}
}
dfs(1, -1, 1);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (res[i]) ans++;
}
printf("%d\n", ans);
}
return 0;
}