首页 > 代码库 > Network (poj1144)
Network (poj1144)
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is
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.
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
55 1 2 3 4062 1 35 4 6 200Sample Output
12Hint
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #include<queue> 5 #include<math.h> 6 #include<stdlib.h> 7 #include<stack> 8 #include<stdio.h> 9 #include<ctype.h>10 #include<map>11 #include<vector>12 using namespace std;13 vector<int>vec[1000];14 char ans[10000];15 bool flag[10000];16 int pre[1000];17 int low[1000];18 int tr[1000];19 int sizee = 0;20 int dfs(int u,int fa);21 int main(void)22 {23 int n;24 while(scanf("%d",&n),n!=0)25 {26 sizee = 0;27 int t;28 memset(flag,0,sizeof(flag));29 memset(pre,0,sizeof(pre));30 memset(low,0,sizeof(low));31 memset(tr,0,sizeof(tr));32 for(int i = 0; i < 1000; i++)33 vec[i].clear();34 while(scanf("%d",&t),t!=0)35 {36 int i,j;37 int id;38 gets(ans);39 int l = strlen(ans);40 int sum = 0;41 for(i = 0; i <= l; )42 {43 if(ans[i]>=‘0‘&&ans[i]<=‘9‘)44 {45 sum = 0;46 for(j = i; ans[j]!=‘ ‘&&ans[j]!=‘\0‘&&j <= l; j++)47 {48 sum = sum*10;49 sum+=ans[j]-‘0‘;50 }51 i = j;52 vec[t].push_back(sum);53 vec[sum].push_back(t);54 }55 else i++;56 }57 }58 dfs(1,-1);59 int sum = 0;60 for(int i = 1; i <= n; i++)61 {62 sum+=tr[i];63 }64 printf("%d\n",sum);65 }66 return 0;67 }68 int dfs(int u,int fa)69 {70 pre[u] = low[u] = ++sizee;71 int child = 0;72 for(int i = 0; i < vec[u].size(); i++)73 {74 int ic = vec[u][i];75 if(!pre[ic])76 {77 child++;78 int lowv = dfs(ic,u);79 low[u] = min(low[u],lowv);80 if(lowv >= pre[u])81 {82 tr[u] = 1;83 }84 }85 else if(pre[ic] < pre[u]&&ic!=fa)86 {87 low[u] = min(low[u],pre[ic]);88 }89 }90 if(fa < 0&& child == 1)tr[u] = 0;91 return low[u];92 }
Network (poj1144)