Problem E
Foreign Exchange
Input: standard input
Output: standard output
Time Limit: 1 second
Your non-profit organization (iCORE-international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. over the last few years, demand has sky-rocketed and now you need maintenance ance with your task.
The program your organization runs works as follows: All candidates are asked for their original location and the location they wocould like to go. the program works out only if every student has a suitable exchange partner. in other words, if a student wants to go from A to B, there must be another student who wants to go from B to. this was an easy task when there were only about 50 candidates , However now there are up to 500000 candidates!
Input
The input file contains multiple cases. each test case will consist of a line containing n-the number of candidates (1≤n ≤ 500000), followed by nlines representing the exchange information for each candidate. each of these lines will contain 2 integers, separated by a single space, representing the candidate's original location and the candidate's target location respectively. locations will be represented by nonnegative integer numbers. you may assume that no candidate will have his or her original location being the same as his or her target location as this wowould fall into the domestic exchange program. the input is terminated by a case where n = 0; this case shocould not be processed.
Output
For each test case, print "YES" on a single line if there is a way for the exchange program to work out, otherwise print "NO ".
Sample Input Output for Sample Input
10
1 2
2 1
3 4
4 3
100 200
200 100
57 2
2 57
1 2
2 1
10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
0
YES
NO
Question: There are n exchange students. Next, it means that each student can go from school A to school B .. Only one student ranges from school A to school B. Another student is from B to. Can be exchanged. That is to say, pair them .. Finally, determine whether all data can be exchanged.
Idea: n has 50 W. If a two-dimensional array is opened directly. Certainly not. Therefore, the vector is used to store the adjacent table. Then you can determine whether the pair can be paired. Read the code in detail.
# Include <stdio. h> # include <string. h> # include <iostream> # include <string> # include <vector> # include <algorithm> using namespace std; int n, I; int a, B; int judge; vector <int> map [500005]; int main () {while (~ Scanf ("% d", & n) {judge = 0; memset (map, 0, sizeof (map); for (I = 0; I <n; I ++) {scanf ("% d", & a, & B); if (find (map [B]. begin (), map [B]. end (), )! = Map [B]. end () {map [B]. erase (find (map [B]. begin (), map [B]. end (), a); judge --;} // if there is a pair. Delete the matched one from the link. Else {map [a]. push_back (B); judge ++;} // if no pair exists. The current link is saved .} If (judge) // if it is a pair of two. Finally, the judge is 0 printf ("NO \ n"); elseprintf ("YES \ n") ;}return 0 ;}