POJ 1144 Network
Network
64-bit integer IO format: %lld Java class name: Main
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
Sample Input
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
Sample Output
1 2
Hint
Source
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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。