题目链接 :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. hdu1428漫步校园( 最短路+BFS(优先队列)+记忆化搜索(DFS))

    Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU校园呈方形布 ...

  2. jsp forward 动作标签

    forward 动作标签: <jsp:forward page="要转向的页面"> </jsp:forward> 或 <jsp:forward pag ...

  3. Android提高21篇之一:MediaPlayer

    本文介绍MediaPlayer的使用.MediaPlayer可以播放音频和视频,另外也可以通过VideoView来播放视频,虽然VideoView比MediaPlayer简单易用,但定制性不如用Med ...

  4. 日志管理 rsyslog服务浅析

    http://www.xiaomastack.com/2014/11/13/rsyslog/

  5. SparkStreamingTest.scala

    /** * Created by root on 9/8/15. */ import org.apache.spark._ import org.apache.spark.rdd.RDD import ...

  6. oracle 基本操作

    1. 开启oralce和监听#su - oracle$sqlplus / as sysdba>startup>exit$lsnrctl start$ps -ef|grep oracle 一 ...

  7. mysql导入数据库

     mysql -u root -p bbs < d:\bbs_2011-06-15 --default-character-set=gbk      mysqldump -uroot -p ta ...

  8. php引入公用部分html出现了一行空白(原创)

    在导入公用部分html(客服信息)时,莫名其妙出现了一行空白,样式,html均无问题 后来才发现是html多了一行空白 <div class="ad-module-item3 fn-m ...

  9. Linux 内核Coding Style整理

    转载:http://www.cnblogs.com/wang_yb/p/3532349.html 总结linux内核开发的coding style, 便于以后写代码时参考. 下面只是罗列一些规则, 具 ...

  10. 小白日记24:kali渗透测试之提权(四)--利用漏洞提权

    利用漏洞提权实例 前提:已渗透进一个XP或2003系统 一.实验目标漏洞:Ms11-080 补丁:Kb2592799 漏洞信息:https://technet.microsoft.com/librar ...