poj 3352--Road Construction(双连通分量)

24 篇文章 0 订阅

题意:给定一无向连通图G,问最少再加多少条边使图变成双连通分量(删除任意一条边后任意两点之间仍然可达)。

题解:

  1. 易知求出图中各个极大双连通分量,对其进行缩点(所有low值相同的点)。
  2. 缩点以后形成的新图G‘,易知G’为连通图,若图中所有点度数均为2,可知图G‘满足双连通分量条件,所以我们统计G’中度数为1的点,假设其个数为n,用归纳法可知,连接这n个点至少需要(n+1)/2(向下取整)条边。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

class node
{
public:
    int x;
    node* next;
    node():next(0){}
};

class adjList
{
private:
    int N;
    node** adj;
public:
    adjList(int n):N(n),adj(new node*[n])
    {
        memset(adj,0,sizeof(node*)*(N));
    }
    void adjInsert(int a,int b)
    {
        node*newNode = new node;
        newNode->x = b;
        newNode->next = adj[a];
        adj[a] = newNode;
    }
    node* adjNode(int x)
    {
        return adj[x];
    }
};

class solve
{
private:
    int N,R;
    adjList* graph;
    int* DFN;
    int* low;
    int* bccIndex;
    int* degree;
    int index;
public:
    solve(int n,int r):N(n),R(r),index(1)
    {
        graph = new adjList(N+5);
        DFN = new int[N+5];
        low = new int[N+5];
        degree = new int[N+5];
        memset(DFN,0,sizeof(int)*(N+1));
        memset(degree,0,sizeof(int)*(N+5));
        processIn();
        tarjan(1,0);
        calcDegree();
        addEdge();
    }
    void processIn();
    void tarjan(int v,int u);
    void calcDegree();
    void addEdge();
};

void solve::processIn()
{
    int a,b;
    while(R--)
    {
        scanf("%d%d",&a,&b);
        graph->adjInsert(a,b);
        graph->adjInsert(b,a);
    }
    return ;
}

void solve::tarjan(int v,int u)
{
    DFN[v] = low[v] = index++;
    for(node* tmpNode = graph->adjNode(v);tmpNode != NULL;tmpNode = tmpNode->next)
    {
        int w = tmpNode->x;
        if(w != u&&w != v)
        {
            if(!DFN[w])     //树边
            {
                tarjan(w,v);
                low[v] = min(low[v],low[w]);
            }
            else if(DFN[w] < DFN[v])    //后向边
            {
                low[v] = min(low[v],DFN[w]);
            }
        }
    }
    return ;
}

void solve::calcDegree()
{
    node* tmpNode;
    for(int u = 1;u <= N;u++)
    {
    for(tmpNode = graph->adjNode(u);tmpNode != NULL;tmpNode = tmpNode->next)
    {
        int v = tmpNode->x;
        if(v != u&&low[u] != low[v])    //low值一样的都在同一个双连通分量
        {
            degree[low[u]]++;
            degree[low[v]]++;
        }
    }
    }
    return ;
}

void solve::addEdge()
{
    int num = 0;
    for(int i = 1;i <= N;i++)
    {
        if(degree[i] == 2)      //无向图每边计算了两次
        {
            num++;
        }
    }
    printf("%d\n",(num+1)>>1);  //使叶节点连通的最小边数
    return ;
}

int main()
{
    int n,r;
    while(~scanf("%d%d",&n,&r))
    {
        solve poj_3352(n,r);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值