无向图求割点 UVA 315 Network

输入数据处理正确其余的就是套强联通的模板了

 

#include <iostream>

#include <cstdlib>

#include <cstdio>

#include <algorithm>

#include <vector>

#include <queue>

#include <cmath>

#include <stack>

#include <cstring>

using namespace std;

#define INF 0xfffffff

#define maxn 106

#define min(a,b) (a<b?a:b)

/*无向图求桥和割点*/

vector<int> G[maxn];

int Father[maxn], time, dfn[maxn], low[maxn], n;

bool cut[maxn];



void init()

{

    memset(Father, 0, sizeof(Father));

    memset(low, 0, sizeof(low));

    memset(dfn, 0, sizeof(dfn));

    memset(cut, false, sizeof(cut));

    time = 1;

    for(int i=1; i<=n; i++)

        G[i].clear();

}

void tarjan(int u,int father)

{

    Father[u] = father;

    low[u] = dfn[u] = time ++;

    int len = G[u].size(), i, v;



    for(i=0; i<len; i++)

    {

        v = G[u][i];

        if(!low[v])

        {

            tarjan(v, u);

            low[u] = min(low[u], low[v]);

        }

        else if(v != father)

        {

            low[u] = min(low[u], dfn[v]);

        }

    }

}

void solve()

{

    int i, RootSons = 0, v, num = 0;

    tarjan(1, 0);

    for(i=2; i<=n; i++)

    {

        v = Father[i];

        if(v == 1)

            RootSons ++;

        else if(dfn[v] <= low[i])

            cut[v] = true;



    }

    if(RootSons > 1)

        cut[1] = true;

    for(i=1; i<=n; i++)

    {

        if(cut[i])

            num ++;

    }

    printf("%d\n", num);



}



int main()

{

    int a, b;

    char ch;

    while(scanf("%d",&n), n)

    {

        init();

        while( scanf("%d",&a), a)

        {

            while( scanf("%d%c",&b,&ch) )

            {

                G[a].push_back(b);

                G[b].push_back(a);

                if(ch == '\n')

                    break;

            }

        }



        solve();

    }

    return 0;

}

 

你可能感兴趣的:(NetWork)