POJ1144(割点入门题)

Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11378   Accepted: 5285

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
/*
    割点:在无向连通图中,若将该点去掉那么图变为两个或两个以上连通分量。 
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=10005;
struct Edge{
    int to,next;
}es[MAXN];
int head[105];
int E;
int Min(int a,int b)
{
    return a>b?b:a;
}
void add_edge(int u,int v)
{
    es[E].to=v;
    es[E].next=head[u];
    head[u]=E++;
}
int ans;
int root;
int dfn[105];//dfs时,记录访问结点的时序 
int low[105];//记录结点的返祖最早结点 
int vis[105];
int articulation[105];
int time;
void Tarjan(int u,int fa)
{
    int son=0;
    vis[u]=1;
    dfn[u]=low[u]=++time;
    for(int i=head[u];i!=-1;i=es[i].next)
    {
        int v=es[i].to;
        if(!vis[v])//父子边 
        {
            Tarjan(v,u);
            son++;
            low[u]=Min(low[u],low[v]);
            if((u==root&&son>1)||(u!=root&&dfn[u]<=low[v]))
            {
                //1.若u为根节点且儿子不止一个,那么该点为割点
                //2.若u不为根结点,当dfn[u]==low[v]时说明存在返祖边使u处于割边与环的交点,所以其为割点
                //当dfn[u]<low[v]时,说明u指向v只有一条路径,所以u为割点
                articulation[u]=1;//不可设为ans++,因为一个割点可能连接多个割边
            }    
        }
        if(vis[v]&&v!=fa)//返祖边 
        {
            low[u]=Min(low[u],dfn[v]);
        }    
    }
}
int seek(int n)
{
    int ans=0;
    for(int i=1;i<=n;i++)
        if(articulation[i])    ans++;
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(articulation,0,sizeof(articulation));
        E=0;
        time=0;
        ans=0;
        int u,v;
        while(scanf("%d",&u)&&u!=0)
        {
            while(getchar()!='
')
            {
                scanf("%d",&v);
                add_edge(u,v);
                add_edge(v,u);
            }    
        }
        root=1;
        Tarjan(root,-1);
        int ans=seek(n);
        printf("%d
",ans);
    }
    return 0;
}

 简洁代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=105;
vector<int> mp[MAXN];
int dfn[MAXN],low[MAXN],time;
bool crit[MAXN];
int root;
void dfs(int u,int fa)
{
    dfn[u]=low[u]=++time;
    int son=0;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(!dfn[v])
        {
            dfs(v,u);
            son++;
            low[u]=min(low[u],low[v]);
            if((root==u&&son>1)||(u!=root&&dfn[u]<=low[v]))
            {
                crit[u]=true;
            }
        }
        else if(v!=fa)    low[u]=min(low[u],dfn[v]);
    }
}

int n;
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(int i=1;i<=n;i++)
            mp[i].clear();
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        time=0;
        memset(crit,false,sizeof(crit));
        int u;
        while(scanf("%d",&u)!=EOF&&u!=0)
        {
            int v;
            while(getchar()!='
')
            {
                scanf("%d",&v);
                mp[u].push_back(v);
                mp[v].push_back(u);
            }
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])
            {
                root=i;
                dfs(i,-1);
            }
        int cnt=0;
        for(int i=1;i<=n;i++)
            if(crit[i])
                cnt++;
        printf("%d
",cnt);    
        
    }
    return 0;
}

 来个JAVA版的

  1 /**
  2  * @title:    poj1144
  3  * @auther:   baneHunter
  4  * @time:     454MS
  5  * @memory:   5244K
  6 */
  7 import java.util.*;
  8 import static java.lang.System.*;
  9 public class Main{
 10     
 11     static final int MAXN=105;
 12     static class Graph{
 13         ArrayList<Integer> vertex = new ArrayList<Integer>();
 14     }
 15     
 16     static ArrayList<Integer> Input(String s)
 17     {
 18         ArrayList<Integer> v=new ArrayList<Integer>();
 19         int e=0;
 20         for(int i=0;i<s.length();i++)
 21         {
 22             if(s.charAt(i)==' ')
 23             {
 24                 v.add(e);
 25                 e=0;
 26             }
 27             else
 28             {
 29                 e*=10;
 30                 int x=(int)(s.charAt(i)-'0');
 31                 e+=x;
 32             }
 33         }
 34         v.add(e);
 35         return v;
 36     }
 37     static class Tarjan{
 38         int n;
 39         int m;
 40         Tarjan(int n)
 41         {
 42             this.n=n;
 43             for(int i=1;i<=n;i++)
 44                 map[i] = new Graph();
 45         }
 46         Graph map[] = new Graph[MAXN];
 47         int dfn[] = new int[MAXN];
 48         int low[] = new int[MAXN];
 49         int time;
 50         int root;
 51         boolean articulation[] = new boolean[MAXN];
 52         
 53         void addegde(int u,int v)
 54         {
 55             map[u].vertex.add(v);
 56             map[v].vertex.add(u);
 57             m++;
 58         }
 59         
 60         void dfs(int fa,int u)
 61         {
 62             dfn[u]=low[u]=++time;
 63             int son=0;
 64             for(int i=0;i<map[u].vertex.size();i++)
 65             {
 66                 int v=map[u].vertex.get(i);
 67                 if(dfn[v]==0)
 68                 {
 69                     dfs(u,v);
 70                     low[u]=Math.min(low[u], low[v]);
 71                     son++;
 72                     if((u==root&&son>=2)||(u!=root&&dfn[u]<=low[v]))
 73                     {
 74                         articulation[u]=true;
 75                     }
 76                 }
 77                 else    if(v!=fa)    low[u]=Math.min(low[u],dfn[v]);
 78             }
 79         }
 80         
 81         int cal()
 82         {
 83             for(int i=1;i<=n;i++)
 84             {
 85                 if(dfn[i]==0)
 86                 {
 87                     root=i;
 88                     dfs(-1,i);
 89                 }
 90             }
 91             
 92             int cnt=0;
 93             for(int i=1;i<=n;i++)
 94             {
 95                 if(articulation[i])
 96                     cnt++;
 97             }
 98             return cnt;
 99         }
100     }
101     static Scanner in = new Scanner(System.in);
102     public static void main(String[] args){
103 
104         int n;
105         while(in.hasNext())
106         {
107             n=in.nextInt();
108             in.nextLine();
109             if(n==0)    break;
110             Tarjan tar = new Tarjan(n);
111             while(true)
112             {
113                 String s;
114                 s=in.nextLine();
115                 ArrayList<Integer> v;
116                 v=Input(s);
117                 int u;
118                 u=v.get(0);
119                 if(u==0)break;
120                 for(int i=1;i<v.size();i++)
121                 {
122                     int to=v.get(i);
123                     if(to!=0)
124                     {
125                         tar.addegde(u, to);
126                         tar.addegde(to, u);
127                     }
128                 }
129             }
130             int res=tar.cal();
131             out.println(res);
132         }
133     }
134 }
原文地址:https://www.cnblogs.com/program-ccc/p/5161019.html