Redundant Paths

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 descri_ption 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.

题目大意:有n个草场,m条无向边。问让图形成边双连通图需要最少新建多少条无向边。图中给的有重边。

解题思路:我们可以先画出缩点后的图,这时已经成了一棵树。那么我们可以看出,要想形成边双连通图,需要没有桥,所以,只要将叶子结点连一条边即可。记叶子个数为leaf。所以需要新建的边即为(leaf+1)/2。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 5100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*4]; //无向图的边
int head[maxn], tot; //邻接表
int dfs_clock, dfn[maxn]; //时间戳
//Stack:存放边双连通节点、instack:在栈中、ebccno:节点所在分量编号、ebcc_cnt分量数目(从1开始编号)
int Stack[maxn], instack[maxn], top, ebccno[maxn], ebcc_cnt;
int deg[maxn];//记录缩点的度
void init(){
tot = 0;
dfs_clock = 0;
top = 0;
ebcc_cnt = 0;
memset(deg,0,sizeof(deg));
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_v,head[_u]);
head[_u] = tot++;
}
int dfs(int u,int fa){ //这里的fa是记录的边的编号,用来处理重边
int lowu = dfn[u] = ++dfs_clock;
Stack[++top] = u; //将每个访问的结点放入栈中
// instack[u] = 1;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){ //如果v没有访问过
int lowv = dfs(v,i); //v及v的后代能访问到的最远祖先
lowu = min(lowu,lowv); //用后代来更新lowu
//如果v已经在栈中了,并且这条边不是回边(一条无向边,拆成了有向边,回指了父亲的这条有向边)
}else if(dfn[v] < dfn[u] && (fa^1) != i){ //有人在这里用instack[v]替代了判断已经在栈中
lowu = min(lowu,dfn[v]); //用反向边更新lowu
}
}
if(dfn[u] == lowu){ //找到一个边双连通分量
ebcc_cnt++;
for(;;){
int v = Stack[top--];
// instack[v] = 0;
ebccno[v] = ebcc_cnt; //把节点划分到分量中
if(u == v){
break;
}
}
}
// low[u] = lowu;
return lowu;
}
void find_ebcc(int n){
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i,-1);
}
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
AddEdge(b,a);
}
find_ebcc(n);
for(int i = 1; i <= n; i++){
for(int j = head[i]; j != -1; j = edges[j].next){
int v = edges[j].to;
if(ebccno[i] != ebccno[v]){
deg[ebccno[v]]++;
}
}
}
int leaf = 0;
for(int i = 1; i <= ebcc_cnt; i++){
if(deg[i] == 1){
leaf++;
}
}
printf("%d\n",(leaf+1)/2);
}
return 0;
}

  

POJ 3177——Redundant Paths——————【加边形成边双连通图】的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  3. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  4. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  5. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

  6. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  7. POJ 3177 Redundant Paths(强连通分量)

    题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条 ...

  8. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  9. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

随机推荐

  1. WC2019 冬眠记

    Day1 做高铁来广州 晚上开幕式,亮点在CCF的日常讲话.dzd有事换wh讲. 我们WC比赛的人数是最多的,性价比是最高的 然后掌声雷动 相信大家鼓掌是同意我的话 再次掌声雷动(雾 Day2-Day ...

  2. 浅识J2EE十三个规范

    前言 没有规矩不成方圆,学习J2EE,先来明白都有什么规范. 内容 1.JDBC(Java Database Connectivity)java数据库连接 a)为java开发人员提供了一个行业标准AP ...

  3. PHP 符号

    注解符号: // 单行注解 /*      */    多行注解 引号的使用 ’   ’ 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,PHP动态处理然后输出,一般 ...

  4. APIO 2012 派遣(可并堆)

    APIO 2012 派遣(可并堆) 给定一棵N个点的树和M,每个点有两个权值ai,bi,每次可以选择一个点x,然后在这个点的子树中选若干点(可以不选自己),使得这些点的\(\sum b_i<=M ...

  5. C#中抽象类与接口

    1抽象类 (1) 抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法 (2) 抽象类不能被实例化 (3) 抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法,就一定要把这个类声明 ...

  6. git 创建、切换和提交新分支

    查看本地分支 git branch 创建新的分支 git branch <newBranch> 切换分支 git checkout <branchName> 创建并切换分支 g ...

  7. Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)

    题目大意:每个同学可以指定一个人,然后构成一个有向图.1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出. 思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实 ...

  8. 2016"百度之星" - 资格赛(Astar Round1)C

    Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字典中删除所有前缀等于给定字 ...

  9. lintcode - 被围绕的区域

    class Solution { public: /* * @param board: board a 2D board containing 'X' and 'O' * @return: nothi ...

  10. 互联网开发-web文件上传性能问题

    1. 问题描述 文件大小 部署环境 平均上传速度 5M 外网 28s-36s 5M 公司局域内网 秒传,很快 2. 问题分析 在网上搜索“测速网”测试了一下公司外网的带宽情况: 上传带宽 = 1.04 ...