poj3177

Redundant Paths

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13190   Accepted: 5618

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

题目大意:

有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场。奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以选择至少两条独立的路。现在F个牧场的任何两个牧场之间已经至少有一条路了,奶牛们需要至少有两条。
给定现有的R条直接连接两个牧场的路,F-1<=R<=10000,计算至少需要新修多少条直接连接两个牧场的路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指没有公共边的路,但可以经过同一个中间顶点

题解:

tarjan求割边(桥)的模板

大致同 vijos P1325桐桐的糖果计划

一个有桥的连通图,如何把它通过加边变成边双连通图

方法为首先求出所有的桥,然后删除这些桥边,剩下的每个连通块都是一个双连通子图。把每个双连通子图收缩为一个顶点,再把桥边加回来,最后的这个图一定是一棵树,边连通度为1。

其实,本题数据保证是一个连通图,那么桥=缩点数-1;

统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

AC代码:

#include<cstdio>
#include<stack>
using namespace std;
#define N 5005
struct node{
    int v,next;
}e[N<<1];
int n,m,tot,head[N],low[N],dfn[N],id[N],in[N],pd,sd;
bool mark[N],mp[N][N];
stack<int>s;
void add(int x,int y){//判断是不是父边
    e[++tot].v=y;
    e[tot].next=head[x];
    head[x]=tot;
}
bool judge(int x,int y){
    if((x&1)&&y==x+1) return 1;
    if(!(x&1)&&y==x-1) return 1;
    return 0;
}
void tarjan(int v,int fa){
    low[v]=dfn[v]=++pd;
    s.push(v);
    mark[v]=1;
    for(int i=head[v];i;i=e[i].next){
        if(judge(i,fa)) continue;
        int w=e[i].v;
        if(!dfn[w]){
            tarjan(w,i);
            low[v]=min(low[v],low[w]);
        }
        else if(mark[w]){
            low[v]=min(low[v],dfn[w]);
        }
    }
    int u;
    if(low[v]==dfn[v]){
        sd++;
        do{
            u=s.top();
            s.pop();
            id[u]=sd;
            mark[u]=0;
        }while(u!=v);
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1,x,y;i<=m;i++){
         scanf("%d%d",&x,&y);
         if(!mp[x][y]){
             add(x,y);add(y,x);
             mp[x][y]=mp[y][x]=1;
         }
    }
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i,-1);
    for(int i=1;i<=n;i++){
        for(int j=head[i];j;j=e[j].next){
            if(id[i]!=id[e[j].v]){
                in[id[i]]++;
            }
        }
    }
    int ans=0;
    for(int i=1;i<=sd;i++) if(in[i]==1) ans++;
    printf("%d\n",(ans+1)/2);
    return 0;
}
时间: 2024-12-23 22:36:43

poj3177的相关文章

poj3177 Redundant Paths 边双连通分量

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define eps 1e-

POJ3177 Redundant Paths 双连通分量

Redundant Paths 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

poj3177 &amp;&amp; poj3352 边双连通分量缩点

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 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

POJ3177 Redundant Paths【边双联通分量】【Tarjan】

题目链接: http://poj.org/problem?id=3177 题目大意: Bessie的农场有F块牧场,已知当前任意两个农场之间至少有一条路径相连(并不一定直接相连) 为了从某块牧场移动到另一块牧场,Bessie和她的伙伴经常需要经过腐烂的树林.奶牛们特别 反感经过不好走的路,于是Bessie决定在农场种再建几条路,使得在去某个地方时总能够有两 条完全独立的路可够选择.那么问题来了:F块牧场,R条路,问至少再修几条路就能使得农场 中任意两个牧场之间都有至少两条相互独立的路径. 思路:

POJ3177 Redundant Paths (双联通缩点)

求对于给定一个连通图,加多少条边可以变成边双连通图. 一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树.需要加的边为(leaf+1)/2 (leaf为叶子结点个数). 对于此题,有重边但重边不加入计算. 重边的话,要么在开始去掉,要么用桥来计算入度. 因为桥不属于任何一个边双连通分支,其余的边和每个顶点都属于且只属于一个边双连通分支.对于重边而言,只有一对边被标记为桥,而对于所有重边来言,belong[u]和belong[v]都是不一样的,那么如果用belong[u]!

poj3177 Redundant Paths

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 forc

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       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 T

poj3177(边双连通分量+缩点)

传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路.两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点. 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,因此同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图的桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加

【BZOJ1718】&amp;&amp;【POJ3177】Redundant Paths

1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 452  Solved: 239[Submit][Status][Discuss] Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another f