[COGS311] Redundant Paths
★★☆ 输入文件:rpaths.in
输出文件:rpaths.out
简单对比
时间限制:1 s
内存限制:128 MB
Description
(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 description 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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
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.
思路
代码实现
#include<cstdio>
const int maxn=5e3+;
const int maxm=2e4+;
int f,r,ans;
int a,b;
int d[maxn];
int main(){
freopen("rpaths.in","r",stdin);
freopen("rpaths.out","w",stdout);
scanf("%d%d",&f,&r);
for(int i=;i<=r;i++){
scanf("%d%d",&a,&b);
++d[a],++d[b];
}
for(int i=;i<=f;i++) if(d[i]<) ans++;
ans=ans+>>;
printf("%d\n",ans);
return ;
}
6/11
然后,正解;
#include<cstdio>
const int maxn=5e3+;
const int maxm=2e4+;
inline int min_(int x,int y){return x<y?x:y;}
int f,r,ans;
int a,b;
int e[maxm][];
int h[maxn],hs=,et[maxm],en[maxm];
void add(int u,int v){
++hs,et[hs]=v,en[hs]=h[u],h[u]=hs;
++hs,et[hs]=u,en[hs]=h[v],h[v]=hs;
}
int q[maxn],top;
int dfn[maxn],dfs,low[maxn];
int d[maxn],t[maxn],ts;
bool v[maxm];
void tarjan(int k){
dfn[k]=low[k]=++dfs;
q[++top]=k;
for(int i=h[k];i;i=en[i])
if(!v[i]){
v[i]=v[i^]=;
if(dfn[et[i]]) low[k]=min_(low[k],dfn[et[i]]);
else tarjan(et[i]),low[k]=min_(low[k],low[et[i]]);
}
if(!t[k]){
++ts;
while(low[k]<=dfn[q[top]]) t[q[top--]]=ts;
}
}
int main(){
freopen("rpaths.in","r",stdin);
freopen("rpaths.out","w",stdout);
scanf("%d%d",&f,&r);
for(int i=;i<=r;i++){
scanf("%d%d",&a,&b);
e[i][]=a,e[i][]=b;
add(a,b);
}
for(int i=;i<=f;i++) if(!dfn[i]) tarjan(i);
for(int i=;i<=r;i++) if(t[e[i][]]!=t[e[i][]]) ++d[t[e[i][]]],++d[t[e[i][]]];
for(int i=;i<=ts;i++) if(d[i]==) ans++;
printf("%d\n",ans+>>);
return ;
}
[COGS311] Redundant Paths的更多相关文章
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- POJ3177 Redundant Paths 双连通分量
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- Luogu2860 [USACO06JAN]冗余路径Redundant Paths
Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...
随机推荐
- winMTR的使用
WinMTR下载链接:http://pan.baidu.com/share/link?shareid=236531&uk=1126982975 WinMTR 使用方法及软件介绍: WinMTR ...
- redis 缓存应用
第1章 部署与安装 wget http://download.redis.io/releases/redis-3.2.10.tar.gz tar xf redis-3.2.10.tar.gz cd r ...
- sql server 行转列 要注意的问题 pivot
select * from ( select mvqr.VoteQuestionId,mvqr.AnswerSolution from JY_MemberVoteQuestionRef as ...
- Meta标签 h5
一 PC端meta标签 1 页面关键词 <meta name="keywords" content="your tags"> 2 页面描述 < ...
- self和super的区别
(1)self调用自己方法,super调用父类方法 (2)self是类,super是预编译指令 (3)[self class]和[super class]输出是一样的 ①当使用 self 调用方法时, ...
- Android Studio 入门 Hello World
Android Studio 入门 Hello World Gavin要加油 1.5k 6月22日 发布 推荐 1 推荐 收藏 17 收藏,2.1k 浏览 引言 前两天开始学习android开发,本来 ...
- dutacm.club_1085_Water Problem_(矩阵快速幂)
1085: Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Othe ...
- Mybatis学习总结四(关联查询)
一.一对一查询 实例:查询所有订单信息,关联查询下单用户信息. Method1:使用resultType,定义订单信息po类,此po类中包括了订单信息和用户信息. public class Order ...
- find命令查找和替换
find命令查找和替换 语法: find -name '要查找的文件名' | xargs perl -pi -e 's|被替换的字符串|替换后的字符串|g' #查找替换当前目录下包含字符串并进行替换 ...
- CF 429B B.Working out (四角dp)
题意: 两个人一个从左上角一个从左下角分别开始走分别走向右下角和右上角,(矩阵每个格子有数)问到达终点后可以得到的最大数是多少,并且条件是他们两个相遇的时候那个点的数不能算 思路: 首先这道题如果暴力 ...