★★☆   输入文件: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. 使用particles.js实现网页背景粒子特效

    得知途径 B3log提供了两套博客系统,一个是用Java开发的,叫做Solo,我也是在网上搜索Java博客系统时发现了它,之后才了解了B3log:还有一个是用Go语言开发的,叫做Pipe.其中Solo ...

  2. 公司4:JrVue主题定制

    JrVue是我们基于element重新封装的一套组件库;  具体组件使用方法可以mnote->研发小组查看; 这里我们定制了一套主题色, 具体变动如下: 1.主题色变动: mfront有蓝.紫. ...

  3. 【原创】Maven安装和配置

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 前提 利用maven进行java项目或J2EE项目开发,要求电脑已配置java开发环境(JDK) 下载 下载地址:http://maven.a ...

  4. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

  5. Java多线程——线程的死锁

    Java多线程——线程的死锁 摘要:本文主要介绍了Java多线程中遇到的死锁问题. 部分内容来自以下博客: https://www.cnblogs.com/wy697495/p/9757982.htm ...

  6. C++学习笔记(三)之函数库

    1.标准库函数 begin end begin 返回数组首地址 end   返回数组尾地址 2.const 在声明变量时对变量限制为只读,不允许修改 const int i = 5; 单个const作 ...

  7. SQL Server的安装笔记

    SQL安装笔记 安装SQL Server 2008 打开SQL Server 2008中的setup.exe,显示SQL安装程序的对话框. 提示必须安装相关组件Microsoft.NET Framew ...

  8. npm换淘宝源 yarn换淘宝源

    查询初始的源 npm get registry > https://registry.npmjs.org/ 设置淘宝源 npm config set registry http://regist ...

  9. 表格 —— 一个单元格插入多个tags

    <st #st [columns]="columns" [data]="data" [bordered]='true'> <ng-templa ...

  10. ThinkPHP---TP功能类之联表查询

    [一]介绍 在原生的sql中使用join 语法进行数据的联表查询, 在ThinkPHP里支持联表查询操作,但是可以归纳成两种方式:table方法.join方法 (1)table方法:在TP中对应SQL ...