题目链接 :http://poj.org/problem?id=3177

Description

In order to get from one of the F ( <= F <= ,) grazing fields (which are numbered ..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- <= R <= ,) 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 : Two space-separated integers: F and R Lines ..R+: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output Line : A single integer that is the number of new paths that must be built.
Sample Input Sample Output Hint Explanation of the sample: One visualization of the paths is: +---+---+
| |
| |
+---+---+
/
/
/
+
Building new paths from to and from to satisfies the conditions. +---+---+
: | |
: | |
+---+---+
/ :
/ :
/ :
+ - - - -
Check some of the routes:
– : –> and –> –> –>
– : –> –> –> and –> –> –>
– : –> –> and –> –> –>
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个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。

现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。

分析:利用tarjan算法进行缩点,算出每个缩点的出入度,所要增加的边为  添加边数=(树中度为1的节点数+1)/2;

#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stack>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define mod 2147493647
#define N 30100
struct node
{
int id,to,next;
}s[N];
int a[N],deg[N];
int low[N],dfn[N],st[N];
int belong[N],vis[N];
int t,k,l,num;
void init()
{
met(vis,);
met(dfn,);
met(a,-);
met(deg,);
t=k=l=num=;
}
void add(int u,int v,int f)
{
s[l].id=f;
s[l].to=v;
s[l].next=a[u];
a[u]=l++;
}
void tarjan(int u,int f)
{
low[u]=dfn[u]=++t;
st[k++]=u;
vis[u]=;
for(int i=a[u];i!=-;i=s[i].next)
{
int v=s[i].to;
if(f==s[i].id)
continue;
if(!dfn[v])
{
tarjan(v,s[i].id);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
}
}
int v;
if(dfn[u]==low[u])
{
++num;
do{
v=st[--k];
vis[v]=;
belong[v]=num;///每个点属于的圈
}while(v!=u);
}
}
void solve(int n)
{
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-);
}
for(int u=;u<=n;u++)
{
for(int j=a[u];j!=-;j=s[j].next)
{
int v=s[j].to;
if(belong[v]==belong[u])
continue;
deg[belong[v]]++;///统计每个缩点里有几个点
//deg[belong[u]]++;
}
}
int ans=;
for(int i=;i<=num;i++)
{
if(deg[i]==)///添加边数=(树中度为1的节点数+1)/2
ans++;
}
printf("%d\n",(ans+)/);
}
int main()
{
int n,m,e,f;
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
for(int i=;i<=m;i++)
{
scanf("%d %d",&e,&f);
add(e,f,i);
add(f,e,i);
}
solve(n);
}
return ;
}

(poj 3177) Redundant Paths的更多相关文章

  1. 【POJ 3177】Redundant Paths(边双连通分量)

    求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...

  2. 【POJ 3177】Redundant Paths

    http://poj.org/problem?id=3177 又写了一遍手动栈... 把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整. 为什么呢?因为1个度数为1的点的树需要多连0 ...

  3. (Problem 15)Lattice paths

    Starting in the top left corner of a 22 grid, and only being able to move to the right and down, the ...

  4. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  5. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  6. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  7. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  8. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  9. 并查集+关系的传递(poj 1182)

    题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...

随机推荐

  1. Html页中使用OCX控件

    原文:http://blog.csdn.net/mouse8166/article/details/5515657 最近准备开发一个b/s架构的应用程序需要用到activeX控件,web服务器尚未进入 ...

  2. [AngularJS] Using $anchorScroll

    If you're in a scenario where you want to disable the auto scrolling, but you want to control the sc ...

  3. python 学习(一)

    python的基础看完了之后,有点像简化并提供了一定优化后的java基础,看java多了的人看python还是比较别扭的.看完别人对于java和python的对比,我只能感慨一句,还有什么是java办 ...

  4. phonegap platform add ios 出错的问题

    在Mac上新安装的Phonegap,版本3.3-0.18.0. 本来创建项目后按教程: http://docs.phonegap.com/en/edge/guide_platforms_ios_ind ...

  5. 《C++ 并发编程》- 第1章 你好,C++的并发世界

    <C++ 并发编程>- 第1章 你好,C++的并发世界 转载自并发编程网 – ifeve.com 本文是<C++ 并发编程>的第一章,感谢人民邮电出版社授权并发编程网发表此文, ...

  6. json对象,使用 “ . ”获取值是,不能使用变量作为属性名。

    var he={'aa':"aa",'bb':'bb'}; var chun={'cc':"aa",'dd':'mm'}; c=he.aa; n=chun.c; ...

  7. PhpCMS标签:专题模块special标签

    专题模块 专题模块PC标签调用说明 模块名:special 模块提供的可用操作 操作名 说明 lists 专题列表 content_list 专题信息列表 hits 专题信息点击排序 下面对所有的操作 ...

  8. NetBeans 安装Android 开发环境

    基本的开发环境都是Eclipse + Android SDK + Android plugin for Eclipse NetBeans下开发Android的所需要的基本条件:NetBeans(包含J ...

  9. 我的第一款Cocos2d 游戏小狗快跑基本完成

  10. 笔记——ES5 Array

    ES5里引入了一些新的数组方法.这些方法可以分为两组: 迭代方法和项的定位. 兼容性:chrome,firefox,safari3,及ie8以上都支持 1. every 查询数组中的每一项是否匹配某个 ...