Foreign Exchange (exchange student swap location)

Source: Internet
Author: User
Tags integer numbers

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

17 18

19 20

0

Sample Output

YES

NO

Puzzle: Open two array S "I", T "I", and then the array data from small to large order, the loop if can be equal to one by one, then the explanation can be exchanged, output yes, otherwise as long as there is an unequal output no.

#include <iostream>
#include <algorithm>
using namespace Std;
int s[500000],t[500000]; //Note array size
int main ()
{
int n,i;
while (Cin>>n&&n)
{
for (i=0; i<n; i++)
cin>>s[i]>>t[i];
Sort (s,s+n); //Sort
Sort (t,t+n);
int f=0;
for (i=0; i<n; i++)
{
if (S[i]!=t[i])
{
f=1;
Break
}
}

if (f)
cout<< "NO" <<endl;
Else
cout<< "YES" <<endl;
}
return 0;

}

Foreign Exchange (exchange student swap location)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.