题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251
求割点,除了输入用strtok和sscanf处理输入以外,对于求割点的tarjan算法有了进一步理解。
特别注意88行,如果u是根并且至少两个儿子,那它一定是割点无误,还有第二个情况用如图代表:
这个例子里显然:low[4]=2,dfn[4]=4,dfn[3]=3。现dfs到3点位置了,4是3的儿子,假如3是割点,那删掉3后4和2依然连通,因此3不是割点。判断依据可以是low[4]<dfn[3]。
假如low[4]>=dfn[3]的话,那3就是割点了。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 36 using namespace std; 37 38 #define fr first 39 #define sc second 40 #define cl clear 41 #define BUG puts("here!!!") 42 #define W(a) while(a--) 43 #define pb(a) push_back(a) 44 #define Rint(a) scanf("%d", &a) 45 #define Rll(a) scanf("%lld", &a) 46 #define Rs(a) scanf("%s", a) 47 #define Cin(a) cin >> a 48 #define FRead() freopen("in", "r", stdin) 49 #define FWrite() freopen("out", "w", stdout) 50 #define Rep(i, len) for(int i = 0; i < (len); i++) 51 #define For(i, a, len) for(int i = (a); i < (len); i++) 52 #define Cls(a) memset((a), 0, sizeof(a)) 53 #define Clr(a, x) memset((a), (x), sizeof(a)) 54 #define Full(a) memset((a), 0x7f7f, sizeof(a)) 55 #define lrt rt << 1 56 #define rrt rt << 1 | 1 57 #define pi 3.14159265359 58 #define RT return 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69 70 const int maxn = 220; 71 char str[66666]; 72 int n, m, rt, bcnt; 73 int G[maxn][maxn]; 74 int dfn[maxn], low[maxn], vis[maxn]; 75 int in[maxn]; 76 bool cut[maxn]; 77 vi e[maxn]; 78 int bridge[maxn][3]; 79 80 void dfs(int u, int d) { 81 int son = 0; 82 vis[u] = 1; dfn[u] = low[u] = d; 83 Rep(i, e[u].size()) { 84 int v = e[u][i]; 85 if(!vis[v]) { 86 dfs(v, d+1); son++; 87 low[u] = min(low[u], low[v]); 88 if((u==rt&&son>1)||(u!=rt&&low[v]>=dfn[u])) cut[u] = 1; 89 } 90 else low[u] = min(low[u], dfn[v]); 91 } 92 } 93 94 int main() { 95 // FRead(); 96 int u, v; 97 while(~Rint(n) && n) { 98 Cls(G); Cls(dfn); Cls(low); Cls(in); 99 Cls(vis); Cls(bridge); Cls(cut); bcnt = 0; 100 Rep(i, n+5) e[i].cl(); 101 getchar(); 102 while(gets(str) && strcmp("0", str)) { 103 char* p = strtok(str, " "); 104 sscanf(p, "%d", &u); 105 p = strtok(NULL, " "); 106 while(p) { 107 sscanf(p, "%d", &v); 108 p = strtok(NULL, " "); 109 G[u][v] = G[v][u] = 1; 110 } 111 } 112 For(i, 1, n+1) { 113 For(j, i+1, n+1) { 114 if(G[i][j]) { 115 e[i].push_back(j); 116 e[j].push_back(i); 117 } 118 } 119 } 120 rt = 1; 121 int ret = 0; 122 dfs(1, 0); 123 For(i, 1, n+1) if(cut[i]) ret++; 124 printf("%d ", ret); 125 } 126 RT 0; 127 }