問題一覧 > 通常問題

No.1420 Census (Easy)

レベル : / AI推定難易度 : 1075 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 119
作問者 : 57tggx / テスター : logx
お気に入りにしたユーザー ProblemId : 5855 / 自分の提出
問題文最終更新日: 2021-05-19 23:50:39
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 285 (順位表) の他の問題:

Problem Statement

A kingdom has $N$ towns, numbered from $1$ to $N$. In town $i$ ( $i = 1, 2, \ldots, N$ ) there are $X_i$ houses, and for now all that is known is that $0 \leq X_i < 2^{30}$. The king of this country wants to know how many houses each town has.

One day, $M$ travelers visited this country. The $j$-th traveler ( $j = 1, 2, \ldots, M$ ) toured 2 of the towns in the kingdom: town $A_j$ and town $B_j$. When leaving the kingdom, each traveler $j$ reported the following 2 things to the king.

  • The numbers $A_j$ , $B_j$ of the towns they toured.
  • The value $Y_j$ obtained by taking the XOR of the numbers of houses in the 2 towns they toured. That is, $$Y_j = X_{A_j} \oplus X_{B_j}$$

The king decided to determine the number of houses in each town, $X_1, X_2, \ldots, X_N$, based on the reports of the $M$ travelers.

However, some of the travelers may have made false reports.

If there exists a set of values $X_1, X_2, \ldots, X_N$ consistent with all of the reports of the $M$ travelers, output one of them. Otherwise (when the travelers' reports are contradictory), output $-1$.

Input

$N$ $M$
$A_1$ $B_1$
$Y_1$
$A_2$ $B_2$
$Y_2$
$\vdots$
$A_M$ $B_M$
$Y_M$

Constraints:

  • All inputs are integers.
  • $1 \leq N \leq 10^5$
  • $1 \leq M \leq 10^5$
  • For each $j = 1, 2, \ldots, M$:
    • $1 \leq A_j < B_j \leq N$
    • $0 \leq Y_i < 2^{30}$

Output

If there is no contradiction, output, separated by newlines,

$X_1$
$X_2$
$\vdots$
$X_N$
Here, $0 \leq X_i < 2^{30}$ must hold for all $i = 1, 2, \ldots, N$. If there are multiple solutions, you may output any of them.

If there is a contradiction, output

-1

Sample

Sample 1
Input
3 3
1 2
3
2 3
5
1 3
6
Output
2
1
4

Suppose towns $1$ , $2$ , $3$ have $2$ , $1$ , $4$ houses, respectively.

Traveler $1$ visits towns $1$ , $2$ , and the XOR is $2 \oplus 1 = 3$ .

Traveler $2$ visits towns $2$ , $3$ , and the XOR is $1 \oplus 4 = 5$ .

Traveler $3$ visits towns $1$ , $3$ , and the XOR is $2 \oplus 4 = 6$ .

Therefore, $(X_1, X_2, X_3) = (2, 1, 4)$ satisfies the conditions. Other possibilities include $(X_1, X_2, X_3) = (0, 3, 6)$ , and you may output any of them.

Sample 2
Input
2 2
1 2
1
1 2
2
Output
-1

Travelers $1$ , $2$ visited the same towns yet reported different values, which is clearly a contradiction.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。