题目链接 :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. Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学

    B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...

  2. Spring MVC Junit4 单元測试 JunitTest

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmVyb21lX3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  3. delphi 自动滚动到最底端scroll

    自动滚动到最底端scrollUses MSHTML;{$R *.dfm}var  ScrollPos: integer=0;procedure TForm1.Button1Click(Sender: ...

  4. android 关于提高第三方app的service优先级

    本博客仅仅要没有注明"转".那么均为原创,转贴请注明本博客链接链接 基本上大家都知道提高service优先级能够在非常大程度上让你的service免于由于内存不足而被kill,当然 ...

  5. Error creating bean with name &#39;menuController&#39;: Injection of autowired dependency……

    出现了一大串错误 Error creating bean with name 'userController': Injection of autowired dependencies failed. ...

  6. FlashBuilder精选插件

    1.Easy Explorer:打开在eclipse中选定文件所在的目录.这是一个非常不错的插件,有了它,你就可以随时跳到你指定文件的目录了.地址:http://sourceforge.net/pro ...

  7. LSM Tree解析

    引言 众所周知传统磁盘I/O是比较耗性能的,优化系统性能往往需要和磁盘I/O打交道,而磁盘I/O产生的时延主要由下面3个因素决定: 寻道时间(将磁盘臂移动到适当的柱面上所需要的时间,寻道时移动到相邻柱 ...

  8. Xquartz远程访问linux

    实验环境:mac 操作系统:         OS X 10.9.4 Mavericksmac IP                      192.168.1.106XQuartz:       ...

  9. MAC mysql安装及设置

    MAC下安装MYSQL有两种方式,一种为压缩包形式 另一种为.dmg文件安装包 . 首先先介绍压缩包形式的安装方法:   去MySql官网下MySQL classic版mysql-5.1.54-osx ...

  10. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...