★★☆   输入文件:rpaths.in   输出文件:rpaths.out   简单对比
时间限制:1 s  
内存限制:128 MB

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields
(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

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7

1 2

2 3

3 4

2 5

4 5

5 6

5 7

Sample Output

2

Hint

Explanation of the sample:

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.

思路

此题的图很良心,都是连通图;
所以。。。Tarjan缩点,ans=(新图中度为一的点的个数+1)/2;

代码实现

先试了个贪心;
 #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的更多相关文章

  1. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  2. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  3. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  4. [POJ3177]Redundant Paths(双联通)

    在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  5. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  6. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  7. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  8. POJ3177 Redundant Paths 双连通分量

    Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  9. Luogu2860 [USACO06JAN]冗余路径Redundant Paths

    Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...

随机推荐

  1. React实战之60s倒计时按钮(发送短信验证按钮)

    React实战之60s倒计时按钮——短信验证按钮 导入:(antd组件——Form表单) import { Button, Form, Input } from 'antd'; const FormI ...

  2. Androidstudio的安装与使用调试

    1安装与基本使用 1.1androidstudio的安装 1.到android-studio\bin文件夹里面,根据自己的电脑配置,打开studio.exe或者studio64.exe 2.按照向导默 ...

  3. 2017杭电多校第六场1008 Kirinriki

    传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. C# 相关概念

    解决方案 在磁盘上由 .sln 文件表示,是一个或多个相关项目的容器. 例如,如果为 Python 应用程序编写 C++ 扩展,该 C++ 项目可以驻留在同一解决方案中. 解决方案还可以包含 Web ...

  5. poi获取word批注

    package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  6. Git——github高级

    分支管理 分支不是越多越好,只求一个稳定的分支,即master不要轻易去更改 对应master要有一个开发者分支,保证mater分支的稳定性 所有的功能都在开发者分支上进行 在所有功能开发后新建发布分 ...

  7. java设计模式之单例模式总结

    面试手写单例模式(通用版)

  8. vue+vux+es6+webpack移动端常用配置步骤

    1.创建项目(vue项目的流程就不多讲了)2.cnpm install vux --save3.在build/webpack.base.conf.js配置:const vuxLoader = requ ...

  9. 【Linux】CentOS安装Jenkins

    sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm -- ...

  10. os下,vs code 自动编译ts

    nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...