POJ 1523 SPF 点双连通分量

题目:http://poj.org/problem?id=1523


题意:给定一个连通图,求这个连通图的割点,以及去掉这个割点后连通分量的个数


思路:模板题,,,就是输入有点小恶心


#include 
#include 
#include 
#include 
#include 
using namespace std;

const int N = 1010;
struct edge
{
    int to, next;
}G[N*100];
int dfn[N], low[N], dcc[N], st[N], head[N], subnet[N];
int index, top, num, cnt, son;
bool vis[N];
int n, x = 0;
void init()
{
    memset(dfn, -1, sizeof dfn);
    memset(head, -1, sizeof head);
    memset(subnet, 0, sizeof subnet);
    index = top = num = cnt = 0;
}
void add_edge(int v, int u)
{
    G[cnt].to = u;
    G[cnt].next = head[v];
    head[v] = cnt++;
}
void tarjan(int v, int fa)
{
    dfn[v] = low[v] = index++;
    int u;
    for(int i = head[v]; i != -1; i = G[i].next)
    {
        u = G[i].to;
        if(u == fa) continue;
        if(dfn[u] == -1)
        {
            tarjan(u, v);
            low[v] = min(low[v], low[u]);
            if(dfn[v] <= low[u])
            {
                if(v == 1) son++;
                else subnet[v]++;
            }
        }
        else low[v] = min(low[v], dfn[u]);
    }
}
void slove()
{
    son = 0;
    tarjan(1, -1);
    if(son > 1) subnet[1] = son - 1;
    bool f = false;
    if(x) printf("\n");
    printf("Network #%d\n", ++x);
    for(int i = 1; i <= n; i++)
        if(subnet[i])
            printf("  SPF node %d leaves %d subnets\n", i, subnet[i] + 1), f = true;
    if(! f) printf("  No SPF nodes\n");
}

int main ()
{
    int a, b;
    while(scanf("%d", &a), a)
    {
        init();
        n = -1;
        scanf("%d", &b);
        add_edge(a, b);
        add_edge(b, a);
        n = max(n, max(a, b));
        while(scanf("%d", &a), a)
        {
            scanf("%d", &b);
            add_edge(a, b);
            add_edge(b, a);
            n = max(n, max(a, b));
        }
        slove();
    }

    return 0;
}



你可能感兴趣的:(连通分量分解)