Under construction

Still being built. Things may change

Asking for Money

Interview

Time limit0.5sMemory limit1024 MB

Summary
Given who each person asks for money, find every person whose two requests can both fail because those people already paid someone else.
Level

Medium5 of 10

Topics
Graph, DFS, BFS
Solved
No attempts yet

Problem

The International Commission for the Prevention of Cons is studying the possible effects of a pyramid scheme in a town. The scheme works as follows. Someone asks a person for $1 and tells them to ask two other people for $1 each, and to tell each of them to ask two others in the same way. The victim thinks they will earn $1. Since the number of people in the world is finite, not everyone can earn money this way, so this is a con.

The NN people in town are susceptible to the con. Each of them is willing to give $1 and later ask two other people for money. However, each person participates only once. If they are asked for money again, they will neither give it nor ask anyone. Once a person is asked for money, they give it immediately, but they may wait some time before asking the other two people. The con starts when someone from outside the town asks a person in the town for money. This triggers a sequence of requests within the town.

For example, the picture below shows a town with five people. An arrow from AA to BB means that AA asks BB for money.

In this example, BB can lose money. Consider the following scenario.

  1. Someone from outside the town asks AA for money.
  2. AA asks BB for money.
  3. AA asks CC for money.
  4. CC asks DD for money.
  5. BB asks CC for money.
  6. BB asks DD for money.

When BB asks CC and DD for money, they do not give it to BB, because they have already given money to someone else.

For each person in the town, you know whom they ask for money. Determine who in the town can lose money.

Input

The first line contains an integer NN (3≤N≤10003 \le N \le 1000), the number of people in the town. Each person is identified by a distinct integer from 11 to NN. For i=1,2,…,Ni = 1, 2, \dots, N, the ii-th of the next NN lines contains two integers XiX_i and YiY_i (1≤Xi,Yi≤N1 \le X_i, Y_i \le N, Xi,Yi≠iX_i, Y_i \ne i, and Xi≠YiX_i \ne Y_i), meaning that person ii asks persons XiX_i and YiY_i for money.

Output

Output a single line containing a string of length NN. The ii-th character is an uppercase Y if person ii can lose money, and an uppercase N otherwise.

Examples2

  1. Example 1

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

    Input
    4
    2 3
    3 4
    2 4
    2 3
    
    Expected output
    NYYY