[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 ...
随机推荐
- LoadRunner监控Linux配置教程
LoadRunner监控Linux资源时弹出如下错误: Monitor name :UNIX Resources. Cannot initialize the monitoring on 192.16 ...
- JS中定时器的返回数值ID值
定时器会返回一个数字值id,可以由clearInterval(id)或clearTimeout(id)来实现对对应定时器的清除. setInterval()/setTimeout()BOM中的Wind ...
- 2017青岛网络赛1008 Chinese Zodiac
Chinese Zodiac Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
- elasticsearch 查询优化
首先对不必要的字段不做分词也就是不做索引,禁止内存交换 1.shard 一个Shard就是一个Lucene实例,是一个完整的搜索引擎. 分片数过多会导致检索时打开比较多的文件,多台服务器之间通讯成本加 ...
- IIS配置负载均衡
一.下载Nginx安装包 二.修改nginx.conf文件信息 如图: 三.重新加载Nginx (nginx -s reload) 启动Nginx: start nginx 停止Nginx:nginx ...
- [ SDOI 2006 ] 仓库管理员的烦恼
\(\\\) Description 有 \(n\) 种货物和 \(n\) 个仓库,开始第 \(i\) 个仓库里有 \(a_{ij}\) 个第 \(j\) 种货物. 现在要让每种货物都只放到一个仓库里 ...
- CF 334 div.2-D Moodular Arithmetic
思路: 易知k = 0的时候答案是pp-1,k = 1的时候答案是pp. 当k >= 2的时候,f(0) = 0,对于 1 <= n <= p - 1,如果f(n)确定,由题意可知f ...
- Firebug
Firebug是网页浏览器火狐下的一款开发类插件,它集HTML查看和编辑.JavaScript控制台.网络状态监视器于一体,是开发JavaScript.CSS.HTML和Ajax的得力助手.F12打开 ...
- (2)麻省理工:计算机科学和 Python 编程导论
语义描述了我们如何从那些表达式中推导出相关的含义,从而解决我们想解决的问题. 语法描述了如何将合法表达式组合在一起. 我们要选择什么样的编程语言? 1. 不管我们选什么,都有如下过程: 输入信 ...
- python与arduino串口通讯对接opencv实现智能物品分拣
2018-05-0118:53:50 先上图: 再来视频: http://v.youku.com/v_show/id_XMzU3NzAwNzMyNA==.html?spm=a2hzp.8244740. ...