uva 10972 类似poj 3352

RevolC FaeLoN

Hopefully, you can remember Koorosh from one of the previous problems. Fortunately, Koorosh has solved Basm’s problem without your help! Now, he is a high-ranked advisor of Basm.

 As usual, Basm has conquered a new territory, called RevolC FaeLoN. In order to make the people of RevolC FaeLoN satisfied, he wants to solve one of their basic problems. The RevolC FaeLoN has a high annual   rate of camel accidents and Basm wants to make the roads of this territory unidirectional in order to reduce the accident rate.

He wants to do this task in such a way that each city has at least one path to every other city. Now, Basm has asked Koorosh to find the minimum number of unidirectional roads which need to be constructed (in addition to the task of making the existing roads unidirectional) in order for every city to be reachable from every other city. Note that the original graph representing the territory is simple but during the road construction process, extra roads can be constructed between two cities that have already been connected to each other by a road.

 

The Input

Input consists of several test-cases. Each test case begins with two numbers  and m which are the number of cities and roads of RevolC FaeLoN respectively. The next m lines, each contain two integers , indicating that there is a road between u and v. The input is terminated by end of file.

 

The Output

For each test-case, print the minimum number of roads that should be constructed to make Basm’s decision possible.

 

Sample Input

3 2

1 2

2 3

10 11

1 2

2 3

3 1

3 7

4 5

5 6

6 4

7 9

6 3

9 8

7 8

 

Sample Output

1

2

 

本题要用到两个定理。

一个无向图G是边双连通的,当且仅当G中存在一条回路,它至少经过每个顶点一次。

可以给一个任意的边-双连通图的边定向,使它成为一个强连通图。

有了这两个定理后,我们只需求出边双连通分量,缩点后,计算度数为1的点的个数为a,度数为0的点的个数为b,则(a+2*b)/2的上取整就是答案。因为对无向无环图来说,要形成一个存在一条回路至少经过每个点一次的图,则每个点度数至少是2,并且是一定能通过合理的添边使新图符合要求的。(这里的证明不大好解释,但可以自己找一些图验证)。然后还要注意一个特殊情况,假如原图就是边双连通的,答案应该是0.

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define max_n 1005
#define max_m 10005
int n,m;
struct e
{
    int data;
    e* next;
};
e edge[max_n];

int dfn[max_n],sta[max_n],low[max_n],scc[max_n];
int top,idx,sum;//sum记录连通分量的个数
int du[max_n];

void init()
{
    top=0;
    idx=0;
    sum=0;
    memset(dfn,0,sizeof(dfn));
    memset(du,0,sizeof(du));
    memset(low,0,sizeof(low));
}
void tar(int s,int f)
{
    int i,j,k;
    low[s]=dfn[s]=++idx;
    sta[++top]=s;
    e *p=edge[s].next;
    while(p)
    {
        if(p->data==f)
        {
            p=p->next;
            continue;
        }
        if(dfn[p->data]==0)
        {
            tar(p->data,s);
            low[s]=min(low[s],low[p->data]);
        }
        else
            low[s]=min(low[s],dfn[p->data]);
        p=p->next;
    }
    if(low[s]==dfn[s])//找到强连通分量,出栈
    {
        sum++;
        do
        {
            i=sta[top--];
            scc[i]=sum;
        }
        while(i!=s);
    }
}

int main()
{
    int a,b;
    while(cin>>n>>m)
    {
        init();
        for(int i=1; i<=n; i++)
            edge[i].next=0;
        for(int i=0; i<m; i++)
        {
            cin>>a>>b;
            e *p=new e;
            p->data=b;
            p->next=edge[a].next;
            edge[a].next=p;

            e *q=new e;
            q->data=a;
            q->next=edge[b].next;
            edge[b].next=q;
        }
        for(int i=1; i<=n; i++)
            if(!dfn[i])
                tar(i,0);
        for(int i=1; i<=n; i++)
        {
            e *p=edge[i].next;
            while(p)
            {
                if(scc[p->data]!=scc[i])
                {
                    du[scc[p->data]]++;
                    du[scc[i]]++;
                }
                p=p->next;
            }
        }
        int ans=0,ans1=0;
        for(int i=1; i<=sum; i++)
        {
            if(du[i]==2)
                ans++;
            if(du[i]==0)
            ans1++;
        }
        if(sum==1)
        cout<<"0"<<endl;
        else
        cout<<((ans+2*ans1)+1)/2<<endl;
    }
    return 0;
}
/*
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
3 3
1 2
2 3
1 3
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值