「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 ...
随机推荐
- nfs 和samba
NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...
- Educational Codeforces Round 11 A
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- HDU 2126 01背包(求方案数)
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数
[Lydsy1704月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 577 Solved: 201[Submit][Status][Di ...
- VC++使用CImage在内存中Bmp转换Jpeg图片
之前写了一篇<VC++使用CImage在内存中Jpeg转换Bmp图片>,通过CImage实现了在内存中Jpeg转Bmp. 既然Jpeg能转Bmp,那CImage也支持Bmp转Jpeg,与上 ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- vivo面试经验4(linux基本操作,最基本,必须得会!!)
操作linux通过xshell进行连接: 基本操作介绍 1.shutdown -h 关机 shutdown -r 重启 2.mkdir aaa 新建目录aaa rmdir aaa 删除目录aaa 3. ...
- bzoj3223 文艺平衡树 codevs3303 翻转区间
splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...
- 用 letsencrypt 生成 SSL 证书
letsencrypt 生成 SSL 证书 事先配置好访问域名解析 在nginx 对应虚拟主机添加一个验证区域: 配置 nginx server { listen 80; ... location ~ ...
- windows启动redis服务
参考:https://www.cnblogs.com/M-LittleBird/p/5902850.html 使用python的pip install redis以后还需要下载安装redis安装文件才 ...