10763 Foreign Exchange
Your Non-Profit organization (Icore-international Confederation of Revolver Enthusiasts) coordinates a very successful F Oreign Student exchange program. Over the last few years, demand have sky-rocketed and now your need assistance with your task. The program your organization runs works as Follows:all candidates is asked for their original location and the location They would like to go. The program works out only if every student have a suitable exchange partner. In the other words, if a student wants to go from a to B, there must is another student who wants to go from B to a. This is an easy task when the There were only about the candidates, however now there is up to 500000 candidates!
Input
The input file contains multiple cases. Each test case would consist of a line containing n–the number of candidates (1≤n≤500000), followed by n lines repres Enting The exchange information for each candidate. Each of these lines would contain 2 integers, separated by a single space, representing the candidate ' s original location A nd the candidate ' s target location respectively. Locations is represented by nonnegative integer numbers. Assume that no candidate'll has his or hers original location being the same as his or hers target location as th Is would fall to the domestic exchange program. The input is terminated by a case where n = 0; This case is should not being processed.
Output
For each test case, print ' YES ' in a single line if there is a a-line for the exchange program to work out, otherwise print ' NO '.
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
#include <iostream>#include<algorithm>using namespacestd;Const intmaxn=500005;intA[MAXN],B[MAXN];intMain () {intN; while(cin>>n&&N) {intk=1; for(intI=0; i<n;i++) {cin>>a[i]>>B[i]; } sort (A,a+N); Sort (b,b+N); for(intj=0; j<n;j++) { if(a[j]!=B[j]) {k=0; Break; } } if(k) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl; } return 0;}
View Code
17 18
19 20
0
Sample Output
YES
NO
Problem Solving Ideas: Let's first define two large enough arrays A and B, respectively, to hold the student's starting place and destination. Enter the number of cases you want, enter the destination and the starting place, and then sort the two arrays with the sort () function (the sort function is included in the header file algorithm). The two arrays are then compared, only if the two arrays have all the same elements to be exchanged (that is, the corresponding elements in A and B are the same), and finally the output of the title is required.
Program code:
ACM Exchange Student Problem