[poj1144 Network]tarjan求割点

来源:互联网 发布:mac桌面贴便签 编辑:程序博客网 时间:2024/05/18 19:43
Network
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9767 Accepted: 4587

Description

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.

Input

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 most N 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;

Output

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

Sample Input

55 1 2 3 4062 1 35 4 6 200

Sample Output

12

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

Source

Central Europe 1996

给一个无向连通图,求割点数量。前两天离散老师讲了tarjan算法,暑假集训时根本无法理解,连连通图,强连通分量云云的概念都看得稀里糊涂,现在想想,自己的接受能力还真是有限,必须慢慢地接触一段时间才能有印象,不知道这算不算智力硬伤。。。
1144Accepted560K16MSG++1219B具体就是tarjan算法的实现了。第一次写出来都是下面这个样子,写了两个以后发现,根节点还是要另外设置参数才更好一些,放在dfs里判断看着挺混乱的,也容易出错。。。
代码:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAX = 105;vector<int> G[MAX];int dfn[MAX], low[MAX];bool vis[MAX];int n, ans;int num;void dfs(int u) {vis[u] = true;dfn[u] = low[u] = ++num;int son = 0;bool flag = false;for (int i = 0; i < G[u].size(); ++i) {int v = G[u][i];if (vis[v]) {if (low[u] > dfn[v]) low[u] = dfn[v];continue;}dfs(v);++son;if (low[u] > low[v]) low[u] = low[v];if (low[v] >= dfn[u]) flag = true;//这里的判断开始搞错了,初始化flag=true,出现low[v]<dfn[u]设为false,其实导致的问题是:即使第一次有孩子可以被“割”开,也可能被判为“不是割点”}if (u == 1) {if (son >= 2) {//printf("&&%d\n", u);++ans;}} else if (flag && son >= 1) {//printf("**%d\n", u);++ans;}}int tarjan() {ans = num = 0;memset(vis, false, sizeof(bool) * MAX);fill(low, low + MAX, MAX);//for (int i = 1; i < MAX; ++i) printf("%d ", low[i]);dfs(1);return ans;}int main() {while (~scanf(" %d", &n) && n) {for (int i = 0; i < MAX; ++i) G[i].clear();int u;while (~scanf(" %d", &u) && u) {int v;while ((getchar()) == ' ') {scanf(" %d", &v);G[u].push_back(v);G[v].push_back(u);}}printf("%d\n", tarjan());}return 0;}


0 0
原创粉丝点击