CF611H.New Year and Forgotten Tree
NOI/NOI+/CTSC
通过率:0%
时间限制:7.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A tree is a connected undirected graph with n - 1 edges, where n denotes the number of vertices. Vertices are numbered 1 through n.
Limak is a little polar bear. His bear family prepares a New Year tree every year. One year ago their tree was more awesome than usually. Thus, they decided to prepare the same tree in the next year. Limak was responsible for remembering that tree.
It would be hard to remember a whole tree. Limak decided to describe it in his notebook instead. He took a pen and wrote n - 1 lines, each with two integers — indices of two vertices connected by an edge.
Now, the New Year is just around the corner and Limak is asked to reconstruct that tree. Of course, there is a problem. He was a very little bear a year ago, and he didn't know digits and the alphabet, so he just replaced each digit with a question mark — the only character he knew. That means, for any vertex index in his notes he knows only the number of digits in it. At least he knows there were no leading zeroes.
Limak doesn't want to disappoint everyone. Please, take his notes and reconstruct a New Year tree. Find any tree matching Limak's records and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1" (without the quotes).
树是一个包含 n 个顶点、n−1 条边的连通无向图。顶点编号为 1 到 n。
Limak 是一只小北极熊。他的熊家族每年都会准备一棵新年树。前一年,他们的树比往常更加炫酷,因此他们决定在来年再次准备同一棵树。Limak 负责记住这棵树。
要记住整棵树是困难的。Limak 决定改用笔记本记录它。他拿起笔,写了 n−1 行,每行包含两个整数——即一条边所连接的两个顶点的编号。
现在,新年即将到来,Limak 被要求根据笔记重新构造出那棵新年树。当然,这里有个问题:一年前他还非常小,还不认识数字和字母,因此他把笔记中所有数字都替换成了问号(?)——这是他唯一认识的字符。这意味着,对于笔记中出现的任意一个顶点编号,他只知道该编号的位数;至少他知道所有编号均无前导零。
Limak 不想让大家失望。请根据他的笔记,重构出一棵新年树。找出任意一棵符合 Limak 记录的树,并以任意顺序输出其所有边。也有可能 Limak 记错了,此时不存在满足条件的树——在这种情况下,请输出 -1(不带引号)。
输入格式
The first line contains a single integer n (2 ≤ n ≤ 200 000) — the number of vertices.
Each of the next n - 1 lines contains two space-separated non-empty strings, both consisting of questions marks only. No string has more characters than the number of digits in n.
第一行包含一个整数 n(2≤n≤200000)——顶点的数量。
接下来的 n−1 行中,每行包含两个由空格分隔的非空字符串,且每个字符串仅由问号(?)组成。任意字符串的长度均不超过 n 的十进制表示的位数。
输出格式
If there is no tree matching Limak's records, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's notes. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. You can print edges in any order.
如果不存在符合Limak记录的树,则输出唯一一行“-1”(不带引号)。
否则,请描述任意一棵符合Limak记录的树。输出 n−1 行,每行包含两个用空格分隔的整数——即该边所连接的两个顶点的编号。你可以以任意顺序输出各条边。
输入输出样例
输入#1
12 ? ? ? ? ? ? ? ?? ?? ? ?? ?? ? ?? ? ? ? ? ? ? ? ?
输出#1
3 1 1 6 9 1 2 10 1 7 8 1 1 4 1 10 5 1 10 11 12 1
输入#2
12 ?? ?? ? ? ? ? ? ?? ?? ? ?? ?? ? ?? ? ? ? ? ?? ?? ? ?
输出#2
-1
输入解题思路,AI测评打分。不知道怎么写?