poj 1144 Network【无向图求割顶模板题】

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

用的大白的模板 1A

#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 200
int pre[maxn],iscut[maxn],dfs_clock,low[maxn];
vector<int>G[maxn];
int dfs(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    int child=0;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            child++;
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
                iscut[u]=true;
        }
        else if(pre[v]<pre[u]&&v!=fa)
            lowu=min(lowu,pre[v]);
    }
    if(fa<0&&child==1)iscut[u]=0;
    low[u]=lowu;
    return lowu;
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int n;
    while(~scanf("%d",&n)&&n)
    {
        char str[200];
        memset(pre,0,sizeof(pre));
        memset(iscut,0,sizeof(iscut));
        for(int i=1;i<=n;i++)G[i].clear();
        dfs_clock=0;
        getchar();
        while(gets(str))
        {
            if(str[0]=='0')break;
            int i,u=0,len=strlen(str);
            //printf("len=%d\n",len);
            for(i=0;i<len;i++)
            {
                if(str[i]==' ')break;
                u+=(str[i]-'0');
                u*=10;
            }
            i++;
            u/=10;
           // printf("u=%d\n",u);
            int tmp=0;
            for(;i<len;i++)
            {
                if(str[i]==' ')
                {
                    tmp/=10;
                   // printf("tmp=%d\n",tmp);
                    G[u].push_back(tmp);
                    G[tmp].push_back(u);
                    tmp=0;
                    i++;
                }
                tmp+=(str[i]-'0');
                tmp*=10;
                if(i==len-1)
                {
                    tmp/=10;
                    //printf("tmp=%d\n",tmp);
                    G[u].push_back(tmp);
                    G[tmp].push_back(u);
                    break;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!pre[i])dfs(i,-1);
            //if(!G[i].empty())for(int j=0;j<G[i].size();j++)printf("i=%d,j=%d\n",i,G[i][j]);
        }
        int num=0;
        for(int i=1;i<=n;i++)
            if(iscut[i])num++;
        printf("%d\n",num);
    }
    return 0;
}
时间: 2024-10-26 18:11:37

poj 1144 Network【无向图求割顶模板题】的相关文章

poj 1144 Network 无向图求割点

Network 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

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

UVA 315 :Network (无向图求割顶)

题目链接 题意:求所给无向图中一共有多少个割顶 用的lrj训练指南P314的模板 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=109; struct Edge { int to,next; Edge(){} Edge(int _to,int _next) { to=_to; next=_next; } }edge[N*N*2]; int head[N]; int dfn[N],lo

POJ 1144 Network(无向图连通分量求割点)

题目地址:POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u不为树根.那么(u,v)为树枝边.当Low[v]>=DFN[u]时. 然后依据这两句来找割点就能够了. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include &

无向图求割顶双连通分量

poj1144: 模板题,不过输入的方式真的真的是非常非常蛋疼...记住了... 1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define rep(i,n) for(int i=1;i<=n;i++) 8 #define clr(x,

poj 3041Asteroids 二分匹配求最小点覆盖模板题

//最大匹配=最小覆盖 //这题是求最小覆盖点的模板题 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 510; int line[maxn][maxn]; int match[maxn]; int vis[maxn]; int N , K; int find(int start) { for(int i = 1;i <=  N;

无向图求割顶和桥总结

1.求能够分成几个联通分量什么的一般都在dfs中间那里if(...>...) cnt[i],iscut[i]维护一下就OK了. 2.根结点特别需要注意. 好像就没了→_→

POJ 3694 Network(无向图求桥+重边处理+LCA)

题目大意: 给你一个无向图,然后再给你一个Q代表有Q次询问,每一次加一条边之后还有几座桥.在这里要对重边进行处理. 每次加入一条边之后,在这条搜索树上两个点的公共祖先都上所有点的桥都没了. 这里重边的处理上要说一下, 我以前第一写的时候根本没考虑这个问题,但是居然过了...过了...  很扯淡,但是重边的问题确实是存在. 这里我们 使用一个 bridge 数组来保存桥, 因为有重边的存在  只有 bridge 数量为 1 的时候这个路径才算是桥,否则则不是桥 bridge[i] 是指  i 和

POJ 1236 Network Of Schools (强连通分量模板题)

代码: #include<iostream> #include<cstdio> #include<cmath> #include<map> #include<queue> #include<vector> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rev(i,a,b)