图论(无向图的割顶):POJ 1144 Network

Posted 既然选择了远方,便只顾风雨兼程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图论(无向图的割顶):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 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.
 
  这道题就是求无向图的割顶,一般用tarjan算法。
  刘汝佳老师的书上有写,还注明要注意一些地方,被称为"写错",可我觉得有些地方没注意只是不严谨,并不影响正确性。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=100010;
int n,fa[maxn],ch[maxn][2],sz[maxn];
int flip[maxn],pos[maxn],rt;
struct Node{
    int x,id;
}a[maxn];

void Flip(int x){
    swap(ch[x][0],ch[x][1]);
    flip[x]^=1;
}

void Push_down(int x){
    if(flip[x]){
        Flip(ch[x][0]);
        Flip(ch[x][1]);
        flip[x]=0;
    }
}

int pd[maxn];
void P(int x){
    int cnt=0;
    while(x){
        pd[++cnt]=x;
        x=fa[x];
    }
    while(cnt){
        Push_down(pd[cnt--]);
    }
}

void Push_up(int x){
    sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
}

void Rotate(int x){
    int y=fa[x],g=fa[y],c=ch[y][1]==x;
    ch[y][c]=ch[x][c^1];fa[ch[x][c^1]]=y;
    ch[x][c^1]=y;fa[y]=x;fa[x]=g;
    if(g)ch[g][ch[g][1]==y]=x;
    Push_up(y);
}

void Splay(int x,int g=0){
    P(x);
    for(int y;(y=fa[x])!=g;Rotate(x))
        if(fa[y]!=g)
            Rotate((ch[fa[y]][1]==y)==(ch[y][1]==x)?y:x);
    Push_up(x);        
    if(!g)rt=x;
}

int Build(int f,int l,int r){
    if(l>r)return 0;
    int mid=(l+r)>>1;fa[mid]=f;
    ch[mid][0]=Build(mid,l,mid-1);
    ch[mid][1]=Build(mid,mid+1,r);
    sz[mid]=1;
    Push_up(mid);
    return mid;
}

bool cmp(Node a,Node b){
    if(a.x!=b.x)
    return a.x<b.x;
    return a.id<b.id;
}

int main(){
    while(~scanf("%d",&n)&&n){
        memset(flip,0,sizeof(flip));
        rt=Build(0,1,n+2);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i].x);
        for(int i=1;i<=n;i++)
            a[i].id=i;    
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++)
            pos[i+1]=a[i].id+1;    
        pos[1]=1;pos[n+2]=n+2;
        for(int i=2,p;i<n+1;i++){
            Splay(pos[1]);
            Splay(pos[i],pos[1]);
            printf("%d ",sz[ch[ch[rt][1]][0]]+1);
            Splay(pos[i]);
            p=ch[pos[i]][1];
            while(ch[p][0]){
                Push_down(p);
                p=ch[p][0];
            }
            Push_down(p);
            Splay(pos[i-1]);
            Splay(p,pos[i-1]);
            Flip(ch[ch[rt][1]][0]);
        }
        printf("%d\n",n);
    }
    return 0;
}

 

 

以上是关于图论(无向图的割顶):POJ 1144 Network的主要内容,如果未能解决你的问题,请参考以下文章

无向图的割顶和桥

无向图的割顶和桥的性质 以及双连通分量的求解算法

Tarjan 算法求无向图的割顶和桥

DFS的运用(二分图判定无向图的割顶和桥,双连通分量,有向图的强连通分量)

2018/2/11 每日一学 无向图割顶和桥

luogu题解 P3388 模板割点(割顶)