[USACO06JAN] 冗余路径 Redundant Paths
题目描述
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的点连起来。
ans = 度数为1的点数量/2 向上取整。
因惰于判重,特判之。
Code
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std; const int MAXN = + ;
const int MAXM = + ; struct Edge {
int nxt;
int to,from;
} l[MAXM<<]; int n,m;
int head[MAXN],cnt;
int low[MAXN],dfn[MAXN];
int index[MAXN],col[MAXN];
int tot,stamp,ans;
bool in[MAXN]; stack<int> S; inline void add(int x,int y) {
cnt++;
l[cnt].nxt = head[x];
l[cnt].to = y;
l[cnt].from = x;
head[x] = cnt;
return;
} void tarjan(int x,int from) {
low[x] = dfn[x] = ++stamp;
in[x] = true;
S.push(x);
for(int i = head[x];i;i = l[i].nxt) {
if(l[i].to == from) continue;
if(!dfn[l[i].to]) {
tarjan(l[i].to,x);
low[x] = min(low[x],low[l[i].to]);
} else if(in[l[i].to]) low[x] = min(low[x],dfn[l[i].to]);
}
if(dfn[x] == low[x]) {
tot++;
while(S.top() != x) {
col[S.top()] = tot;
in[S.top()] = false;
S.pop();
}
col[x] = tot;
in[x] = false;
S.pop();
}
return;
} int main() {
scanf("%d%d",&n,&m);
if(n == && m == ) {
puts("");
return ;
}
int x,y;
for(int i = ;i <= m;i++) {
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
for(int i = ;i <= n;i++) {
if(!dfn[i]) tarjan(i,);
}
for(int i = ;i <= *m;i+=) {
if(col[l[i].to] == col[l[i].from]) continue;
else index[col[l[i].to]]++,index[col[l[i].from]]++;
}
for(int i = ;i <= tot;i++) {
if(index[i] == ) ans++;
}
printf("%d\n",(ans+)/);
return ;
}
[USACO06JAN] 冗余路径 Redundant Paths的更多相关文章
- Luogu2860 [USACO06JAN]冗余路径Redundant Paths
Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...
- 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告
P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...
- 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths
P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...
- 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- luogu P2860 [USACO06JAN]冗余路径Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...
- 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- 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. ...
- 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解
题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...
- (精)题解 guP2860 [USACO06JAN]冗余路径Redundant Paths
(写题解不容易,来我的博客玩玩咯qwq~) 该题考察的知识点是边双连通分量 边双连通分量即一个无向图中,去掉一条边后仍互相连通的极大子图.(单独的一个点也可能是一个边双连通分量) 换言之,一个边双连通 ...
随机推荐
- [Other]面试复习笔记:线程与进程复习
基本概念 1. 进程的基本概念 线程(thread)是进程(processes)中某个单一顺序的控制流,也被称为轻量进程(lightweight processes).进程是表示资源分配的基本单位,又 ...
- 【Poj 1832】连环锁
连环锁 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1260 Accepted: 403 Description 许多 ...
- 【BJOI 2018】 求和
[题目链接] 点击打开链接 [算法] 预处理i^k的前缀和,对于每次询问,树上倍增即可 时间复杂度 : O(nk + mlog(n)) [代码] #include<bits/stdc++.h&g ...
- 33. Extjs中的tree节点的操作
转自:https://blog.csdn.net/masterShaw/article/details/51354351?utm_source=blogkpcl9 ext 树节点操作 tree ...
- GitHub中有关已建仓库及上传项目文件的删除
上传了项目,竟然发现找不到删除的地方,真是囧... 1. 已建仓库的删除 点击 settings,进入以下界面,点击箭头指向的按钮 进入以下界面 2. 某个文件的删除 直接点开文件,在右上角有个删除工 ...
- eclipse faild to creat the java Virtual Machine的解决办法
打开eclipse的时候突然出现了 faild to creat the java Virtual Machine 解决办法:打开解压后的Eclipse文件夹,找到eclipse.ini配置文件 打开 ...
- codehunter 「Adera 6」杯省选模拟赛 网络升级 【树形dp】
直接抄ppt好了--来自lyd 注意只用对根判断是否哟留下儿子 #include<iostream> #include<cstdio> using namespace std; ...
- PHP的包依赖管理工具Composer简介
composer是一个基于项目的依赖管理器,负责将php项目的所依赖的包和库安装在项目的目录中,默认不会不会安装任何数据到全局.他用于取代之前pear工具 1 安装Composer curl -sS ...
- [COCI2006-2007 Contest#3] BICIKLI
不难的一道题,就是码的时候出了点问题,看了其他巨佬的题解才发现问题所在... 题目大意: 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答 ...
- 树莓派zero_w设置中文(已成功)
树莓派默认是采用英文字库的,而且系统里没有预装中文字库,所以即使你在locale中改成中文,也不会显示中文,只会显示一堆方块.因此需要我们手动来安装中文字体. 好在有一个中文字体是免费开源使用的.ss ...