码迷,mamicode.com
首页 > Web开发 > 详细

无向图求割点 UVA 315 Network

时间:2015-04-28 15:41:31      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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

 

#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;
}

 

无向图求割点 UVA 315 Network

标签:

原文地址:http://www.cnblogs.com/chenchengxun/p/4463024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!