「Baltic2015」Network
题目描述
原文
The government of Byteland has decided that it is time to connect their little country to the Internet, so that all citizens can participate in programming competitions and watch videos of cute cats. When it was time to build the network backbone of the country, they assigned the company Internet Optimists Inc. with connecting all the NNN computers of Byteland. The connections were made as direct links between pairs of computers in such a way that any pair of computers are connected by a sequence of links.
Byteland is not a rich country by any means, so to minimize costs the network topology was built in the form of a tree (i.e. there are exactly N−1N-1N−1 direct links between computers). Far too late, it was realised that this solution suffers from a serious drawback. If just a single link is broken, the computers of Byteland will be partitioned so that some computers cannot communicate with each other! To improve the reliability of Byteland's network, it was decided that it should at least tolerate if a single link is broken. Your task is to help Internet Optimists Inc. to improve the network in a cheapest way. Given the network topology of Byteland (i.e. which N−1N-1N−1 pairs of computers are connected by direct links), find the minimum number of links that need to be added so that the network will still be connected if any single link is broken.
译文
给定一颗无根树,求最少加多少条边使形成的图形任意删除一条边后都联通,边是无向的 输出最少加边数和任意一种加边方案。
注意不能加已出现的边
输入格式
The first line of input contains a positive integer NNN ( N≥3N \geq 3N≥3 ) , the number of computers in Byteland. For simplicity, all computers are numbered from 1 to NNN. Each of the following N−1N-1N−1 lines contains a pair of integers aaa and bbb ( 1≤a,b≤n,a≠b1\leq a,b \leq n,a \ne b1≤a,b≤n,a≠b ) that describes a direct link between computers aaa and bbb.
输出格式
In the first line of output your program should write an integer kkk, the minimal number of links that should be added to the network. In each of the following kkk lines your program should write a pair of integers aaa and bbb ( 1≤a,b≤n,a≠b1\leq a,b \leq n,a \ne b1≤a,b≤n,a≠b ) that denote the numbers of computers that should be connected by a link. The links can be written in any order. If there is more than one solution, your program should output any one of them.
样例
样例输入 1
6
1 2
2 3
2 4
5 4
6 4
样例输出 1
2
1 5
3 6
数据范围与提示
N<=5*10^5
题目要求就是加边之后任意一条树边都在至少一个环中。。
我也不知道怎么手玩了一个猜想就A了。。。。。
首先一个很贪心的想法是连边都是叶子之间的,这个很显然,因为不是叶子的边拉到两端都是叶子上可以覆盖更多的边。
有一种肯定能行的构造是,设node[i]为第i个被dfs到的叶子,那么对于i>=2,我们都连node[i]和node[i-1]一条边。
这样用的总边数是 叶子数-1 的。
但是我很快找出了一个反例,,,比如随便一个菊花图。
不过答案的下界我们是可以看出来的,因为每个叶子的加边度数至少为1,那么最优的加边数至少为 上取整(叶子数/2)。
那么是否对于每个图都可以构造出一种等于下界的答案呢???
至少数据告诉我是可以的hhhh
我们设叶子数为x,o=x/2。
那么对于i<=o,我们连node[i]和node[i+o]。
最后如果x是奇数的话,还要连node[x]到随便一个i<=o的node[i]。
然后我们考虑一下原图中的每条边,发现总会有另一条路径使得两个点可以到达,所以这样做总是正确的。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#define ll long long
#define maxn 500005
#define pb push_back
using namespace std;
vector<int> g[maxn];
int n,m,num,node[maxn]; void dfs(int x,int fa){
int to,siz=g[x].size();
if(siz==1) node[++num]=x; for(int i=0;i<siz;i++){
to=g[x][i];
if(to==fa) continue;
dfs(to,x);
}
} int main(){
int uu,vv;
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d",&uu,&vv);
g[uu].pb(vv);
g[vv].pb(uu);
} for(int i=1;i<=n;i++) if(g[i].size()>1){
dfs(i,i);
break;
} int o=num>>1;
printf("%d\n",num-o);
for(int i=1;i<=o;i++) printf("%d %d\n",node[i],node[i+o]);
if(num&1) printf("%d %d\n",node[o],node[num]); return 0;
}
「Baltic2015」Network的更多相关文章
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「CF1023F」Mobile Phone Network
「CF1023F」Mobile Phone Network 传送门 直接钦定那 \(k\) 条边在最小生成树中,然后把最小生成树树剖一下. 每条其它边的效果就是把该边端点路径上的边的权对该边边权取 \ ...
- 「CF555E」 Case of Computer Network
「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...
- Linux 小知识翻译 - 「NTP」
这周聊聊「NTP」. 上次,聊了「时区」,也就是时间相关的话题. NTP是「Network Time Protocol」的简称,是为了将网络中计算机的时钟同步到正确时间的协议. PC内部的时钟是相当不 ...
- iOS 9,为前端世界都带来了些什么?「译」 - 高棋的博客
2015 年 9 月,Apple 重磅发布了全新的 iPhone 6s/6s Plus.iPad Pro 与全新的操作系统 watchOS 2 与 tvOS 9(是的,这货居然是第 9 版),加上已经 ...
- 面试都在问的「微服务」「RPC」「服务治理」「下一代微服务」一文带你彻底搞懂!
❝ 文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) ❞ 单体式应用程序 与微服务相对的另一个概念是传统的「单体式应用程 ...
- 「MoreThanJava」计算机系统概述
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
随机推荐
- 【BZOJ 4034】[HAOI2015]树上操作 差分+dfs序+树状数组
我们只要看出来这道题 数组表示的含义就是 某个点到根节点路径权值和就行 那么我们可以把最终答案 看做 k*x+b x就是其深度 ,我们发现dfs序之后,修改一个点是差分一个区间,修改一个点的子树,可以 ...
- Angular 表单验证 基础篇
<div class="nav"> <h4>表单验证</h4> <form ng-app="myApp" name=& ...
- 怎么把centos虚拟机zip文件导入vm虚拟机中
执行以上三步就可以将一个压缩的centoszip文件导入到虚拟机中
- socket编程 ------ 建立 TCP 服务器和客户端流程(阻塞方式)
服务器端: 服务器端先创建一个socket,然后把这个socket绑定到端口上,接着让它向tcp/ip协议栈请求一个监听服务并创建一个accept队列来接受客户端请求. void creat_tcpS ...
- TCP之close_wait
TCP之close_wait 浏览:3697次 出处信息 /* * @author: ahuaxuan * @date: 2010-4-30 */ 查看各状态连接数: netstat -n | aw ...
- Spring学习--引用其他Bean , 内部Bean
引用其他Bean: 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能 , 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用. 在 Bean 的配置文 ...
- python并发进程
1 引言 2 创建进程 2.1 通过定义函数的方式创建进程 2.2 通过定义类的方式创建进程 3 Process中常用属性和方法 3.1 守护进程:daemon 3.2 进程终结于存活检查:termi ...
- 【poj1743-Musical Theme】不可重叠最长重复子串-后缀数组
http://poj.org/problem?id=1743 这题是一道后缀数组的经典例题:求不可重叠最长重复子串. 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲 ...
- ecma 2018, javascript spread syntax behaves like Object.assign
as the subject. It is only supported in Chrome version 60+, so, first check the version, or just use ...
- bzoj 1951 lucas crt 费马小定理
首先假设输入的是n,m 我们就是要求m^(Σ(c(n,i) i|n)) mod p 那么根据费马小定理,上式等于 m^(Σ(c(n,i) i|n) mod (p-1)) mod p 那么问题的关键就 ...