当前位置: 首页 > news >正文

Tree Compass( Codeforces Round 934 (Div. 2) )

Tree Compass( Codeforces Round 934 (Div. 2) )

You are given a tree with n n n vertices numbered 1 , 2 , … , n 1, 2, \ldots, n 1,2,,n. Initially, all vertices are colored white.

You can perform the following two-step operation:

  1. Choose a vertex v v v ( 1 ≤ v ≤ n 1 \leq v \leq n 1vn) and a distance d d d ( 0 ≤ d ≤ n − 1 0 \leq d \leq n-1 0dn1).
  2. For all vertices u u u ( 1 ≤ u ≤ n 1 \leq u \leq n 1un) such that dist † ( u , v ) = d \text{dist}^\dagger(u,v)=d dist(u,v)=d, color u u u black.

Construct a sequence of operations to color all the nodes in the tree black using the minimum possible number of operations. It can be proven that it is always possible to do so using at most n n n operations.

† ^\dagger dist ( x , y ) \text{dist}(x, y) dist(x,y) denotes the number of edges on the (unique) simple path between vertices x x x and y y y on the tree.

Input

Each test contains multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 200 1 \leq t \leq 200 1t200) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 3 1 \le n \le 2 \cdot 10^3 1n2103) — the number of vertices of the tree.

The following n − 1 n - 1 n1 lines of each test case describe the edges of the tree. The i i i-th of these lines contains two integers u i u_i ui and v i v_i vi ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin, u i ≠ v i u_i \neq v_i ui=vi), the indices of the vertices connected by the i i i-th edge.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 3 2 \cdot 10^3 2103.

Output

For each test case, first output a single integer o p op op ( 1 ≤ o p ≤ n ) (1 \le op \le n) (1opn), the minimum number of operations needed to color all vertices of the tree black.

Then, output o p op op lines, each containing 2 2 2 integers. The i i i-th line should contain the values of v v v and d d d chosen for the i i i-th operation ( 1 ≤ v ≤ n 1 \le v \le n 1vn, 0 ≤ d ≤ n − 1 0 \le d \le n - 1 0dn1)

You must guarantee that at the end of o p op op operations, all vertices are colored black.

If there are multiple solutions, you may output any one of them.

Example

Input

4121 241 21 31 472 73 26 45 71 66 7

Output

1
1 0
2
1 1
2 1
2
1 1
2 1
3
6 1
7 1
2 1

Note

In the first test case, there is only one possible operation, and performing it gives us a valid answer.

In the second test case, the first operation colors vertex 2 2 2 black, and the second operation colors vertex 1 1 1 black. It can be shown that it is impossible to color both vertices black in one operation, so the minimum number of operations needed is 2 2 2. Another possible solution is to use the 2 2 2 operations: ( u , r ) = ( 1 , 0 ) (u, r) = (1, 0) (u,r)=(1,0) and ( u , r ) = ( 2 , 0 ) (u, r) = (2, 0) (u,r)=(2,0).

In the third test case, the first operation colors vertices 2 2 2, 3 3 3 and 4 4 4 black, and the second operation colors vertex 1 1 1 black. Again, it can be shown that it is impossible to color all vertices black in 1 1 1 operation, so the minimum number of operations needed is 2 2 2.

In the fourth test case, the first operation colors vertices 4 4 4, 1 1 1 and 7 7 7 black, the second operation colors vertices 2 2 2, 5 5 5 and 6 6 6 black while the third operation colors vertices 3 3 3 and 7 7 7 black. Notice that it is allowed to color vertex 7 7 7 black twice.

Thus, each node was marked at least once, with node 7 7 7 marked twice. It can be shown that it is impossible to color all vertices black in fewer than 3 3 3 moves.

题解进一步分析和拓展

这个问题的关键在于树的直径,即树中两个最远节点之间的路径长度。直径的长度和树的染色策略有很大关系。接下来我们详细分析如何利用直径的性质优化我们的染色操作,并提出最优的染色方案。

一、直径链的染色操作

考虑一条长度为 (d) 的链,链上的每个节点都需要染色,操作一次最多能染黑链上的 2 个点。我们从树的直径出发,考虑如何使用最少的操作将所有节点染黑。

1. 直径 (d) 为奇数
  • 当直径 (d) 是奇数时,我们可以选择直径的中点 (u),然后进行一系列操作,操作的顺序是从 (u) 出发,染色 (u) 和与其距离为 (0, 1, 2, …, d-1) 的节点。这些操作的形式可以是:

    [
    (u, 0), (u, 1), (u, 2), …, (u, d-1)
    ]

    这样,通过最多 ( \frac{d+1}{2} ) 次操作,所有节点都能被染黑,因为每次操作都会把 2 个点染黑。

2. 直径 (d) 为偶数
  • 当直径 (d) 为偶数时,我们面临的挑战是如何通过最少次数的操作,覆盖整棵树的所有节点。这里分为两种情况:

    • 当 (d \mod 4 = 0)(即直径长度为 4 的倍数):
      我们可以选择直径的中心边 ( (x, y) ),然后交替地进行以下操作:

      [
      (x, 1), (y, 1), (x, 3), (y, 3), …, (x, d/2 - 1), (y, d/2 - 1)
      ]

      这样只需要 ( \frac{d}{2} ) 次操作。

    • 当 (d \mod 4 = 2)(即直径长度为 2 或 6 的余数为 2):
      由于不能完全通过交替操作来染色,我们只能从中心出发,进行如下操作:

      [
      (x, 0), (x, 1), (x, 2), …, (x, d/2)
      ]

      这样需要 ( \frac{d}{2} + 1 ) 次操作。

二、整体思路
  1. 找直径

    • 通过两次 DFS 来找到树的直径。第一次 DFS 从任意节点出发,找到最远的节点 (A);第二次 DFS 从 (A) 开始,找到最远的节点 (B),则 (A) 到 (B) 的路径即为树的直径。
  2. 根据直径的长度选择操作策略

    • 如果直径 (d) 为奇数,从中点出发进行操作。
    • 如果直径 (d) 为偶数,分为两种情况:
      • 如果 (d \mod 4 = 0),通过中心边交替操作。
      • 如果 (d \mod 4 = 2),从中心出发逐渐染色。
  3. 输出操作结果

    • 输出最少的操作次数和每次操作的具体节点及距离。

三、时间复杂度分析

  • DFS 查找直径的时间复杂度是 (O(n)),每次 DFS 都需要遍历整个树的节点和边,最多遍历 (n-1) 条边。
  • 操作计算 的时间复杂度是 (O(1)),因为操作次数由直径的长度确定,而直径已经通过 DFS 计算出来。
  • 因此,每个测试用例的时间复杂度是 (O(n)),而给定 (T) 个测试用例,所有测试用例的总时间复杂度为 (O(T \cdot n))。题目保证了总节点数 (n) 不超过 2000,所以这是一个高效的解法。

四、代码实现

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define BoBoowen ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e3 + 10;int n;
vector<int> g[N];
int fa[N];
int started;
int ended;
int height;
vector<int> rode;void dfs1(int x, int f, int h)
{for (auto it : g[x]){if (it == f){continue;}dfs1(it, x, h + 1);}if (h > height){height = h;started = x;}
}void dfs2(int x, int f, int h)
{fa[x] = f;for (auto it : g[x]){if (it == f){continue;}dfs2(it, x, h + 1);}if (h > height){height = h;ended = x;}
}void cleared()
{for (int i = 0; i <= n; ++i){g[i].clear();fa[i] = 0;}height = 0;rode.clear();
}void solved()
{cin >> n;for (int i = 0; i < n - 1; ++i){int x, y;cin >> x >> y;g[x].push_back(y);g[y].push_back(x);}dfs1(1, 0, 1);// cout << started << endl;height = 0;dfs2(started, 0, 1);int now = ended;while (now != 0){rode.push_back(now);now = fa[now];}// cout << ended << endl;// for (auto it : rode)// {//     cout << it << ' ';// }// cout << endl;if (height % 2 == 1){cout << height / 2 + 1 << endl;for (int i = 0; i <= height / 2; ++i){cout << rode[height / 2] << ' ' << i << endl;}}else if (height % 4 == 0){cout << height / 2 << endl;for (int i = 1; i <= height / 2 - 1; i += 2){cout << rode[height / 2 - 1] << ' ' << i << endl;cout << rode[height / 2] << ' ' << i << endl;}}else{cout << height / 2 + 1 << endl;for (int i = 0; i <= height / 2; i++){cout << rode[height / 2 - 1] << ' ' << i << endl;}}cleared();
}signed main()
{BoBoowen;int T = 1;cin >> T;while (T--){solved();}
}

五、总结

  • 树的直径是这道题的核心,利用树的直径来优化染色操作,可以大幅减少操作次数。
  • 根据直径的长度,决定从中点出发进行染色或者采用交替操作,这样能够保证染色的最优性。
  • 时间复杂度为 (O(n)),对于每个测试用例能够在合理时间内求解出最少的染色操作次数。

相关文章:

Tree Compass( Codeforces Round 934 (Div. 2) )

Tree Compass&#xff08; Codeforces Round 934 (Div. 2) &#xff09; You are given a tree with n n n vertices numbered 1 , 2 , … , n 1, 2, \ldots, n 1,2,…,n. Initially, all vertices are colored white. You can perform the following two-step operation: …...

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.17 掩码数组:缺失值处理的优雅方案

2.17 掩码数组&#xff1a;缺失值处理的优雅方案 目录 #mermaid-svg-12vjJJbyudPnkYBO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-12vjJJbyudPnkYBO .error-icon{fill:#552222;}#mermaid-svg-12vjJJbyudPnkYBO…...

PHP 常用函数2025.02

PHP implode() 函数 语法 implode(separator,array) 参数描述separator可选。规定数组元素之间放置的内容。默认是 ""&#xff08;空字符串&#xff09;。array必需。要组合为字符串的数组。 技术细节 返回值&#xff1a;返回一个由数组元素组合成的字符串。PHP 版…...

react中如何获取dom元素

实现代码 const inputRef useRef(null) inputRef.current.focus()...

【C++】继承(下)

大家好&#xff0c;我是苏貝&#xff0c;本篇博客带大家了解C的继承&#xff08;下&#xff09;&#xff0c;如果你觉得我写的还不错的话&#xff0c;可以给我一个赞&#x1f44d;吗&#xff0c;感谢❤️ 目录 5.继承与友元6.继承与静态成员7.复杂的菱形继承及菱形虚拟继承8.继…...

C语言实现字符串排序:从代码到原理深度解析

在编程的世界里&#xff0c;字符串处理是一项基础且重要的技能。今天&#xff0c;我们通过分析一段C语言代码来深入了解如何对字符串进行排序。 一、代码呈现 #include <stdio.h> #include <string.h> int main() { char s[1001]; scanf("%s", s); int…...

Vue3的el-table-column下拉输入实时查询API数据选择的实现方法

由于本人对el-table-column有下拉输入选择的要求&#xff0c;根据网上搜索的资料及本人优化&#xff0c;推出我比较满意的方法&#xff0c;供各位读者参考使用。 效果图 el-table-column写法 <el-table-columnlabel"货品编号"align"center"prop"…...

【数据结构】_链表经典算法OJ:复杂链表的复制

目录 1. 题目链接及描述 2. 解题思路 3. 程序 1. 题目链接及描述 题目链接&#xff1a;138. 随机链表的复制 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给你一个长度为 n 的链表&#xff0c;每个节点包含一个额外增加的随机指针 random &#xff0c;…...

Vue 图片引用方式详解:静态资源与动态路径访问

目录 前言1. 引用 public/ 目录2. assets/ 目录3. 远程服务器4. Vue Router 动态访问5. 总结6. 扩展&#xff08;图片不显示&#xff09; 前言 &#x1f91f; 找工作&#xff0c;来万码优才&#xff1a;&#x1f449; #小程序://万码优才/r6rqmzDaXpYkJZF 在 Vue 开发中&#x…...

chatGPT写的网页版贪吃蛇小游戏

chatGPT写的网页版贪吃蛇小游戏 前言网页版贪吃蛇小游戏 前言 之前无聊&#xff0c;让ChatGPT写了一段基于html语言的贪吃蛇小游戏代码 网页版贪吃蛇小游戏 将以下内容复制到记事本&#xff0c;重命名为xxx.html即可打开浏览器游玩 这里是一个使用HTML、CSS和JavaScript编写…...

Python量化交易助手:xtquant的安装与应用

Python量化交易助手&#xff1a;xtquant的安装与应用 技术背景和应用场景 在量化交易领域&#xff0c;Python因其强大的库支持和灵活性成为了许多开发者的首选语言。其中&#xff0c;xtquant 是迅投官方开发的一个Python包&#xff0c;专门用于与miniqmt通信&#xff0c;实现…...

前缀和算法

文章目录 算法总览题目1371.每个元音包含偶数次的最长子字符串 算法总览 题目 1371.每个元音包含偶数次的最长子字符串 1371.每个元音包含偶数次的最长子字符串 参考博主的讲解 思路分析&#xff1a;就是得使用前缀和记录情况&#xff0c;dp[i][j]表示s[0] 到s[i] 中&…...

Qt常用控件 输入类控件

文章目录 1.QLineEdit1.1 常用属性1.2 常用信号1.3 例子1&#xff0c;录入用户信息1.4 例子2&#xff0c;正则验证手机号1.5 例子3&#xff0c;验证输入的密码1.6 例子4&#xff0c;显示密码 2. QTextEdit2.1 常用属性2.2 常用信号2.3 例子1&#xff0c;获取输入框的内容2.4 例…...

《最小阻力之路》关于愿景的理解和思考

一、愿景的形成机制 1. 愿景的三层来源 来源层级形成机制案例潜在偏差风险① 本能冲动层对快感/痛苦的即时反应"想暴富"源于缺钱焦虑易被短期情绪劫持② 社会镜像层内化外界标准&#xff08;家庭/社会/文化&#xff09;"必须考研"因家人期待混淆他人需求…...

Ubuntu 22.04系统安装部署Kubernetes v1.29.13集群

Ubuntu 22.04系统安装部署Kubernetes v1.29.13集群 简介Kubernetes 的工作流程概述Kubernetes v1.29.13 版本Ubuntu 22.04 系统安装部署 Kubernetes v1.29.13 集群 1 环境准备1.1 集群IP规划1.2 初始化步骤&#xff08;各个节点都需执行&#xff09;1.2.1 主机名与IP地址解析1.…...

虚幻基础17:动画层接口

能帮到你的话&#xff0c;就给个赞吧 &#x1f618; 文章目录 animation layer interface animation layer interface 动画层接口&#xff1a;动画图表的集。仅有名字。 添加到动画蓝图中&#xff0c;由动画蓝图实现动画图表。...

无人机PX4飞控 | PX4源码添加自定义uORB消息并保存到日志

PX4源码添加自定义uORB消息并保存到日志 0 前言 PX4的内部通信机制主要依赖于uORB&#xff08;Micro Object Request Broker&#xff09;&#xff0c;这是一种跨进程的通信机制&#xff0c;一种轻量级的中间件&#xff0c;用于在PX4飞控系统的各个模块之间进行高效的数据交换…...

HTMLCSS :下雪了

这段代码创建了一个动态的雪花飘落加载动画&#xff0c;通过 CSS 技术实现了雪花的下落和消失效果&#xff0c;为页面添加了视觉吸引力和动态感。 大家复制代码时&#xff0c;可能会因格式转换出现错乱&#xff0c;导致样式失效。建议先少量复制代码进行测试&#xff0c;若未能…...

如何处理 Typecho Joe 主题被抄袭或盗版的问题

在开源社区中&#xff0c;版权保护是一个非常重要的话题。如果你发现自己的主题&#xff08;如 Joe 主题&#xff09;被其他主题&#xff08;如子比主题&#xff09;抄袭或盗版&#xff0c;你可以采取以下措施来维护自己的权益。 一、确认侵权行为 在采取任何行动之前&#xf…...

利用Vue和javascript分别编写一个“Hello World”的定时更新

目录 一、利用Vue编写一个“Hello World”的定时更新&#xff08;1&#xff09;vue编码在Html文件中&#xff08;2&#xff09;vue编码在js文件中 二、利用javascript编写一个“Hello World”的定时更新 一、利用Vue编写一个“Hello World”的定时更新 &#xff08;1&#xff…...

SAC算法实战:用PyTorch实现自动驾驶控制(附完整代码)

SAC算法实战&#xff1a;用PyTorch构建自动驾驶控制系统 在自动驾驶技术快速发展的今天&#xff0c;强化学习已成为解决复杂决策问题的有力工具。而Soft Actor-Critic&#xff08;SAC&#xff09;算法凭借其在连续动作空间中的卓越表现&#xff0c;正在成为自动驾驶控制领域的新…...

梦幻动漫魔法工坊快速上手:无需代码,网页端直接生成动漫图像

梦幻动漫魔法工坊快速上手&#xff1a;无需代码&#xff0c;网页端直接生成动漫图像 你是否也曾幻想过&#xff0c;用几句话就能召唤出脑海中的梦幻场景&#xff1f;一个可爱的猫耳少女&#xff0c;在樱花树下回眸&#xff1b;或是奇幻的魔法森林里&#xff0c;精灵在月光下起…...

BERT 模型:自然语言处理的新篇章

BERT模型&#xff1a;自然语言处理的新篇章 在人工智能领域&#xff0c;自然语言处理&#xff08;NLP&#xff09;一直是研究的热点之一。2018年&#xff0c;谷歌推出的BERT模型彻底改变了NLP的发展方向&#xff0c;成为该领域的重要里程碑。BERT&#xff08;Bidirectional En…...

STEP3-VL-10B实战案例:科研论文截图→公式识别→LaTeX还原→语义解释生成

STEP3-VL-10B实战案例&#xff1a;科研论文截图→公式识别→LaTeX还原→语义解释生成 1. 引言&#xff1a;当科研遇上多模态AI 如果你经常需要阅读英文论文&#xff0c;特别是那些数学、物理、计算机科学领域的文章&#xff0c;一定遇到过这样的困扰&#xff1a;论文里密密麻…...

HG-ha/MTools行业实践:短视频工作室AI配音+自动字幕+封面图生成闭环

HG-ha/MTools行业实践&#xff1a;短视频工作室AI配音自动字幕封面图生成闭环 你是不是也遇到过这样的场景&#xff1f;作为短视频工作室的创作者&#xff0c;每天都要面对海量的视频素材。一条1分钟的视频&#xff0c;从剪辑、配音、加字幕到制作封面&#xff0c;前前后后可能…...

别再为GEO数据注释发愁了!三种方法(TXT/Soft/R包)保姆级代码实战

GEO数据注释实战指南&#xff1a;TXT/Soft/R包三种方法全解析 刚接触生物信息学的研究者常常会在GEO数据分析的第一步就卡壳——面对五花八门的注释文件格式&#xff0c;如何准确高效地将探针ID转换为基因Symbol&#xff1f;这个问题看似简单&#xff0c;实则暗藏玄机。我曾见过…...

springboot-vue+nodejs的农产品扶贫助农系统的开发与实现

目录技术栈选择系统架构设计核心功能模块开发阶段划分关键代码示例&#xff08;Spring Boot&#xff09;前端组件示例&#xff08;Vue&#xff09;注意事项项目技术支持源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作技术栈选择 Spring Bo…...

MacBook安装OpenClaw实录:M1芯片适配Qwen3-32B镜像的解决方案

MacBook安装OpenClaw实录&#xff1a;M1芯片适配Qwen3-32B镜像的解决方案 1. 为什么要在M1 MacBook上折腾OpenClaw&#xff1f; 作为一个长期使用MacBook Pro&#xff08;M1芯片&#xff09;的技术爱好者&#xff0c;我一直在寻找能够充分利用本地计算资源的AI工具。当我第一…...

从草图到文档:我用这5个Miro/PlantUML模板,高效搞定团队架构设计评审

从草图到文档&#xff1a;5个高效架构设计模板与团队协作实战指南 在敏捷开发环境中&#xff0c;架构设计往往陷入两难困境——既要快速响应需求变化&#xff0c;又要保证设计文档的准确性与可维护性。Tech Lead们经常面临这样的场景&#xff1a;在白板前与团队激情讨论出的架构…...

GHelper深度解析:华硕笔记本终极性能调校实战指南

GHelper深度解析&#xff1a;华硕笔记本终极性能调校实战指南 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops. Control tool for ROG Zephyrus G14, G15, G16, M16, Flow X13, Flow X16, TUF, Strix, Scar and other models 项目地址: h…...