UVA 315 Network 无向图求割点

一、内容

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectingseveral places numbered by integers from 1 toN. 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.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first lineof each block there is the number of placesN<100. Each of the next at mostNlines contains thenumber of a place followed by the numbers of some places to which there is a direct line from this place.These at mostNlines completely describe the network, i.e., each direct connection of two places inthe network is contained at least in one row. All numbers in one line are separated by one space. Eachblock ends with a line containing just ‘0’. The last block has only one line withN= 0.

Output

The output contains for each block except the last in the input file one line containing the number ofcritical places.Sample

Input

5
5  1  2  3  4
0
6
2  1  3
5  4  6  2
0
0

Sample Output

1
2

二、思路

  • 无向图求割点。

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105, M = 2e4 + 5;
struct E {int v, next;} e[M];
int n, u, v, len, root, h[N], num, dfn[N], low[N];
char c;
bool cut[N];
void add(int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
void tarjan(int u) {
	dfn[u] = low[u] = ++num;
	int cnt = 0; //满足条件的子节点 
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
			if (dfn[u] <= low[v]) {
				cnt++;
				if (u != root || cnt > 1) cut[u] = true; 
			}
		} else low[u] = min(low[u], dfn[v]);
	}
}
int main() {
	while (scanf("%d", &n), n) {
		memset(h, 0, sizeof(h)); len = 1, num = 0;
		memset(dfn, 0, sizeof(dfn));
		memset(cut, false, sizeof(cut));
		while (scanf("%d", &u), u) {
			while (scanf("%c", &c), c != '\n') {
				scanf("%d", &v); add(u, v); add(v, u);
			}
		}
		for (root = 1; root <= n; root++) if (!dfn[root]) tarjan(root);
		int ans = 0;
		for (int i = 1; i <= n; i++) if (cut[i]) ans++;
		printf("%d\n", ans);
	} 
	return 0;
}
发布了446 篇原创文章 · 获赞 455 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104429314
315
今日推荐