poj1523.SPF(割点模板)

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=1001;
int low[maxn];
bool iscut[maxn];
int pre[maxn];
int dfs_clock,son;
int subnets[maxn];
vector<int>G[maxn];

void init(){
    for(int i=1;i<=1000;i++)
        G[i].clear();
    mem0(pre);
    memset(iscut,false,sizeof(iscut));
    mem0(low);
    mem0(subnets);
}

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;
                if(u!=1)
                    subnets[u]++;
                else
                    son++;
            }
        }
        else if(pre[v]<pre[u]&&v!=fa){
            lowu=min(lowu,pre[v]);
        }
    }
    if(fa<0&&child==1)
        iscut[u]=false;
    low[u]=lowu;
    return lowu;
}


int main(){
    int n,m;
    int case1=1;
    while(scanf("%d",&n)!=EOF){
        int root=n;
        dfs_clock=0;
        init();
        if(n==0)
            break;
        scanf("%d",&m);
        G[n].push_back(m);
        G[m].push_back(n);
        while(scanf("%d",&n)!=EOF){
            if(n==0)
                break;
            scanf("%d",&m);
            G[n].push_back(m);
            G[m].push_back(n);
        }
        son=0;
        dfs(root,-1);
        printf("Network #%d\n",case1++);
        int flag=0;
        for(int i=1;i<=1000;i++){
            if(iscut[i]&&i!=root){
                flag=1;
                printf(" SPF node %d leaves %d subnets\n",i,subnets[i]+1);
            }
            else if(iscut[i]&&i==root){
                flag=1;
                printf(" SPF node %d leaves %d subnets\n",i,son);
            }
        }
        if(flag==0)
            printf(" No SPF nodes\n");
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(poj1523.SPF(割点模板))