「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; }