UVa 10763 Foreign Exchange

Source: Internet
Author: User

10763-foreign Exchange

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1704

Your Non-Profit organization (icore-internationalconfederation of revolver enthusiasts) coordinates a very successful fo Reign Student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.

The program your organization runs works as Follows:all candidates are asked for their original location and the location They would like to go. The program works for only if every student has a suitable exchange partner. In the other words, the 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 a easy task when there were only about candidates, however now there are up to500000

Input

The input file contains multiple cases. Each test case would consist of a line containingn-the number of candidates (1≤n≤500000) followed Byn lines representing The exchange information for each candidate. Each of these lines'll contain2 integers, separated by a, representing the candidate ' s original location D The candidate ' s target location respectively. Locations is represented by nonnegative integer numbers. You could assume that no candidate would have his or her original location being the same as he or her target location as th Is would fall to the domestic exchange program. The input is terminated by a case wheren = 0; This case should is processed.

Output

For each test case, print ' YES ' on a single line if there are a way for the exchange program to work out, otherwise print "N O ".

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

I actually went to the multimap to think ...

In fact, just a sequence of the line AH (┬_┬)

Complete code:

/*0.162s*/
    
#include <cstdio>  
#include <algorithm>  
using namespace std;  
const int MAXN = 500010;  
    
int LOC[MAXN], res[maxn], n, I;  
    
BOOL Solve ()  
{  
    sort (loc, loc + N);  
    Sort (res, res + N);  
    for (i = 0; i < n; ++i)  
        if (Loc[i]!= res[i]) return false;  
    return true;  
}  
    
int main ()  
{while  
    (scanf ("%d", &n), N)  
    {for  
        (i = 0; i < n; ++i)  
            scanf ("%d%d", &loc [i], &res[i]);  
        Puts (Solve ()? "YES": "NO");  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.