UVA 10763 (13.12.05)

来源:互联网 发布:反淘宝联盟yy是多少 编辑:程序博客网 时间:2024/05/21 09:05

Problem E
Foreign Exchange
Input:
standard input
Output: standard output
Time Limit: 1 second

Your non-profit organization (iCORE - internationalConfederation ofRevolverEnthusiasts) coordinates a very successful foreignstudent exchange program. Over the last few years, demand has sky-rocketed andnow you need assistance with your task.

The program your organization runs works asfollows: All candidates are asked for their original location and the locationthey would like to go to. The program works out only if every student has asuitable exchange partner. In other words, if a student wants to go from A toB, there must be another student who wants to go from B to A. This was an easytask when there were only about 50 candidates, however now there are up to500000 candidates!

Input

The input file contains multiplecases. Each test case will consist of a line containingn - the numberof candidates(1≤n≤500000), followed bynlines representing the exchange information for each candidate. Each of theselines will contain2 integers,separated by a single space, representing the candidate's original location andthe candidate's target location respectively. Locations will be represented bynonnegative integer numbers. You may assume that no candidate will have his orher original location being the same as his or her target location as thiswould fall into the domestic exchange program. The input is terminated by acase wheren = 0;this case should not be processed.

 

Output

For each testcase, print "YES" on asingle line if there is a way for the exchange program to work out, otherwiseprint"NO".

 

SampleInput                              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


Problem setter: Gilbert Lee, University of Alberta,Canada

题意: 就是如果有一个1 2数对, 那么一定要有一个对应的2 1数对~


做法: 两个数组, 各自存每对数组的第一个数字和第二个数字, 然后排序

不过看了网上也有这种做法的题解, 看到bug了, 例如三对数组, 1 2, 2 3, 3 1, 按题意是不行的, 但是用我这个方法是YES...


AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int num1[555555];int num2[555555];int main() {    int n;    while(scanf("%d", &n) != EOF && n) {        for(int i = 0; i < n; i++)            scanf("%d %d", &num1[i], &num2[i]);        sort(num1, num1+n);        sort(num2, num2+n);        int mark = 1;        for(int i = 0; i < n; i++) {            if(num1[i] != num2[i]) {                mark = 0;                break;            }        }        if(mark)            printf("YES\n");        else            printf("NO\n");    }    return 0;}

原创粉丝点击