poj 1144 Network (tarjan割點模板)

題目鏈接:poj 1144

Network

Time Limit: 1000MS Memory Limit: 10000K

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

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

Sample Output

1
2

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.

模板題。
輸入輸出比較麻煩,要自行判斷是否到達行末。

鄰接矩陣實現:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 105
#define INF -0x7f7f7f7f

using namespace std;

int dfn[MAX];
int low[MAX];
bool vis[MAX];
int depth, n, m, root, rt_cnt, gra[MAX][MAX];

void init()
{
    root = 1;
    depth = 0;
    rt_cnt = 0;
    memset(dfn, -1, sizeof(dfn));
    memset(gra, 0, sizeof(gra));
    memset(vis, false, sizeof(vis));
    memset(low, 0, sizeof(low));
}

void dfs(int cur, int father)
{
    dfn[cur] = low[cur] = depth++;
    for(int i = 1; i <= n; i++)
    {
        if(gra[cur][i] == 1)
        {
            if(dfn[i] == -1)
            {
                dfs(i, cur);
                low[cur] = min(low[cur], low[i]);

                if(cur == root)
                    rt_cnt++;
                else if(low[i] >= dfn[cur])
                    vis[cur] = true;
            }
            else if(i != father)
            {
                low[cur] = min(low[cur], dfn[i]);
            }
        }
    }
}

int main()
{
    while(scanf("%d", &n) && n)
    {
        init();
        int tmp;
        while(scanf("%d", &tmp) && tmp)
        {
            while(getchar() != '\n')
            {
                int t;
                scanf("%d", &t);
                gra[tmp][t] = gra[t][tmp] = 1;
            }
        }

        dfs(1, root);
        int cnt = 0;
        for(int i = 2; i <= n; i++)
        {
            if(vis[i])
                cnt++;
        }
        if(rt_cnt > 1)
            cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}

運行結果:
這裏寫圖片描述

鄰接鏈表實現:

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 105
#define INF -0x7f7f7f7f

using namespace std;

int dfn[MAX], low[MAX];
vector<int> V[MAX];
bool vis[MAX];
int depth, n, m, root, rt_cnt;

void init()
{
    root = 1;
    depth = 0;
    rt_cnt = 0;
    memset(dfn, -1, sizeof(dfn));
    memset(vis, false, sizeof(vis));
    memset(low, 0, sizeof(low));
    memset(V, 0, sizeof(V));
}

void dfs(int cur, int father)
{
    dfn[cur] = low[cur] = depth++;
    for(int i = 0; i < V[cur].size(); i++)
    {
        int v = V[cur][i];
        if(dfn[v] == -1)
        {
            dfs(v, cur);
            low[cur] = min(low[cur], low[v]);

            if(cur == root)
                rt_cnt++;
            else if(low[v] >= dfn[cur])
                vis[cur] = true;
        }
        else if(v != father)
        {
            low[cur] = min(low[cur], dfn[v]);
        }
    }
}

int main()
{
    while(scanf("%d", &n) && n)
    {
        init();
        int tmp;
        while(scanf("%d", &tmp) && tmp)
        {
            while(getchar() != '\n')
            {
                int t;
                scanf("%d", &t);
                V[tmp].push_back(t);
                V[t].push_back(tmp);
            }
        }
        dfs(1, root);
        int cnt = 0;
        for(int i = 2; i <= n; i++)
        {
            if(vis[i])
                cnt++;
        }
        if(rt_cnt > 1)
            cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}

運行結果:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章