Codeforces1406 C - Link Cut Centroids(重心)

Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles.

A vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the remaining graph is the smallest possible.

For example, the centroid of the following tree is 2, because when you cut it, the size of the largest connected component of the remaining graph is 2 and it can’t be smaller.

However, in some trees, there might be more than one centroid, for example:

Both vertex 1 and vertex 2 are centroids because the size of the largest connected component is 3 after cutting each of them.

Now Fishing Prince has a tree. He should cut one edge of the tree (it means to remove the edge). After that, he should add one edge. The resulting graph after these two operations should be a tree. He can add the edge that he cut.

He wants the centroid of the resulting tree to be unique. Help him and find any possible way to make the operations. It can be proved, that at least one such way always exists.

Input
The input consists of multiple test cases. The first line contains an integer 𝑡 (1≤𝑡≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer 𝑛 (3≤𝑛≤105) — the number of vertices.

Each of the next 𝑛−1 lines contains two integers 𝑥,𝑦 (1≤𝑥,𝑦≤𝑛). It means, that there exists an edge connecting vertices 𝑥 and 𝑦.

It’s guaranteed that the given graph is a tree.

It’s guaranteed that the sum of 𝑛 for all test cases does not exceed 105.

Output
For each test case, print two lines.

In the first line print two integers 𝑥1,𝑦1 (1≤𝑥1,𝑦1≤𝑛), which means you cut the edge between vertices 𝑥1 and 𝑦1. There should exist edge connecting vertices 𝑥1 and 𝑦1.

In the second line print two integers 𝑥2,𝑦2 (1≤𝑥2,𝑦2≤𝑛), which means you add the edge between vertices 𝑥2 and 𝑦2.

The graph after these two operations should be a tree.

If there are multiple solutions you can print any.

Example
inputCopy
2
5
1 2
1 3
2 4
2 5
6
1 2
1 3
1 4
2 5
2 6
outputCopy
1 2
1 2
1 3
2 3
Note
Note that you can add the same edge that you cut.

In the first test case, after cutting and adding the same edge, the vertex 2 is still the only centroid.

In the second test case, the vertex 2 becomes the only centroid after cutting the edge between vertices 1 and 3 and adding the edge between vertices 2 and 3.

题意:
要求对树拆一条边删一条边,使得结果仍然是树,而且重心只有一个。

思路:
重心是指拆掉这个点最大块最小的点。
假设点x是重心,一个子树为y。
则如果拆掉x后子树y对应为最大块,说明除去子树y和x的其他节点一定不是重心。
这个很好想,其他点拆掉以后最大块的大小一定不会小于size[y]+1。

那么假设其他多个重心,那也一定在y子树内。
假设这个点是z,则size[z]<size[y],所以最大块一定不是子树,则有n-size[z]=size[y]。

也即 size[y]=(n+tmp)/2。tmp>0。
假设size[y]>n/2,将y拆掉的最大块比将x拆掉的最大块要小,则x不是重心,则矛盾。
故size[y]≤n/2,故tmp=0。

所以假设存在多个重心,则另外一个重心就是y,且满足size[y]=n/2(要求n是偶数)。

所以如果重心的最大块为n/2(n为偶数),则存在两个重心,且互为父子关系。你只要拆掉儿子的一个子树拼到父亲上就可以破坏这个n/2关系了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
 
using namespace std;
const int maxn = 1e5 + 7;
 
vector<int>G[maxn];
int siz[maxn],f[maxn],vis[maxn];
int n;
 
void dfs(int x,int fa) {
    siz[x] = 1;
    for(int i = 0;i < G[x].size();i++) {
        int v = G[x][i];
        if(v == fa) continue;
        dfs(v,x);
        siz[x] += siz[v];
        f[x] = max(f[x],siz[v]);
    }
    f[x] = max(f[x],n - siz[x]);
}
 
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            f[i] = 0;
            G[i].clear();
        }
        int X = 0,Y = 0;
        for(int i = 1;i < n;i++) {
            int x,y;scanf("%d%d",&x,&y);
            X = x;Y = y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        dfs(1,-1);
        int mi = 1e9;
        for(int i = 1;i <= n;i++) mi = min(mi,f[i]);
        int x = 0,y = 0;
        for(int i = 1;i <= n;i++) {
            if(f[i] == mi) {
                if(!x) x = i;
                else if(!y) y = i;
            }
        }
        if(x && y) {
            int pos = 0;
            for(int i = 0;i < G[x].size();i++) {
                int v = G[x][i];
                if(v == y) continue;
                pos = v;break;
            }
            printf("%d %d\n%d %d\n",x,pos,y,pos);
        } else {
            printf("%d %d\n%d %d\n",X,Y,X,Y);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值