Hooligan

Time limit1sMemory limit128 MB

Summary
Given partial results of a round-robin tournament where each pair plays M times, decide whether team 0 can finish alone in first place.
Level

Hard8 of 10

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

Problem

Soccer is the American English word for the sport that British English calls football — the most popular sport in Latin America (and in the world). Hooligan is sometimes used to describe an aggressive, troublemaking soccer fan.

In Linearonia, a soccer tournament is under way. Ranking works as follows: for each game the winner earns 22 points and the loser earns 00 points; in case of a tie, each team earns 11 point. The champion is the team with the most points. Every pair of distinct teams plays against each other exactly the same number of times, called the matching number MM.

You support one team — your dream team, numbered 00 — and you wonder whether it can still become the champion. You are given the number of teams, the matching number, and the results of some games already played. Decide whether, after all remaining games are played, your dream team can end up as the sole champion, with strictly more points than every other team.

Input

The input contains several test cases. Each test case consists of one or more lines. The first line has three integers NN, MM and GG separated by single spaces: the number of teams (2≤N≤402 \le N \le 40), the matching number (1≤M≤41 \le M \le 4), and the number of games already played (1≤G1 \le G). Your dream team is team 00; the other teams are numbered 1,2,…,N−11, 2, \ldots, N-1.

Each of the next GG lines describes one game already played. The line contains an integer II, a character CC and an integer JJ separated by single spaces (I≠JI \ne J and 0≤I,J≤N−10 \le I, J \le N-1). The character CC is < if team II lost to team JJ, or = if the game was a tie.

The last test case is followed by a line containing three zeros (0 0 0) separated by single spaces, which must not be processed.

Output

For each test case, print a single line with one character: uppercase Y if your dream team can be the sole champion, or uppercase N otherwise.

Examples3

  1. Example 1

    Input
    4 2 6
    0 < 3
    3 = 2
    2 < 0
    1 < 0
    2 = 0
    3 < 0
    4 1 5
    2 = 0
    0 < 1
    1 = 3
    2 < 1
    0 < 3
    4 2 5
    2 = 0
    0 < 1
    1 = 3
    2 < 1
    0 < 3
    2 1 1
    1 < 0
    4 1 1
    0 < 1
    4 1 2
    0 < 1
    0 < 2
    0 0 0
    
    Expected output
    Y
    N
    Y
    Y
    Y
    N
    
  2. Example 2

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

    Input
    2 1 1
    0 < 1
    0 0 0
    
    Expected output
    N