poj 1144 割点模板题

题目大意:

求割点;

基本思路:

tarjan算法,套板子

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 110+10;

bool vis[maxn];
int dfn[maxn],low[maxn],cut[maxn];
vector<int>gra[maxn];
int cnt=0;
void cutPoint(int fa,int u){
    vis[u]=true;
    dfn[u]=low[u]=++cnt;
    int child=0;
    int sz=gra[u].size();
    for(int i=0;i<sz;i++){
        int v=gra[u][i];
        if(v!=fa&&vis[v]){
            low[u]=min(low[u],dfn[v]);
        }else if(!vis[v]){
            child++;
            cutPoint(u,v);
            low[u]=min(low[u],low[v]);
            if((fa==-1&&child>1)||(fa!=-1&&low[v]>=dfn[u])){
                cut[u]=1;
            }
        }
    }
}
int main(){
    int n;
    while(scanf("%d",&n)==1&&n){
        if(!n) break;
        cnt=0;
        for(int i=1;i<=n;i++){
            gra[i].clear();
        }
        memset(vis,false,sizeof(vis));

        memset(cut,0,sizeof(cut));
        int u,v;
        while(scanf("%d",&u)==1&&u){
            while(getchar()!='\n'){
                scanf("%d",&v);
                gra[u].push_back(v);
                gra[v].push_back(u);
            }
        }
        for(int i=1;i<=n;i++){
            if(!vis[i]){
                cutPoint(-1,i);
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            if(cut[i]){
                ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

posted @ 2018-03-28 15:09  愿~得偿所愿,不负时光  阅读(202)  评论(0编辑  收藏  举报