#abc352f. F - Estimate Order

F - Estimate Order

Score: 525525 points

问题陈述

NN 个人,编号为 11NN

在这些人之间举行了一场比赛,并且他们已经根据比赛结果进行了排名。关于他们的排名,给出了以下信息:

  • 每个人都有一个独特的排名。
  • 对于每个 1iM1 \leq i \leq M,如果第 AiA_i 个人排名第 xx,第 BiB_i 个人排名第 yy,那么 xy=Cix - y = C_i

给定的输入保证至少存在一种排名,它不会与给定的信息相矛盾。

回答 NN 个查询。第 ii 个查询的答案如下确定:

  • 如果可以唯一确定第 ii 个人的排名,返回该排名。否则,返回 1-1

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN people, numbered 11 to NN.

A competition was held among these NN people, and they were ranked accordingly. The following information is given about their ranks:

  • Each person has a unique rank.
  • For each 1iM1 \leq i \leq M, if person AiA_i is ranked xx-th and person BiB_i is ranked yy-th, then xy=Cix - y = C_i.

The given input guarantees that there is at least one possible ranking that does not contradict the given information.

Answer NN queries. The answer to the ii-th query is an integer determined as follows:

  • If the rank of person ii can be uniquely determined, return that rank. Otherwise, return 1-1.

Constraints

  • 2N162 \leq N \leq 16
  • 0MN(N1)20 \leq M \leq \frac{N(N - 1)}{2}
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • 1CiN11 \leq C_i \leq N - 1
  • (Ai,Bi)(Aj,Bj)(ij)(A_i, B_i) \neq (A_j, B_j) (i \neq j)
  • There is at least one possible ranking that does not contradict the given information.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

\vdots

AMA_M BMB_M CMC_M

Output

Print the answers to the 11-st, 22-nd, \ldots, NN-th queries in this order, separated by spaces.

Sample Input 1

5 2
2 3 3
5 4 3

Sample Output 1

3 -1 -1 -1 -1

Let XiX_i be the rank of person ii. Then, (X1,X2,X3,X4,X5)(X_1, X_2, X_3, X_4, X_5) could be (3,4,1,2,5)(3, 4, 1, 2, 5) or (3,5,2,1,4)(3, 5, 2, 1, 4).

Therefore, the answer to the 11-st query is 33, and the answers to the 22-nd, 33-rd, 44-th, and 55-th queries are 1-1.

Sample Input 2

3 0

Sample Output 2

-1 -1 -1

Sample Input 3

8 5
6 7 3
8 1 7
4 5 1
7 2 1
6 2 4

Sample Output 3

1 -1 -1 -1 -1 -1 -1 8