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.

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

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

Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)的更多相关文章

  1. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  2. Codeforces Round #339 Div.2 A - Link/Cut Tree

    第一次正式参加常规赛想想有些小激动的呢 然后第一题就被hack了 心痛 _(:зゝ∠)_ tle点在于越界 因此结束循环条件从乘变为除 done //等等 这题没过总评 让我静静........ // ...

  3. Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)

    1406A. Subset Mex https://codeforces.com/contest/1406/problem/A Example input 4 6 0 2 1 5 0 1 3 0 1 ...

  4. Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)

    题目链接 题目大意 给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q ...

  5. Codeforces Round #670 (Div. 2) B. Maximum Product (暴力)

    题意:有一长度为\(n\)的序列,求其中任意五个元素乘积的最大值. 题解:先排序,然后乘积能是正数就搞正数,模拟一下就好了. 代码: int t; ll n; ll a[N]; int main() ...

  6. Codeforces Round #670 (Div. 2) A. Subset Mex (贪心)

    题意:给你一长度为\(n\)的序列,将其分为两个集合,求两个集合中未出现的最小元素的最大值, 题解:用桶存一下每个元素的个数,两次枚举\([1,100]\),找出两个最小值即可. 代码: int t; ...

  7. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  8. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  9. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

随机推荐

  1. vim 手动添加脚本头部信息

    vim /root/.vimrc 8,1 全部 set autoindent set tabstop=5 set shiftwidth=4 function AddTitle() call setli ...

  2. TCP/IP协议栈在Linux内核中的运行时序分析

    网络程序设计调研报告 TCP/IP协议栈在Linux内核中的运行时序分析 姓名:柴浩宇 学号:SA20225105 班级:软设1班 2021年1月 调研要求 在深入理解Linux内核任务调度(中断处理 ...

  3. misc刷题

    前言:听说misc打得好,头发多不了 kali自带的字典: cd /usr/share/wordlists/ 字典网站:http://contest-2010.korelogic.com/wordli ...

  4. 在recover database时,如何决定该从哪一个SCN开始恢复

    使用备份恢复的方法搭建DG库,还原数据文件后,打开数据库时报错 SQL> ALTER DATABASE OPEN READ ONLY; ALTER DATABASE OPEN READ ONLY ...

  5. eCATT使用前的配置

    如果想在SAP中使用eCATT,必须做一下相关的配置才行,下面简单介绍这几步:1.SM30,输入表T000,然后点击维护,或者是进入事物SCC4,进入对应的clint属性编辑视图下,将CATT and ...

  6. 基于 WebRTC 实现自定义编码分辨率发送

    2020年如果问什么技术领域最火?毫无疑问:音视频.2020年远程办公和在线教育的强势发展,都离不开音视频的身影,视频会议.在线教学.娱乐直播等都是音视频的典型应用场景. 更加丰富的使用场景更需要我们 ...

  7. Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节

    目录 Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节 优先队列的默认用法-从小到大排序 对String类用优先队列从大到小排序 通过自定义比较器对自定义的类进行从小到大 ...

  8. javascript通过递归改子节点数据-用于层级深度未知的树形结构

    最近在做这么个需求:树形结构,层级深度未知,一旦某个节点的状态是置灰的话,其所有子节点都要置灰. 方案一(数据库有值):如果数据库里置灰节点的所有子节点,值也都是"置灰",那后台取 ...

  9. Java实现QQ邮件发送客户端

    目录 一.前言:QQ邮件发送程序 二.封装SMTP操作 三.实现多线程接收 四.QQ邮件客户端界面设计 1.连接按钮 2.发送按钮 五.QQ邮件发送效果演示 六.总结 一.前言:QQ邮件发送程序 在上 ...

  10. 解析MySQL中存储时间日期类型的选择问题

    解析MySQL中存储时间日期类型的选择问题_Mysql_脚本之家 https://www.jb51.net/article/125715.htm 一般应用中,我们用timestamp,datetime ...