Under construction

Still being built. Things may change

Experiment

Interview

Time limit3sMemory limit128 MB

Summary
Orient each undecided corridor so the whole maze stays acyclic, following the smallest-numbered topological order of the fixed corridors.
Level

Medium4 of 10

Topics
Topological sort, Graph, Heap
Solved
No attempts yet

Problem

Witek and his friends are building a maze for ants. The maze has NN chambers numbered from 00 to N−1N-1, connected by one-way corridors. Once an ant is placed in a chamber, it keeps walking, at each step picking at random one of the outgoing corridors of its current chamber, and it stops only when it reaches a chamber with no outgoing corridor.

Some corridors already have a fixed direction; the rest are dug, but their direction has not been chosen yet. Your task is to choose a direction for every undecided corridor so that no ant can ever walk forever. In other words, once all directions are chosen, the whole maze (all corridors together) must contain no directed cycle.

Input

The first line contains three integers NN, AA, and BB (1≤N,A,B≤2000001 \le N, A, B \le 200000): the number of chambers, the number of corridors whose direction is already fixed, and the number of corridors whose direction is still undecided.

Each of the next AA lines contains two integers SS and KK, describing a fixed corridor that goes from chamber SS to chamber KK.

Each of the following BB lines contains two integers SS and KK, describing an undecided corridor that connects chambers SS and KK.

For every corridor, S≠KS \ne K. It is guaranteed that a valid orientation exists, which is equivalent to saying that the corridors with a fixed direction contain no directed cycle.

Output

Many orientations may keep every ant from walking forever, so to make the answer unique we ask for one specific valid orientation, defined as follows.

Look only at the corridors that already have a fixed direction. Because a valid orientation exists, these fixed corridors form a directed acyclic graph. Build a canonical ordering of the chambers with this process: counting each chamber's in-degree over the fixed corridors only, repeatedly take the smallest-numbered chamber whose current in-degree is 00, append it to the ordering, and remove its outgoing fixed corridors (decreasing the in-degree of each target chamber by 11). After this process every chamber has a distinct position.

Then, following the input order, print one line for each undecided corridor:

  • print 0 if SS comes before KK in the ordering (the corridor is directed from SS to KK),
  • print 1 otherwise (the corridor is directed from KK to SS).

Orienting every corridor from the earlier chamber to the later chamber in this ordering guarantees that no ant can walk forever.

Examples3

  1. Example 1

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

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

    Input
    2 1 1
    0 1
    1 0
    
    Expected output
    1