5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
1 2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 110; 18 int n,cnt,dfn[maxn],low[maxn]; 19 vector<int>g[maxn]; 20 bool iscut[maxn]; 21 void tarjan(int u,int fa) { 22 dfn[u] = low[u] = ++cnt; 23 int v,son = 0; 24 for(v = 0; v < g[u].size(); v++) { 25 if(!dfn[g[u][v]]) { 26 son++; 27 tarjan(g[u][v],u); 28 if(low[u] > low[g[u][v]]) low[u] = low[g[u][v]]; 29 if(low[g[u][v]] >= dfn[u]) iscut[u] = true; 30 } else if(g[u][v] != fa && low[u] > dfn[g[u][v]]) 31 low[u] = dfn[g[u][v]]; 32 } 33 if(fa < 0 && son == 1) iscut[u] = false; 34 } 35 int main() { 36 int u,v,ans; 37 while(scanf("%d",&n),n) { 38 for(int i = 0; i <= n; i++){ 39 g[i].clear(); 40 dfn[i] = low[i] = 0; 41 iscut[i] = false; 42 } 43 while(scanf("%d",&u),u) { 44 while(scanf("%d",&v)) { 45 g[u].push_back(v); 46 g[v].push_back(u); 47 if(getchar() == '\n') break; 48 } 49 } 50 ans = cnt = 0; 51 tarjan(1,-1); 52 for(int i = 1; i <= n; i++) 53 ans += iscut[i]; 54 printf("%d\n",ans); 55 } 56 return 0; 57 }
。。再写一次。。。复习下
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 200; 6 struct arc{ 7 int to,next; 8 arc(int x = 0,int y = -1){ 9 to = x; 10 next = y; 11 } 12 }e[maxn*maxn]; 13 int head[maxn],dfn[maxn],low[maxn],tot,idx,ans; 14 void add(int u,int v){ 15 e[tot] = arc(v,head[u]); 16 head[u] = tot++; 17 } 18 void tarjan(int u,int fa){ 19 dfn[u] = low[u] = idx++; 20 bool flag = true,iscut = false; 21 int son = 0; 22 for(int i = head[u]; ~i; i = e[i].next){ 23 if(flag && e[i].to == fa){ 24 flag = false; 25 continue; 26 } 27 if(dfn[e[i].to] == -1){ 28 tarjan(e[i].to,u); 29 son++; 30 low[u] = min(low[u],low[e[i].to]); 31 if(fa == -1 && son >= 2 || fa != -1 && low[e[i].to] >= dfn[u]) iscut = true; 32 }else if(fa != e[i].to) low[u] = min(low[u],dfn[e[i].to]); 33 } 34 ans += iscut; 35 } 36 int main(){ 37 int n,tmp,u,v; 38 while(scanf("%d",&n),n){ 39 idx = tot = ans = 0; 40 memset(dfn,-1,sizeof(dfn)); 41 memset(low,-1,sizeof(low)); 42 memset(head,-1,sizeof(head)); 43 bool flag = false; 44 while(scanf("%d",&u),u){ 45 while(~scanf("%d",&v)){ 46 add(u,v); 47 add(v,u); 48 if(getchar() == '\n') break; 49 } 50 } 51 for(int i = 1; i <= n; ++i) 52 if(dfn[i] == -1) tarjan(i,-1); 53 printf("%d\n",ans); 54 } 55 return 0; 56 }