题意:给你一张无向图,求割点的个数。
思路:输入稍微处理一下接着直接套模版。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #include <queue> 8 #include <stack> 9 #include <vector> 10 #include <set> 11 #include <map> 12 #define MP(a, b) make_pair(a, b) 13 #define PB(a) push_back(a) 14 15 using namespace std; 16 17 typedef long long ll; 18 typedef pair<int, int> pii; 19 typedef pair<unsigned int, unsigned int> puu; 20 typedef pair<int, ll> pil; 21 typedef pair<int,double> pid; 22 23 const int INF = 0x3f3f3f3f; 24 const double eps = 1e-6; 25 const int LEN = 101; 26 int cnt, n, cut[LEN], low[LEN], dfn[LEN], vis[LEN]; 27 vector<int> Map[LEN]; 28 29 //割点模版初始化vis[] cut[]置0 30 //求割点若有重边需要判重 31 void dfs(int u, int fa){ 32 int v, i, son = 0; 33 vis[u] = 1; 34 dfn[u] = low[u] = cnt ++; 35 for(i = 0; i < Map[u].size(); i ++){ 36 v = Map[u][i]; 37 if(vis[v] == 1 && v != fa) 38 low[u] = min(low[u], dfn[v]); 39 if(vis[v] == 0){ 40 dfs(v, u); 41 son ++; 42 low[u] = min(low[u], low[v]); 43 if((fa < 0 && son > 1) || (fa > 0 && low[v] >= dfn[u])) 44 cut[u] = true; 45 //if(low[v] > dfn[u]){ 46 // bridge[nbri][0] = u; bridge[nbri++][1] = v; //边(i,j)是桥。 47 //} 48 } 49 } 50 vis[u] = 2; 51 } 52 53 int main() 54 { 55 // freopen("in.txt", "r", stdin); 56 57 int from, to; 58 while(scanf("%d", &n)!=EOF && n){ 59 for(int i=0; i<LEN; i++)Map[i].clear(); 60 while(scanf("%d", &from) && from){ 61 while(getchar()!=' '){ 62 scanf("%d", &to); 63 Map[from].PB(to); 64 Map[to].PB(from); 65 } 66 } 67 cnt = 0; 68 memset(vis, 0, sizeof vis); 69 memset(cut, 0, sizeof cut); 70 dfs(1,-1); 71 int ans = 0; 72 for(int i=1; i<=n; i++)ans+=cut[i]; 73 printf("%d ", ans); 74 } 75 return 0; 76 }