[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 ...
随机推荐
- Akka源码分析-Cluster-Singleton
akka Cluster基本实现原理已经分析过,其实它就是在remote基础上添加了gossip协议,同步各个节点信息,使集群内各节点能够识别.在Cluster中可能会有一个特殊的节点,叫做单例节点. ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- 使用Oracle SQL Developer迁移MySQL至Oracle数据库
Oracle SQL Developer是Oracle官方出品的数据库管理工具.本文使用Oracle SQL Developer执行从MySQL迁移至Oracle数据库的操作. 2017年3月6日 操 ...
- ToolBar教程:AppCompatActivity下用toolbar当actionbar用
参考: https://developer.android.com/training/appbar/index.html 1,自定义toolbar主题 2,在布局xml中使用toolbar 3,在代码 ...
- ASP.NET Core&EF 笔记
首先创建Asp.net Core项目,然后通过 NuGet 安装 EntityFrameworkCore: Microsoft.EntityFrameworkCore.SqlServer Micros ...
- ES6 学习笔记 - 变量的解构赋值
变量的解构赋值 学习资料:ECMAScript 6 入门 数组的解构赋值 基本用法 可以从数组中提取值,按照对应位置,对变量赋值.这种写法属于"模式匹配". let [a, b, ...
- offset家族基本简介
Offset家族简介 offset这个单词本身是--偏移,补偿,位移的意思. js中有一套方便的获取元素尺寸的办法就是offset家族: offsetWidth和offsetHight 以及offse ...
- css3 动画 vs js 动画
之前被问到过,css3 动画与 js 动画孰优孰劣,脑袋的第一反应就是性能上肯定 css3 动画会好很多,但别人说不对,我就在想,不对?难道还有别的原因吗?答案是肯定的.先来看看二者实现动画的原理吧. ...
- 可以在一个html的文件当中读取另一个html文件的内容
1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 marg ...
- Android学习笔记(五)Android框架
一.技术结构图 注:开发者最需要关注的是第三层“Application Framework” 二.基于组件的应用程序开发 1)Activity 一个Activity就是一个界面,负责和用户交互. 2) ...