A wildlife tracking program tags migratory birds with radio transmitters and logs which wetland each bird was sighted at during a season. A bird can be sighted at more than one wetland, and each sighting record carries a flag indicating whether that wetland is marked as the bird's declared primary (breeding) site. If a bird was sighted at only one wetland all season, that wetland is automatically its primary wetland, even if the flag on that single record says it is not. If a bird was sighted at more than one wetland, exactly one of its sighting records is flagged as primary, and that wetland is the answer for that bird.
Given the season's full sighting log, report the primary wetland for every distinct tagged bird.
The first line contains a single integer n, the number of sighting records.
Each of the next n lines contains three space-separated values bird_id wetland_id flag, where bird_id and wetland_id are positive integers and flag is either Y (this record is flagged as the primary wetland) or N (it is not).
Print one line per distinct bird, sorted by bird_id in ascending order, containing bird_id and its primary wetland_id, separated by a single space.
Y or NY.Y or N; either way that wetland is its primary wetland.Example 1
Input
5 101 1 N 101 2 Y 102 1 Y 103 3 N 104 2 N
Expected
101 2 102 1 103 3 104 2
Explanation
Bird 101 has two sighting records — wetland 1 (unflagged) and wetland 2 (flagged Y) — so its primary wetland is 2. Bird 102 has a single flagged record at wetland 1, so its primary wetland is 1. Bird 103 has only one sighting record at all, wetland 3, and even though that record's flag is N, a lone sighting is automatically the primary wetland, so the answer is 3. Bird 104 likewise has a single record at wetland 2, so 2 is its primary wetland regardless of the flag.
Example 2
Input
3 201 5 Y 201 6 N 202 7 N
Expected
201 5 202 7
Explanation
Bird 201 has two records, and the one flagged Y is at wetland 5, so 5 is its primary wetland. Bird 202 has only one record, at wetland 7; being its only sighting, wetland 7 is automatically its primary wetland.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →