题目描述

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.

为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分离的路径,这样她们就有多一些选择.

每对草场之间已经有至少一条路径.给出所有R(F-1≤R≤10000)条双向路的描述,每条路连接了两个不同的草场,请计算最少的新建道路的数量, 路径由若干道路首尾相连而成.两条路径相互分离,是指两条路径没有一条重合的道路.但是,两条分离的路径上可以有一些相同的草场. 对于同一对草场之间,可能已经有两条不同的道路,你也可以在它们之间再建一条道路,作为另一条不同的道路.

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

7 7

1 2

2 3

3 4

2 5

4 5

5 6

5 7

输出样例#1:

2

bcc的模板题。用tarjan缩边双联通分量,然后就可以建出bcc树,答案就是(bcc树上的叶子节点个数+1)2\frac{(bcc树上的叶子节点个数+1)}{2}2(bcc树上的叶子节点个数+1)​。因为我们要将肯定将叶子节点两两链接起来最优。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<queue>
#define ll long long
#define N 5005
#define M 100005 using namespace std;
inline int Get() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;} int n,m;
int low[N],dfn[N],id; struct load {
int to,next;
}s[M<<1]; int h[N],cnt=1;
void add(int i,int j) {s[++cnt]=(load) {j,h[i]};h[i]=cnt;}
int st[N],top;
int bcc;
int bel[N];
void tarjan(int v,int fr) {
dfn[v]=low[v]=++id;
st[++top]=v;
for(int i=h[v];i;i=s[i].next) {
if((i^1)==fr) continue ;
int to=s[i].to;
if(!dfn[to]) {
tarjan(to,i);
low[v]=min(low[v],low[to]);
} else low[v]=min(low[v],dfn[to]);
}
if(low[v]==dfn[v]) {
bcc++;
while(1) {
int j=st[top--];
bel[j]=bcc;
if(j==v) break;
}
}
} int r[N];
int main() {
n=Get(),m=Get();
int a,b;
for(int i=1;i<=m;i++) {
a=Get(),b=Get();
add(a,b),add(b,a);
}
tarjan(1,0);
for(int v=1;v<=n;v++) {
for(int i=h[v];i;i=s[i].next) {
int to=s[i].to;
if(bel[v]!=bel[to]) r[bel[to]]++;
}
}
int ans=0;
for(int i=1;i<=bcc;i++)
if(r[i]==1) ans++;
cout<<(ans+1)/2;
return 0;
}

luogu P2860 [USACO06JAN]冗余路径Redundant Paths的更多相关文章

  1. 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...

  2. luogu P2860 [USACO06JAN]冗余路径Redundant Paths |Tarjan

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  3. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  4. 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告

    P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...

  5. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  6. P2860 [USACO06JAN]冗余路径Redundant Paths tarjan

    题目链接 https://www.luogu.org/problemnew/show/P2860 思路 缩点,之后就成了个树一般的东西了 然后(叶子节点+1)/2就是答案,好像贪心的样子,lmc好像讲 ...

  7. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  8. P2860 [USACO06JAN]冗余路径Redundant Paths

    题解: 首先要边双缩点这很显然 然后变成树上问题 发现dp,dfs好像不太对 考虑一下度数 发现只要在度数为1的点之间连边 但我好像不太会证明这个东西.. 网上也没有看到比较正确的证明方法和连边策略. ...

  9. 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths

    P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...

随机推荐

  1. java基础之继承(一)

    虽然说java中的面向对象的概念不多,但是具体的细节还是值得大家学习研究,java中的继承实际上就是子类拥有父类所有的内容(除私有信息外),并对其进行扩展.下面是我的笔记,主要包含以下一些内容点: 构 ...

  2. 使用css的-moz-element()把html元素当背景图片去

    background:-moz-element(id)定义了一个从任意HTML元件产生的值.此图像是实时的,这意味着如果更改了HTML元素,则会自动更新使用结果值的CSS属性.element() &l ...

  3. CentOS安装Subversion 1.9.*版本客户端

    安装yum仓库 以下以CentOS6为例,其他类似 # vim /etc/yum.repos.d/wandisco-svn.rep [WandiscoSVN] name=Wandisco SVN Re ...

  4. [算法]PHP随机合并数组并保持原排序

    场景 原有帖子列表A,现需在A中推广新业务B,则需要在A列表中1:1混合B的数据,随机混合,但需保持A和B两列表原来的数据排序.具体参考下面示例的效果. 原理 获知总共元素数量N: for循环N次,取 ...

  5. JavaScript实现iphone时钟

    看效果(欢迎各位同学推荐更好的gif制作软件) 请看代码 <!DOCTYPE html> <html lang="en"> <head> < ...

  6. SpringBoot之使用Scheduled做定时任务

    定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便. 一.@Scheduled注解介绍 这里先贴上@Schedu ...

  7. ssh隧道的妙用

    场景说明:A主机和B主机在同一个园区网,A主机可以出公网,B主机不可以出公网.渗透工程师已经拿下A主机控制权. 一.如何在外网访问到B主机 方案:ssh本地端口映射背景:当我们拿下A主机之后想和B主机 ...

  8. [PHP] 算法-请找出带环链表的环的入口结点的PHP实现

    给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...

  9. ES6学习之变量的解构赋值

    前言:什么是ES6?ECMAScript 6(简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.其中相比较于ES5新增了诸多的特性,并且ES6可转换为ES5的语法.- ...

  10. Python 简单的远程执行命令

    client端执行命令,server端返回命令结果 # server 端 import socket, subprocess sk = socket.socket() address=('127.0. ...