The Continental Cowngress

Time limit1sMemory limit128 MB

Summary
Each of M cows casts yes/no votes on two distinct bills, and every cow must win at least one vote; decide for each bill whether it passes in all valid outcomes, fails in all, or varies.
Level

Hard8 of 10

Topics
Graph, DFS, Implementation, Greedy
Solved
No attempts yet

Problem

Unhappy with Farmer John's leadership, the cows have left the farm and founded the first Continental Cowngress. Governing by the principle that "every cow gets something she wants," they adopt the following voting scheme.

The MM cows in attendance (1≤M≤40001 \le M \le 4000) vote on NN legislative bills (1≤N≤10001 \le N \le 1000). In the end each bill is either passed or rejected.

Each cow casts a "yes" or "no" vote (written Y or N) on exactly two distinct bills BiB_i and CiC_i (1≤Bi,Ci≤N1 \le B_i, C_i \le N and Bi≠CiB_i \ne C_i). The two votes are VBiVB_i and VCiVC_i, each Y or N.

The bills must be passed or rejected so that every cow gets her way on at least one of her two votes. For example, if a cow votes "yes" on bill 1 and "no" on bill 2, then in any valid outcome bill 1 must pass or bill 2 must be rejected (or both).

Given every cow's two votes, determine the fate of each bill. If no outcome can satisfy every cow, the answer is IMPOSSIBLE. Otherwise, for each bill report:

  • Y — the bill passes in every valid outcome;
  • N — the bill is rejected in every valid outcome;
  • ? — some valid outcome passes the bill and some other valid outcome rejects it.

Input

  • Line 1: two space-separated integers NN and MM.
  • Lines 2 to M+1M+1: line i+1i+1 describes cow ii with four space-separated fields — an integer, a vote, another integer, and another vote: BiB_i, VBiVB_i, CiC_i, VCiVC_i.

Output

  • If at least one valid outcome exists, output a single line of NN characters; the ii-th character is Y if bill ii must pass, N if bill ii must be rejected, or ? if it cannot be determined.
  • If no outcome satisfies every cow, output the single line IMPOSSIBLE.

Examples1

  1. Example 1

    Input
    3 4
    1 Y 2 N
    1 N 2 N
    1 Y 3 Y
    1 Y 2 Y
    
    Expected output
    YN?