(poj 3177) Redundant Paths
题目链接 :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的更多相关文章
- 【POJ 3177】Redundant Paths(边双连通分量)
求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...
- 【POJ 3177】Redundant Paths
http://poj.org/problem?id=3177 又写了一遍手动栈... 把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整. 为什么呢?因为1个度数为1的点的树需要多连0 ...
- (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 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- Games:取石子游戏(POJ 1067)
取石子游戏 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37662 Accepted: 12594 Descripti ...
- BFS 或 同余模定理(poj 1426)
题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple 非零倍数 啊. 英语弱到爆炸,理解不了题意... ...
- 并查集+关系的传递(poj 1182)
题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...
随机推荐
- .Net语言 APP开发平台——Smobiler学习日志:开发APP时,如何快速地实现屏幕自适应
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.属性介绍 设置控件在客户端屏幕可见并超出客户端屏幕时,是否自动调节高度以适应屏幕高 ...
- swift3.0 中NSNotification 的使用
swift3.0 有很大变化,其中之一就是NSNotification使用跟原来不一样,以前NSNotification name是String:3.0中定义了一个类型NSNotification.n ...
- Percona-Galera-Monitoring-Template监控模板说明
http://blog.chinaunix.net/uid-16844903-id-4054635.html 官网链接:http://www.percona.com/doc/percona-monit ...
- LVS 详解
http://zh.linuxvirtualserver.org/node/25 http://chrinux.blog.51cto.com/6466723/1198748 http://www.cn ...
- Type mytableview does not confirm to portocol UITableViewDataResource
继承UITableViewDataSource报上面这个总是,是重写协议时写错了 override func numberOfRowsInSection(section: Int) -> Int ...
- Linux vi编辑器
vim在内存缓冲区中处理数据 如果在启动vim时未指定文件名,或者这个文件不存在,vim会新开一段缓冲区来编辑. h 左移一个字符 j 下移一行 k 上移一行 l 右边移一个字符 PageDown(C ...
- struts2简单示例
今天写一个struts2的例子,目的是为了让大家明白struts2的基本流程,其实框架没有大家想象的那么难,说白了struts2的本质就是一个大的Servlet,即原本需要提交到Servlet处理的部 ...
- css笔记09:选择器优先级
1. (1) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 设计包含min函数的栈
stack<pair<int, int>> sta; void push(int x) { int min_i; if(sta.empty()) { min_i = x; } ...