poj - 3177(tarjan-割边)

31 篇文章 0 订阅
30 篇文章 0 订阅

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1…F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Line 1: Two space-separated integers: F and R

Lines 2…R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1: A single integer that is the number of new paths that must be built.
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
Explanation of the sample:

One visualization of the paths is:
1 2 3
±–±--+
| |
| |
6 ±–±--+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3
±–±--+
: | |
: | |
6 ±–±--+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.

It’s possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

题目大意:给你一个连通图,问最小添加几条边使任意一个点到另一个点的方法至少有两种。
输入:第一行:顶点数 边数,随后每一行:起点 终点
输出:最小的添加方案
解题步骤:用tarjan缩点把环去掉,整个图形成由若干个强连通分量形成的弱连通,然后统计与其他的强连通分量连接的路径个数只有1的强连通分量,因为如果一个强连通分量两条路连接时,当图形成环时自然可以通过这两个路到达它。
样例:
样例的图
缩点后的图,序号为强连通分量编号,虚线是加的边
缩点后的图
代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
#define N 1000
int dfn[N],low[N];
int head[N],belong[N];//belong存每个点属于哪个分量
int degree[N];//每个强连通分量的通路的个数
stack<int> s;//tarjan的栈
int vis[N];//看是否在栈中
int n,m,cnt,k,num;
struct node
{
    int to;
    int next;
}edge[10*N];

void addedge(int u,int v)
{
    edge[cnt].to=u;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

void tarjan(int u,int fa)//缩点
{
    low[u]=dfn[u]=++k;
    s.push(u);
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if(i==(fa^1))//fa^1以后是子边的编号,因为cnt从零开始
            continue;
        if(!dfn[v])
        {
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])//强连通分量完毕
    {
        num++;
        while(1)
        {
            int v=s.top();
            s.pop();
            vis[v]=0;
            belong[v]=num;//存放每个点属于的强连通分量的序号
            if(v==u)
                break;
        }

    }
}

void init()//初始化
{
    num=k=cnt=0;
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(degree,0,sizeof(degree));
   // memset(belong,0,sizeof(belong));//输入只有一组可以不写
    while(!s.empty()) s.pop();

}


int main()
{

    while(cin>>n)
    {
        cin>>m;
        init();
        for(int i=0; i<m; i++)
        {
            int u,v;

            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }

        for(int i=1; i<=n; i++)
        {
            if(!dfn[i])
                tarjan(1,-1);
        }
		//判断相连的两个点是否在同一个分量里
        for(int i=1; i<=n; i++)
        {
            for(int j=head[i]; j!=-1; j=edge[j].next)
            {
                int v=edge[j].to;
                if(belong[i]!=belong[v])
                    degree[belong[i]]++;
            }
        }
		//统计度只有一的强连通分量
        int sum=0;
        for(int i=1; i<=num; i++)
        {
          if(degree[i]==1)
              sum++;
        }
        cout<<(sum+1)/2<<endl;  //答案是度为一的相连的边,两两组队相连形成的边,加一条边两个分量的度都会加一,偶数除以2,奇数+1除以二就变成(sum+1)/2
    }



    return 0;
}

(代码来自某位大神,附加自己对代码的理解 ,已AC)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值