Under construction

Still being built. Things may change

The Problem Writer Does Not Work!

Time limit8sMemory limit512 MB

Summary
Given a directed graph with no cycles and stones on each vertex, decide whether the first player wins, the second player wins, or the game never ends under optimal play.
Level

Medium7 of 10

Topics
Game theory, Topological sort
Solved
No attempts yet

Problem

Dialogue between the statement writer (W), the mediator (M), and the problem author (P).

W: No way, this one is impossible.

M: What is?

W: Not "what"! How am I supposed to give this a natural problem setting?

M: Ha ha. Figuring that out is your job. You are the statement writer, right?

W: Passing the work off is one thing, but this is too much. First, the game rules are unnatural.

M: Are they? I think it is an ordinary stone-taking game.

W: Since when is there a common stone-taking game where players can add stones at will without limit?

M: Oh, you just said something clever.

W: Yes, at will... no, forget that! You should think about it too.

M: Well then. How about this: a stone is taken by the will of a child who inherited the will of a doctor who hanged himself?

W: That is not what needs thinking about! This is about the stone-taking game!

M: Is it not? But "tori" (taking) has only "tori" (bird) as a homophone, which makes it hard.

W: Stop the wordplay! I am asking you to think of a natural problem setting!

M: That is your job. I am only the middleman between the problem author and the statement writer.

W: Damn... why did the problem author give me such a troublesome problem?

M: A message from the problem author to you: "Hehehe. Suffer, statement writer, lol lol."

W: Damn that guy! Someday I will punch him!

M: My advice that a stone-taking game where stones keep growing is very hard to set up may have helped a little, though.

W: Damn, you too! Not someday, I will punch you now!

M: Ha ha ha. If you fail to finish your part before the deadline, I will write the statement myself.

W: Wait, you will write it?

M: The title will be "The Problem Writer Does Not Work!" and the body will print our conversation as it is.

W: Please stop! If a silly problem like that is published under my name, what will everyone say?

M: Ha ha. Someone who cannot meet a deadline deserves to be socially erased, right?

W: Damn! Then I will come up with a natural problem setting no matter what!

(Note: in the end, it was published exactly as written here.)

There are NN vertices in a directed graph with no cycles. Each vertex holds some stones. Two players play a game on this graph. The players take turns. On his turn, a player chooses a vertex vv and removes one or more stones from it. Then, for every vertex ww with an edge from vv to ww, the player may change the number of stones on ww freely (increase it by any amount, decrease it by any amount, or leave it unchanged). The game repeats, and the player who runs out of stones to take first loses.

If both players play optimally, which player wins: the first player or the second player? Or does the game never end?

Input

The first line contains integers NN (2≤N≤10002 \le N \le 1000) and MM (1≤M≤100001 \le M \le 10000), separated by a space. The graph has NN vertices and MM edges. Vertices are numbered from 1 to NN.

The next NN lines each contain an integer viv_i (1≤vi≤100001 \le v_i \le 10000). The integer viv_i on line i+1i+1 is the number of stones initially on vertex ii.

The next MM lines each contain integers aia_i and bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N), separated by a space. They mean there is an edge from vertex aia_i to vertex bib_i. There are no self-loops, and there is at most one edge from any vertex AA to any vertex BB.

You may assume the given graph has no cycles.

N M
v1
v2
.
.
.
vN
a1 b1
a2 b2
.
.
.
aM bM

Output

If the first player wins, print 1. If the second player wins, print 2. If the game never ends, print 0.

Examples3

  1. Example 1

    Input
    6 5
    7
    14
    5
    11
    2
    5
    1 2
    2 3
    3 4
    4 5
    5 6
    
    Expected output
    2
    
  2. Example 2

    Input
    5 7
    295
    127
    350
    982
    426
    1 5
    3 5
    4 2
    3 1
    3 4
    5 4
    3 2
    
    Expected output
    1
    
  3. Example 3

    Input
    8 7
    6
    1
    7
    2
    5
    2
    6
    3
    1 3
    6 3
    7 4
    4 2
    1 6
    5 4
    7 5
    
    Expected output
    2