CF739D.Recover a functional graph
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Functional graph is a directed graph in which all vertices have outdegree equal to 1. Loops are allowed.
Some vertices of a functional graph lay on a cycle. From the others we can come to a cycle by making a finite number of steps along the edges (we consider only finite functional graphs in this problem).
Let's compute two values for each vertex. precycle__i is the amount of edges we should pass to get to a vertex which is a part of some cycle (zero, if i itself lies on a cycle), cycle__i is the length of the cycle we get to.
You are given the information about these values for some functional graph. For each vertex you know the values precycle__i and cycle__i, however, instead of some values there can be the question mark. It means that these values are unknown.
Build any functional graph that suits the description or determine that there is no such graph.
函数图(Functional graph)是一种有向图,其中每个顶点的出度均为 1(允许自环)。
在函数图中,某些顶点位于某个环上;其余顶点则可通过沿有向边走有限步到达某个环(本题中仅考虑有限函数图)。
对每个顶点 i,我们定义两个值:
- precyclei 表示从顶点 i 出发、沿有向边行走,到达某个环上的顶点所需经过的边数(若 i 本身就在环上,则 precyclei=0);
- cyclei 表示从 i 出发最终到达的那个环的长度。
现给出某函数图中各顶点对应的 precyclei 和 cyclei 值(部分值可能为问号 ?),即这些值未知。
请构造任意一个满足该描述的函数图;若不存在这样的函数图,则判定其不可能。
输入格式
The first line contains single integer n (1 ≤ n ≤ 300) — the number of vertices in the graph.
Each of the next n lines contain two integers — precycle__i (0 ≤ precycle__i ≤ n - 1) and cycle__i (1 ≤ cycle__i ≤ n). There could be question marks instead of some of these values.
第一行包含一个整数 n(1≤n≤300)—— 图中顶点的数量。
接下来的 n 行,每行包含两个整数:precyclei(0≤precyclei≤n−1)和 cyclei(1≤cyclei≤n)。这些值中某些可能被问号(?)代替。
输出格式
In case there is no solution, print -1.
Otherwise, print n integers. i-th of them is the number of vertex to which the edge form the i-th vertex go.
The vertices should be in the same order as they go in input data.
If there are multiple solutions, print any of them.
若无解,请输出 -1。
否则,输出 n 个整数。其中第 i 个整数表示从第 i 个顶点出发的边所指向的顶点编号。
顶点的顺序应与输入数据中的顺序一致。
若存在多个解,输出任意一个即可。
输入输出样例
输入#1
3 0 3 0 3 ? ?
输出#1
2 3 1
输入#2
5 3 2 ? ? ? ? ? ? ? ?
输出#2
5 3 2 2 4
输入#3
8 ? 3 ? ? 0 2 0 2 0 3 0 3 0 3 3 3
输出#3
5 1 4 3 6 7 5 2
输入#4
1 ? ?
输出#4
1
输入#5
6 0 3 0 3 0 3 0 3 0 3 0 3
输出#5
2 3 1 5 6 4
输入#6
2 1 1 1 1
输出#6
-1
输入解题思路,AI测评打分。不知道怎么写?