Under construction

Still being built. Things may change

Spy 2

Interview

Time limit2sMemory limit1024 MB

Summary
Given each member's spy status and interview testimonies, decide whether the constraints are consistent and output a spy or non-spy label for every member.
Level

Medium5 of 10

Topics
Graph, DFS, Implementation
Solved
No attempts yet

Problem

JOI has N members of parliament, numbered from 1 to N. As the minister of JOI, you are trying to find the spies among them. For each member i (1 ≦ i ≦ N), you have the following information.

  • If Ti = 1, member i is a spy.
  • If Ti = 2, member i is not a spy.
  • If Ti = 3, it is unknown whether member i is a spy.

You also conducted interviews and obtained M more pieces of information. The j-th interview result (1 ≦ j ≦ M) says that member Aj (1 ≦ Aj ≦ N) testified that "member Bj (1 ≦ Bj ≦ N) is a spy, and member Cj (1 ≦ Cj ≦ N) is not a spy."

If member Aj is a spy, the testimony in the j-th interview is false. That is, if Aj is a spy, at least one of "Bj is a spy" and "Cj is not a spy" is false. If Aj is not a spy, the testimony may or may not be true.

Given the information about each member and the interview results, determine whether the N + M pieces of information contradict each other. If they are consistent, find for each member whether they are a spy. If several answers match all N + M pieces of information, you may output any of them.

Input

The input is given from standard input in the following format.

N M
T1 T2 … TN
A1 B1 C1
A2 B2 C2
:
AM BM CM

Output

If the information is contradictory, print -1 on a single line.

Otherwise, print N lines. On line i (1 ≦ i ≦ N), print 1 if member i is a spy, and 2 if member i is not a spy. If several answers match all N + M pieces of information, you may print any of them.

Constraints

  • 1 ≦ N ≦ 300 000.
  • 1 ≦ M ≦ 300 000.
  • 1 ≦ Ti ≦ 3 (1 ≦ i ≦ N).
  • 1 ≦ Aj ≦ N (1 ≦ j ≦ M).
  • 1 ≦ Bj ≦ N (1 ≦ j ≦ M).
  • 1 ≦ Cj ≦ N (1 ≦ j ≦ M).
  • Aj ≠ Bj (1 ≦ j ≦ M).
  • Aj ≠ Cj (1 ≦ j ≦ M).
  • Bj ≠ Cj (1 ≦ j ≦ M).

Examples3

  1. Example 1

    Input
    4 1
    1 3 2 3
    1 2 3
    
    Expected output
    1
    2
    2
    1
    
  2. Example 2

    Input
    4 2
    2 1 3 1
    4 3 1
    2 4 3
    
    Expected output
    -1
    
  3. Example 3

    Input
    3 2
    1 2 2
    2 1 3
    2 3 1
    
    Expected output
    1
    2
    2