CF1340D.Nastya and Time Machine
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Denis came to Nastya and discovered that she was not happy to see him... There is only one chance that she can become happy. Denis wants to buy all things that Nastya likes so she will certainly agree to talk to him.
The map of the city where they live has a lot of squares, some of which are connected by roads. There is exactly one way between each pair of squares which does not visit any vertex twice. It turns out that the graph of the city is a tree.
Denis is located at vertex 1 at the time 0. He wants to visit every vertex at least once and get back as soon as possible.
Denis can walk one road in 1 time. Unfortunately, the city is so large that it will take a very long time to visit all squares. Therefore, Denis took a desperate step. He pulled out his pocket time machine, which he constructed in his basement. With its help, Denis can change the time to any non-negative time, which is less than the current time.
But the time machine has one feature. If the hero finds himself in the same place and at the same time twice, there will be an explosion of universal proportions and Nastya will stay unhappy. Therefore, Denis asks you to find him a route using a time machine that he will get around all squares and will return to the first and at the same time the maximum time in which he visited any square will be minimal.
Formally, Denis's route can be represented as a sequence of pairs: v1,t1,v2,t2,v3,t3,…,vk,tk, where vi is number of square, and ti is time in which the boy is now.
The following conditions must be met:
- The route starts on square 1 at time 0, i.e. v1=1,t1=0 and ends on the square 1, i.e. vk=1.
- All transitions are divided into two types:
- Being in the square change the time: vi,ti→vi+1,ti+1:vi+1=vi,0≤ti+1<ti.
- Walk along one of the roads: vi,ti→vi+1,ti+1. Herewith, vi and vi+1 are connected by road, and ti+1=ti+1
- All pairs vi,ti must be different.
- All squares are among v1,v2,…,vk.
You need to find a route such that the maximum time in any square will be minimal, that is, the route for which max(t1,t2,…,tk) will be the minimum possible.
丹尼斯来到娜斯佳家,却发现她并不高兴见到他……目前只有一种可能让她开心:丹尼斯打算买下娜斯佳喜欢的所有东西,这样她就一定会答应和他交谈。
他们所居住城市的地图由许多广场构成,其中一些广场由道路相连。任意两个广场之间恰好存在一条不重复经过任何顶点的路径。换言之,该城市的图结构是一棵树。
在时刻 0,丹尼斯位于顶点 1。他希望至少访问每个顶点一次,并尽快返回起点。
丹尼斯沿一条道路行走耗时为 1。然而,城市规模实在太大,若仅靠步行遍历所有广场将耗费极长时间。因此,丹尼斯采取了一个孤注一掷的举措:他掏出了自己在地下室制造的便携式时间机器。借助该装置,丹尼斯可将当前时间回拨至任意一个小于当前时间的非负时刻。
但该时间机器有一个特殊限制:若英雄在同一时刻、同一地点出现两次,便会引发宇宙级大爆炸,娜斯佳也将永远无法开心。因此,丹尼斯请求你为他设计一条借助时间机器的路线,使其能遍历全部广场并最终返回起点 1,同时使他在任一广场所处的最大时刻值最小化。
形式上,丹尼斯的路线可表示为一系列二元组:v1,t1,v2,t2,v3,t3,…,vk,tk,其中 vi 表示所在广场编号,ti 表示此时的时间。
需满足以下条件:
- 路线起始于广场 1、时刻 0,即 v1=1, t1=0;且终点也为广场 1,即 vk=1。
- 所有转移分为两类:
- 原地改变时间:vi,ti→vi+1,ti+1,满足 vi+1=vi,且 0≤ti+1<ti;
- 沿道路行走一步:vi,ti→vi+1,ti+1,满足 vi 与 vi+1 由道路直接相连,且 ti+1=ti+1。
- 所有二元组 vi,ti 必须互不相同。
- 所有广场均出现在序列 v1,v2,…,vk 中。
你需要找出一条路线,使得其经过的所有时刻中的最大值最小化,即最小化 max(t1,t2,…,tk)。
输入格式
The first line contains a single integer n (1≤n≤105) — the number of squares in the city.
The next n−1 lines contain two integers u and v (1≤v,u≤n,u=v) - the numbers of the squares connected by the road.
It is guaranteed that the given graph is a tree.
第一行包含一个整数 n(1≤n≤105)—— 表示城市中广场的数量。
接下来的 n−1 行每行包含两个整数 u 和 v(1≤u,v≤n,且 u=v)—— 表示由道路相连的两个广场的编号。
保证所给图是一棵树。
输出格式
In the first line output the integer k (1≤k≤106) — the length of the path of Denis.
In the next k lines output pairs vi,ti — pairs that describe Denis's route (as in the statement).
All route requirements described in the statements must be met.
It is guaranteed that under given restrictions there is at least one route and an answer whose length does not exceed 106. If there are several possible answers, print any.
第一行输出整数 k(1≤k≤106)—— Denis 路径的长度。
接下来 k 行,每行输出一对 vi,ti,表示 Denis 的路径(格式如题目描述中所述)。
题目描述中所述的所有路径要求均须满足。
在给定约束条件下,保证至少存在一条路径,且其长度不超过 106。若存在多个可行答案,输出任意一个即可。
输入输出样例
输入#1
5 1 2 2 3 2 4 4 5
输出#1
13 1 0 2 1 3 2 3 1 2 2 4 3 4 1 5 2 5 1 4 2 2 3 2 0 1 1
输入解题思路,AI测评打分。不知道怎么写?