题目描述

原文

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的更多相关文章

  1. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  2. 「CF1023F」Mobile Phone Network

    「CF1023F」Mobile Phone Network 传送门 直接钦定那 \(k\) 条边在最小生成树中,然后把最小生成树树剖一下. 每条其它边的效果就是把该边端点路径上的边的权对该边边权取 \ ...

  3. 「CF555E」 Case of Computer Network

    「CF555E」 Case of Computer Network 传送门 又是给边定向的题目(马上想到欧拉回路) 然而这个题没有对度数的限制,你想歪了. 然后又开始想一个类似于匈牙利的算法:我先跑, ...

  4. Linux 小知识翻译 - 「NTP」

    这周聊聊「NTP」. 上次,聊了「时区」,也就是时间相关的话题. NTP是「Network Time Protocol」的简称,是为了将网络中计算机的时钟同步到正确时间的协议. PC内部的时钟是相当不 ...

  5. iOS 9,为前端世界都带来了些什么?「译」 - 高棋的博客

    2015 年 9 月,Apple 重磅发布了全新的 iPhone 6s/6s Plus.iPad Pro 与全新的操作系统 watchOS 2 与 tvOS 9(是的,这货居然是第 9 版),加上已经 ...

  6. 面试都在问的「微服务」「RPC」「服务治理」「下一代微服务」一文带你彻底搞懂!

    ❝ 文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) ❞ 单体式应用程序 与微服务相对的另一个概念是传统的「单体式应用程 ...

  7. 「MoreThanJava」计算机系统概述

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  8. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  9. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

随机推荐

  1. codeforces 1077D

    题目:https://codeforces.com/contest/1077/problem/D 题意:给你一个长度为n的串,你需要在里面找到出现次数最多的长度为k的子序列(子序列中元素可重复),求这 ...

  2. hive对有特殊值null的数据倾斜处理

    对有特殊值的数据倾斜处理 SET mapred.reduce.tasks=20;SET hive.map.aggr=TRUE;SET hive.groupby.skewindata=TRUE;SET ...

  3. 解读python小练习

    1.新建一个函数,判断是不是int 类型,并测试,不是抛出错误def adder(x, y):"""Return x + y if they are both integ ...

  4. lnmp重置mysql数据库root密码

    第一种方法:用军哥的一键修改LNMP环境下MYSQL数据库密码脚本 一键脚本肯定是非常方便.具体执行以下命令: wget http://soft.vpser.net/lnmp/ext/reset_my ...

  5. wiki1285

    2013-09-21 16:50 裸 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, ...

  6. bzoj 2120 线段树套平衡树

    先吐下槽,改了快一个小时,最后发现是SBT的delete写错了,顿时就有想死的心..... 首先对于这道题,我们应该先做一下他的小问题,bzoj1878,虽然和这道题几乎一点关系没有, 但是能给我们一 ...

  7. 在SDK中使用Ubuntu仿真器

    Ubuntu仿真器是开发过程中非常有用,尤其是在没有任何Ubuntu设备时.在将仿真器附加到SDK后,你便可以在上面运行程序,安装点击数据包,等等,类似在物理设备上的操作一样. 在这里,您可以了解如何 ...

  8. concurrent

    from concurrent.futures import ThreadPoolExecutor

  9. System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO

    A data processor supports the use of multiple memory models by computer programs. At a device extern ...

  10. [TJOI2007]segment

    题目描述 在一个 n*n 的平面上,在每一行中有一条线段,第 i 行的线段的左端点是(i, L(i)),右端点是(i, R(i)),其中 1 ≤ L(i) ≤ R(i) ≤ n. 你从(1, 1)点出 ...