标签:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251
Network |
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.
The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at mostN lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0.
The output contains for each block except the last in the input file one line containing the number of critical places.
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
1 2
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<vector> #define N 110 #define min(a, b)(a < b ? a : b) using namespace std; vector<vector<int> >G; int low[N], dfn[N], f[N]; int n, Time, num; bool Cut[N]; void Init() { G.clear(); G.resize(n + 1); memset(low, 0, sizeof(low));//最先到达该点的时间 memset(dfn, 0, sizeof(dfn));//该点能到达之前点的最早时间 memset(f, 0, sizeof(f));//保存一个点的父节点 memset(Cut, false, sizeof(Cut));//判断该是否为割点 Time = num = 0; } void Tarjan(int u, int fa) { int len, v, i; low[u] = dfn[u] = ++Time; f[u] = fa; len = G[u].size(); for(i = 0 ; i < len ; i++) { v = G[u][i]; if(!dfn[v]) { Tarjan(v, u); low[u] = min(low[u], low[v]); } else if(fa != v) low[u] = min(low[u], dfn[v]); } } void Solve() { int rootson = 0, i, v; Tarjan(1, 0); for(i = 2 ; i<= n ; i++) { v = f[i]; if(v == 1)//i的父节点为根节点 rootson++;//子树 else if(dfn[v] <= low[i]) Cut[v] = true; } for(i = 2 ; i <= n ; i++) if(Cut[i]) num++; if(rootson > 1) num++; } int main() { int a, b; char ch; while(scanf("%d", &n), n) { Init(); while(scanf("%d", &a), a) { while(scanf("%d%c", &b, &ch)) { G[a].push_back(b); G[b].push_back(a); if(ch == ‘\n‘) break; } } Solve(); printf("%d\n", num); } return 0; }
标签:
原文地址:http://www.cnblogs.com/qq2424260747/p/4707078.html