Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)

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 22, because when you cut it, the size of the largest connected component of the remaining graph is 22 and it can't be smaller.

Codeforces Round #670 (Div. 2)  C. Link Cut Centroids  (dfs,树)

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

Codeforces Round #670 (Div. 2)  C. Link Cut Centroids  (dfs,树)

Both vertex 11 and vertex 22 are centroids because the size of the largest connected component is 33 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 tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.

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

Each of the next n−1n−1 lines contains two integers x,yx,y (1≤x,y≤n1≤x,y≤n). It means, that there exists an edge connecting vertices xx and yy.

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

It's guaranteed that the sum of nn for all test cases does not exceed 105105.

Output

For each test case, print two lines.

In the first line print two integers x1,y1x1,y1 (1≤x1,y1≤n1≤x1,y1≤n), which means you cut the edge between vertices x1x1 and y1y1. There should exist edge connecting vertices x1x1 and y1y1.

In the second line print two integers x2,y2x2,y2 (1≤x2,y2≤n1≤x2,y2≤n), which means you add the edge between vertices x2x2 and y2y2.

The graph after these two operations should be a tree.

If there are multiple solutions you can print any.

  • 题意:有一颗树,现在让你删去一条边再连一条边(两条边可以相同),使得操作后树的重心是唯一的.

  • 题解:这题如果知道树的重心的性质的话,其实就是一个结论就解决了.

    这里直接贴个链接吧.

    https://www.cnblogs.com/zjl192628928/p/11155816.html

    了解了之后,这题我们先去找树的重心,如果只有一个,那么我们可以随便删一条边再连回去,否则如果有两个重心,那么我们只要把其中一个重心的没有连另外一个重心的子节点删去,连到另外一个重心上面就可以了.

  • 代码:

    int t;
    int n;
    int a,b;
    vector<int> v[N];
    int son[N],mx[N];
    int mi; void dfs(int u,int fa){
    son[u]=1;
    for(auto w:v[u]){
    if(w==fa) continue;
    dfs(w,u);
    son[u]+=son[w];
    mx[u]=max(mx[u],son[w]);
    }
    mx[u]=max(mx[u],n-son[u]);
    mi=min(mi,mx[u]);
    } int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
    cin>>n;
    mi=INF;
    for(int i=1;i<n;++i){
    cin>>a>>b;
    v[a].pb(b);
    v[b].pb(a);
    }
    dfs(1,0);
    int cnt1=0;
    int cnt2=0;
    for(int i=1;i<=n;++i){
    if(mx[i]==mi){
    if(!cnt1) cnt1=i;
    else cnt2=i;
    }
    }
    if(cnt1 && !cnt2){
    cout<<a<<" "<<b<<endl;
    cout<<a<<" "<<b<<endl;
    }
    else{
    for(auto w:v[cnt1]){
    if(w!=cnt2){
    cout<<cnt1<<" "<<w<<endl;
    cout<<cnt2<<" "<<w<<endl;
    break;
    }
    }
    }
    for(int i=1;i<=n;++i){
    v[i].clear();
    son[i]=0;
    mx[i]=0;
    }
    } return 0;
    }
上一篇:Learning Core Data 1


下一篇:php ini_set('display_errors', $value)