12206. 电缆网络

电话线路公司(TLC)正在建立一个新的电话电缆网络。它们连接了n个位置。每个地方都有一个交换机。电缆是双向的,连接两个地方,电缆每一端都会连接到当地的交换机中。从每一个地方都可以通过电缆连接到其他地方(可以通过其他交换机到达某地,而未必直接连接)。
有时有的地方停电导致交换机故障。TLC官员认识到,在这样的情况下,除了停电的地方无法到达之外,也可能导致其他地方无法相互连接。在这样的情况下我们会说那个停电的地方是关键的。现在,官员们正在努力编写一个程序来查找所有这些关键地点的数量。帮帮他们。
=========================================================  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectingseveral places numbered by integers from 1 to N. No two places have the same number. The linesare bidirectional and always connect together two places and in each place the lines end in a telephoneexchange. There is one telephone exchange in each place. From each place it is possible to reachthrough lines every other place, however it need not be a direct connection, it can go through severalexchanges. 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 placewith 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 tryingto write a program for finding the number of all such critical places. Help them.

输入格式:

输入文件由多个部分组成。每个部分描述一个网络。
每个部分的第一行,给出位置个数n(n<100)。
接下来至多有N行,每行首先给出一个位置的编号i,然后出现的编号表示此位置与i相连。请注意,电缆连接是双向的,保证一条电缆一定会在某端点作为i的时候被给出。
每部分以0结束。
文件末尾,在最后一个部分以0结束后,会再给出一个0.

输出格式:

按顺序输出给出网络的关键地点数,每行一个。

样例 1 :

输入:
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
输出:
1 2

样例 2 :

输入:
5 1 2 2 3 3 4 4 5 5 1 0 5 1 2 2 3 3 4 4 5 0 0
输出:
0 3

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <vector>
  4 #include <cstring>
  5 
  6 using namespace std;
  7 
  8 const int max_N = 100 + 5;
  9 
 10 int n,a,b;
 11 // 
 12 int num[max_N],low[max_N],flag[max_N],time_index;
 13 int ans,root;
 14 char c;
 15 vector<int> g[max_N];
 16 
 17 void dfs(int cur,int fa)
 18 {
 19     // 用来记录生成树中当前顶点的子节点个数
 20     int child=0;
 21 
 22     // 每次搜索递增时间戳index
 23     ++time_index;
 24     // 给当前节点编号
 25     num[cur]=time_index;
 26     // 初始化所能到达的最早节点为当前节点
 27     low[cur]=time_index;
 28 
 29     // 遍历与此节点相邻的节点
 30     for(int i=0;i<g[cur].size();++i)
 31     {
 32         int v=g[cur][i];
 33         if(num[v]==0)
 34         {
 35             // 与当前节点相邻,且未被访问过的节点,是dfs序中当前节点的子节点
 36             ++child;
 37             // 深搜遍历当前节点的子节点
 38             dfs(v,cur);
 39             // 由于子节点可达当前节点,更新当前节点的最早可达值为当前的最小值和子节点最小值中较小的一个
 40             low[cur]=min(low[cur],low[v]);
 41             // 非根节点满足low[v]>=num[cur],
 42             if(cur!=root && low[v]>=num[cur])
 43             {
 44                 // 可能被标记多次,可用来统计此割点将多少个点与根节点断开。
 45                 // 错误警示,当时在此处直接ans++,统计错误.
 46                 flag[cur]=1;
 47             }
 48             if(cur==root && child==2)
 49             {
 50                 flag[cur]=1;
 51             }
 52         }
 53         // 如果该节点已经被访问过,且不是其父节点,更新当前可达的最小编号的值
 54         else if(v!=fa)
 55         {
 56             // low[v]?
 57             low[cur]=min(num[v],low[cur]);
 58         }
 59     }
 60 }
 61 
 62 int solve()
 63 {
 64 
 65     memset(num,0,sizeof(num));
 66     memset(flag,0,sizeof(flag));
 67     //memset(low,0,sizeof(low));
 68     ans=0;
 69     time_index=0;
 70     root=1;
 71 
 72     dfs(1,root);
 73 
 74     for(int i=1;i<=n;++i)
 75     {
 76         if(flag[i])
 77         {
 78             ++ans;
 79         }
 80     }
 81 
 82     return ans;
 83 }
 84 
 85 int main()
 86 {
 87     scanf("%d",&n);
 88     while(n)
 89     {
 90         scanf("%d",&a);
 91         while(a)
 92         {
 93             c=getchar();
 94             if(c=='\n')
 95             {
 96                 scanf("%d",&a);
 97                 continue;
 98             }
 99             scanf("%d",&b);
100             g[a].push_back(b);
101             g[b].push_back(a);
102         }
103 
104         printf("%d\n",solve());
105         for(int i=1;i<=n;++i)
106         {
107             g[i].clear();
108         }
109         scanf("%d",&n);
110     }
111     return 0;
112 }

猜你喜欢

转载自www.cnblogs.com/jishuren/p/12233891.html
今日推荐