UVA 10763 (13.12.05)

原创 2013年12月05日 21:43:41

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

Your non-profit organization (iCORE - internationalConfederation ofRevolver Enthusiasts) 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;
}

相关文章推荐

UVa 10763 - Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_p...

uva 10763 Foreign Exchange 交换生

uva 10763

[排序]Foreign Exchange uva10763

一到简单的排序题。 题意: 有一个非营利性的国际性机构执行一项国际交换学生计画。这几年来执行得相当好,有越来越多的学生想要参与这项计画,所以现在请你来帮忙这个机构。每个提出交换学生申请的人有2项资料:...

UVA 10763 Foreign Exchange

题意:n个学生要当交换生,每个学生想从A校换到B校,交换条件是找到一个想要从B换到A的学生配对,每个学生只能配对一次,问这些学生能不能都成功交换。 思路:这题挺简单的,首先是配对的问题,由于对于每个...
  • qian99
  • qian99
  • 2014年01月12日 16:06
  • 565

UVa 10763 - Foreign Exchange(结构体排序)

有n个学生要当交换生,输入每个人的目标地和原始地,学生两两交换,问能否交换成功。 用结构体排序做的,两个vector分别保存交换前和交换后的学生,对比目标地和原始地是否完全相同,相同即可完成交换。...
  • wcr1996
  • wcr1996
  • 2014年11月03日 18:15
  • 1336

习题5-4 交换学生 UVa10763

其实我的思路想复杂了,AC了之后看了别人的代码才发现如此简单

UVA 10763 Foreign Exchange 出国交换 pair+map

题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a)。 放在贪心这其实有点像检索。 用stl做,map+pair。 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有...
  • hcbbt
  • hcbbt
  • 2013年08月25日 21:13
  • 1883

[刷题]算法竞赛入门经典(第2版) 5-4/UVa10763 - Foreign Exchange

题意:有若干交换生、若干学校,有人希望从A校到B校,有的想从B到C、C到A等等等等。如果有人想从A到B也刚好有人想从B到A,那么可以交换(不允许一对多、多对一)。看作后如果有人找不到人交换,那么整个交...

【UVa】10763 – Foreign Exchange

Problem hereProblemYour non-profit organization (iCORE – international Confederation of Revolver Ent...

uva 10763 Foreign Exchange

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordina...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 10763 (13.12.05)
举报原因:
原因补充:

(最多只允许输入30个字)