POJ 3177 Redundant Paths 图的强连通

原创 2015年11月19日 21:14:14
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址:  POJ 3352 Road...
  • hcbbt
  • hcbbt
  • 2014-08-15 11:22
  • 1000

【POJ】3177 Redundant Paths 边连通

Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8849 ...

POJ3177 Redundant Paths 3352的双胞胎题 双连通分量

虽然基本上和3352是一样的题意,但是要数据比较严格,这题有可能出现重边,而3352不会有重边。 思路: 直接在POJ3352,即上一篇博文上的代码稍微改动。 新建一个char matc...

POJ3177_Redundant_Paths_边双连通分量_tarjan

题意: 给一个图,问你最少添加多少条边可以成为一个双连通图(就是去掉任何一条边后图仍然连通) 题解:【摘自北大的集训课件】 只需在求出所有的桥以后,把桥边删除,原图变成了多个连通块,则每个连通块...

POJ 3177 Redundant Paths (双连通)

题目地址:POJ 3177 找出各个双连通分量度数为1的点,然后作为叶子节点,那么ans=(叶子结点数+1)/2。需要注意的是有重边。 代码如下: #include #include #in...

[POJ3177]Redundant Paths 边双连通分量 做题笔记

题目来源:http://poj.org/problem?id=3177 解题思路:http://www.cnblogs.com/frog112111/p/3367039.html 分析:在同一个...

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

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7598  ...

poj 3177 Redundant Paths 边双连通分量+缩点

题意:给定n个点m条边。要求

poj 3177 Redundant Paths 【图论-边双连通】

Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Description In order t...

POJ 3177 Redundant Paths(边双连通分量+缩点)

POJ 3177 Redundant Paths(边双连通分量+缩点) http://poj.org/problem?id=3177 题意:给你一个无向连通图,问你至少需要添加几条边能使得该图是一...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)