「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 ...
随机推荐
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)
[Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...
- [hdu 2102]bfs+注意INF
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的……把INF改成I ...
- SynchronizationContext.Post方法 代替
http://www.codeproject.com/KB/threads/SynchronizationContext.aspx看吧,不好,就将就的看下我的吧,呵呵!(没有直接翻译,不过大概的思路相 ...
- PHP设计模式-代理模式
概念理解: 代理模式,是对简单处理程序(或指针)的增强,用于引用一个对象:这个指针被代理对象取代,代理对象位于客户端和真实程序之间,指针有一个可被多个目标利用的钩子. 参与者: client(参与者) ...
- PostgreSQL(Linux)安装、启动、停止、重启
If we don't already have PostgreSQL installed, we must install it. $ sudo apt-get install postgresql ...
- Web应用程序开发,基于Ajax技术的JavaScript树形控件
感谢http://www.cnblogs.com/dgrew/p/3181769.html#undefined 在Web应用程序开发领域,基于Ajax技术的JavaScript树形控件已经被广泛使用, ...
- java深入解析
具体内容安排如下: Java Collections Framework概览 对Java Collections Framework,以及Java语言特性做出基本介绍. Java ArrayList源 ...
- CSS3动画(重要)
CSS3 动画 CSS3,我们可以创建动画,它可以取代许多网页动画图像,Flash动画,和JAVAScripts. CSS3 @keyframes 规则 要创建CSS3动画,你将不得不了解@keyfr ...
- JAVA Eclipse 教程
http://www.runoob.com/eclipse/eclipse-tutorial.html
- python基础===pip安装模块失败
此情况只用于网络不畅的安装模块背景: 总出现红色的 Could not find a version that satisfies the requirement pymongo(from versi ...