A mountaineering club tracks climbing partnerships and gear endorsements. Partnerships are mutual: whenever climber a and climber b are recorded as partners, each is considered a partner of the other, and no climber is ever listed as their own partner. Separately, an endorsement record states that a specific climber endorses a specific piece of gear; a climber may endorse several different items, and no climber-gear pair carries any special meaning beyond "this climber endorses this gear."
Given the full partnership list, the full endorsement list, and the id of one target climber, find every distinct gear id that at least one of the target climber's partners endorses, except for any gear id the target climber already endorses themself. If the target climber has no recorded partners, there is nothing to recommend.
The first line contains three integers p, l, and x -- the number of partnership records, the number of endorsement records, and the target climber's id, respectively. Each of the next p lines contains two integers a b, meaning climbers a and b are partners (a != b). Each of the next l lines contains two integers u g, meaning climber u endorses gear item g.
Print the recommended gear ids, each on its own line, in strictly increasing order and with no duplicates. If there are no recommendations, print nothing.
Example 1
Input
3 4 1 1 2 1 3 2 4 1 88 2 23 3 24 4 56
Expected
23 24
Explanation
Climber 1's partners are climbers 2 and 3 (from the first two partnership records; the third record, 2-4, does not involve climber 1). Climber 1 already endorses gear 88. Climber 2 endorses gear 23 and climber 3 endorses gear 24, neither of which climber 1 already endorses, so both are recommended, printed in increasing order: 23 then 24.
Example 2
Input
2 5 5 5 6 5 7 5 10 6 10 6 11 7 11 7 12
Expected
11 12
Explanation
Climber 5's partners are 6 and 7. Climber 5 already endorses gear 10. Partner 6 endorses gear 10 (already owned by climber 5, so it is excluded) and gear 11. Partner 7 endorses gear 11 (already counted once, so it stays a single entry) and gear 12. The distinct not-already-owned gear ids among the partners' endorsements are 11 and 12.
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 →