关闭

POJ 3177 Redundant Paths 图的强连通

标签: c++poj
257人阅读 评论(0) 收藏 举报
分类:
Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11137   Accepted: 4769

Description

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.

Source

USACO 2006 January Gold

给出一个有向图G求要加几条边使得原图任意两点之间都是强连通

用tarjan缩点,求出叶子结点数,则构造图需要加(结点数+1)/2

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct Edge{
    int to,next;
    bool cut;
}e[maxn];
int low[maxn],dfn[maxn],head[maxn],Stack[maxn],belong[maxn],du[maxn];
bool instack[maxn];
int tot,top,block,pos;
void addedge(){
    int u,v;rd2(u,v);
    e[tot].to=v;e[tot].next=head[u];e[tot].cut=false;
    head[u]=tot++;
    e[tot].to=u;e[tot].next=head[v];e[tot].cut=false;
    head[v]=tot++;
}
void tarjan(int u,int pre){
    int v;
    low[u]=dfn[u]=++pos;
    Stack[top++]=u;
    instack[u]=true;
    for(int i=head[u];i!=-1;i=e[i].next){
        v=e[i].to;
        if(v==pre)continue;
        if(!dfn[v]){
            tarjan(v,u);
            if(low[u]>low[v])low[u]=low[v];
            if(low[v]>dfn[u])
                e[i].cut=e[i^1].cut=true;
        }else if(instack[v]&&low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(low[u]==dfn[u]){
        block++;
        do{
            v=Stack[--top];
            instack[v]=false;
            belong[v]=block;
        }
        while(v!=u);
    }
}
void init(){
    tot=top=block=pos=0;
    FOR(i,0,maxn-1){
        head[i]=-1;
        Stack[i]=instack[i]=dfn[i]=du[i]=0;
    }
}
void solve(int n){
    int ans(0);
    tarjan(1,0);
    for(int i=1;i<=n;++i)
        for(int k=head[i];k!=-1;k=e[k].next)
            if(e[k].cut)
                du[belong[i]]++;
    for(int i=1;i<=block;i++)
        if(du[i]==1)
            ans++;
    printf("%d\n",(ans+1)/2);
}
int main(){
    int n,m;
    while(rd2(n,m)!=EOF){
        init();FOR(i,1,m)addedge();
        solve(n);
    }
    return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Redundant Paths-POJ3177(并查集+双连通分量)

Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (w...
  • huayunhualuo
  • huayunhualuo
  • 2016-01-11 10:31
  • 328

[ACM] POJ 2186 Popular Cows (强连通分量,Kosaraju算法知识整理)

首先是一些知识整理:来源于网络: 以下转载于:http://blog.sina.com.cn/s/blog_4dff87120100r58c.html   Kosaraju算法是求解有向图强...
  • sr19930829
  • sr19930829
  • 2014-09-25 16:10
  • 1896

POJ 1236(tarjan 强连通分量 缩点)

POJ1236题目大意问,对于一个DAG(又向无环图): 1.至少要选几个点,才能从这些点出发到达所有点 2.至少加入几条边,就能从图中任何一个点出发到达所有点分析先求DAG的强连通分量数,再缩点...
  • mmy1996
  • mmy1996
  • 2017-02-22 22:11
  • 442

poj 3177 Redundant Paths(构造边双连通)

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7598  ...
  • WEYuLi
  • WEYuLi
  • 2013-08-18 17:39
  • 513

连通、弱连通

有向图的连通性 有向图强连通分量在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly con...
  • sinat_24520925
  • sinat_24520925
  • 2015-04-06 18:09
  • 2038

增加最少的边使有向图变为强连通图

http://codeforces.com/contest/22/problem/E 标题党。。。 此题是这个问题的简化版本,每个点出度至多为一。先强连通缩点,每个点出度至多为一可以保证从任一个入度为...
  • gyarenas
  • gyarenas
  • 2015-04-30 16:14
  • 1496

有向图的强连通分量的分解 总结 poj2186例题举例

常用两种算法: tarjan和korasaju算法。 学习资料: https://www.byvoid.com/blog/scc-tarjan/ https://zh.wikipedia.org...
  • liujc_
  • liujc_
  • 2016-03-26 20:58
  • 1508

求解有向图的强连通分量的SCC问题---POJ 2186 Popular Cows

【SCC问题】 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected),如果有向图G的每两个顶点都强连通,称G是一个强连通图.通俗的说法是:从图G内任意...
  • u011523762
  • u011523762
  • 2016-07-25 16:16
  • 1282

强连通图的算法

说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的求强连通分量的Tarjan算法。而提出此算法的普林斯顿大学的Robert E Tarjan教授也是1986年的图灵奖获得者(具...
  • u013946585
  • u013946585
  • 2014-10-12 23:57
  • 697

[图论] 有向图强连通分量 (kosaraju算法,Tarjan算法)

记录自己的想法:在有向图中,如果一些顶点中任意两个顶点都能互相到达(间接或直接),那么这些顶点就构成了一个强连通分量,如果一个顶点没有出度,即它不能到达其他任何顶点,那么该顶点自己就是一个强连通分量。...
  • sr19930829
  • sr19930829
  • 2014-12-09 09:46
  • 3077
    个人资料
    • 访问:376917次
    • 积分:11812
    • 等级:
    • 排名:第1476名
    • 原创:805篇
    • 转载:2篇
    • 译文:0篇
    • 评论:111条
    最新评论