A1145 | [COCI-2008_2009-contest1]#2 KRTICA
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Moles are tidy and hard-working animals. Our mole likes to keep its underground residence in utmost order, so that everyone living there knows where to find things.
To achieve this, the mole connected rooms with tunnels so that there is a single unique way to get from one room to any other room. The distance between two rooms is the number of halls passed on the way from one to the other.
Despite all the effort, some of the mole's guests are complaining that it takes too long to walk between certain pairs of rooms.
The mole decided to reconstruct her residence, closing one tunnel and opening a new one, so that the distance between the farthest two rooms is the smallest possible, but so that it is still possible to reach every room from every other room.
Write a program which determines the distance between the farthest two rooms after reconstruction, which tunnel to close and which to open.
To achieve this, the mole connected rooms with tunnels so that there is a single unique way to get from one room to any other room. The distance between two rooms is the number of halls passed on the way from one to the other.
Despite all the effort, some of the mole's guests are complaining that it takes too long to walk between certain pairs of rooms.
The mole decided to reconstruct her residence, closing one tunnel and opening a new one, so that the distance between the farthest two rooms is the smallest possible, but so that it is still possible to reach every room from every other room.
Write a program which determines the distance between the farthest two rooms after reconstruction, which tunnel to close and which to open.
输入格式
The first line contains an integer N (1 ≤ N ≤ 300 000), the number of rooms. The rooms are numbered 1 to N.
Each of the next N−1 lines contains two integers, the numbers of rooms a tunnel connects.
Each of the next N−1 lines contains two integers, the numbers of rooms a tunnel connects.
输出格式
Output on separate lines, in order:
• The distance between the two farthest rooms after reconstruction.
• A pair of integers representing a previously existing tunnel, which should be closed.
• A pair of integers, the rooms between which a new tunnel should be opened.
Note: The solution will not necessarily be unique. Output any reconstruction plan which achieves the smallest distance between the farthest two rooms.
• The distance between the two farthest rooms after reconstruction.
• A pair of integers representing a previously existing tunnel, which should be closed.
• A pair of integers, the rooms between which a new tunnel should be opened.
Note: The solution will not necessarily be unique. Output any reconstruction plan which achieves the smallest distance between the farthest two rooms.
输入输出样例
输入 #1
4 1 2 2 3 3 4
输出 #1
2 3 4 4 2
输入 #2
7 1 3 2 3 2 7 4 3 7 5 3 6
输出 #2
3 2 3 7 3
In test cases worth 40% of points, N will be less than 30.
In test cases worth 70% of points, N will be less than 3000.
Additionally, if the first line of your output is correct and the other two are missing or incorrect, you
will receive about 70% of the value of that test case.
In test cases worth 70% of points, N will be less than 3000.
Additionally, if the first line of your output is correct and the other two are missing or incorrect, you
will receive about 70% of the value of that test case.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted