POJ 1144-Network(无向连通图求割点数)

Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16069   Accepted: 7273

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.

Source

Central Europe 1996

题目大意:给你一个无向的连通图,让你求这个图的割点数目,对于求割点的数目呢,主要看两种点:

1、根节点(这个随便定义一个即可,有两个子节点就为割点)

2、子节点:(该节点的某个子节点能够访问到最小根序号的大于该节点的时间戳)也就是该节点的子节点不能访问到该节点的的祖先节点,那么该节点就为一个割点,为什么呢,因为删除了该节点的话,那么那个子节点一定与原来的图没有连通性了啊,自己画个树形图其实很好理解的。

3、最后运用tarjan算法进行求解即可O(n+e)。
其实枚举每个点删除,然后dfs判断删除后图的连通性,其实也可以实现,这样求解的复杂度为O(n*(n+e))。
AC代码:tarjan算法( 0ms):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, cnt, idx, ans;
int head[110], dfn[110], low[110], instack[110];

struct edge
{
    int to;
    int next;
}e[110*110];

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void tarjan(int k)
{
    dfn[k] = low[k] = idx ++;
    int s = 0;
    for(int i = head[k]; i != -1; i = e[i].next) {
        int en = e[i].to;
        if(!dfn[en]) {
            s ++;
            tarjan(en);
            low[k] = min(low[en], low[k]);
            if((k == 1 && s >= 2) || (k != 1 && low[en] >= dfn[k])) //两个条件的判断
                instack[k] = 1; //标记该节点为 割点
                
        }else {
            low[k] = min(low[k], dfn[en]);
        }
    }
    if(instack[k]) ans ++;
}

void init()
{
    mem1(head);
    mem0(low);
    mem0(dfn);
    mem0(instack);
    cnt = ans = 0;
    idx = 1;
}

int main()
{
    int m, k;
    char str;
    while(~scanf("%d", &n) && n) {
        init();
        while(scanf("%d", &m) && m) { //这个输入有点小技巧,可以学习下
            while(getchar() != '\n') {
                scanf("%d", &k);
                add(m, k);
                add(k, m);
            }
        }
        tarjan(1);
        out1(ans);
    }
    return 0;
}
AC代码:dfs枚举(32ms):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, cnt, idx, ans;
int head[110], vis[110];

struct edge
{
    int to;
    int next;
}e[110*110];

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void dfs(int k, int fuck)
{
    for(int i = head[k]; i != -1; i = e[i].next) {
        int en = e[i].to;
        if(!vis[en] && en != fuck) {
            vis[en] = 1;
            dfs(en, fuck);
        }
    }
}

void init()
{
    mem1(head);
    cnt = ans = 0;
    idx = 1;
}

int main()
{
    int m, k;
    char str;
    while(~scanf("%d", &n) && n) {
        init();
        while(scanf("%d", &m) && m) { //这个输入有点小技巧,可以学习下
            while(getchar() != '\n') {
                scanf("%d", &k);
                add(m, k);
                add(k, m);
            }
        }
        for(int i = 1; i <= n; i ++) {
            mem0(vis);
            int u = 1;
            if(i < n) u = i+1; //找不同节点开始遍历
            vis[u] = 1;
            dfs(u, i);
            int flag = 0;
            for(int j = 1; j <= n; j ++) {
                if(!vis[j] && j != i) flag = 1;
            }
            if(flag) ans ++;
        }
        out1(ans);
    }
    return 0;
}

你可能感兴趣的:(ACM_强连通)