ACM: uva 10763 -&…

来源:互联网 发布:ios 网络请求第三方 编辑:程序博客网 时间:2024/05/16 05:54

Foreign Exchange

 

Your non-profit organization(iCORE international Confederationof Revolver Enthusiasts)coordinates a very successful foreign student exchange program.Over the last few years, demand has sky-rocketed and now you needassistance with your task.

The program your organization runs works as follows: Allcandidates are asked for their original location and the locationthey would like to go to. The program works out only if everystudent has a suitable exchange partner. In other words, if astudent wants to go from A to B, there must be another student whowants to go from B to A. This was an easy task when there were onlyabout 50 candidates, however now there are upto 500000 candidates!

Input

The input file contains multiple cases. Each test case willconsist of a linecontaining n - the numberof candidates (1≤n≤500000), followedby n lines representingthe exchange information for each candidate. Each of these lineswill contain 2 integers,separated by a single space, representing the candidate's originallocation and the candidate's target location respectively.Locations will be represented by nonnegative integer numbers. Youmay assume that no candidate will have his or her original locationbeing the same as his or her target location as this would fallinto the domestic exchange program. The input is terminated by acasewhere n 0;this case should not be processed.

 

Output

For each test case,print "YES" on a singleline if there is a way for the exchange program to work out,otherwise print "NO".

 

题意: 有n个人想当交换生, 有信息自己location和想去的location, 判断是否可以满足全部人得需求.

 

解题思路:

    1. 简单排序即可.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 500005

struct node
{
 int A, B;
 bool operator <(const node&x) const
 {
  if(A == x.A) return B< x.B;
  else return A <x.A;
 }
}a[MAX], b[MAX];

int n;

int main()
{
// freopen("input.txt", "r", stdin);
 int i;
 while(scanf("%d", &n) !=EOF)
 {
  if(n == 0) break;
  for(i = 0; i <n; ++i)
  {
   scanf("%d%d", &a[i].A, &a[i].B);
   b[i].A =a[i].B;
   b[i].B =a[i].A;
  }

  sort(a, a+n);
  sort(b, b+n);
  bool flag = false;
  for(i = 0; i <n; ++i)
  {
   if( a[i].A ==b[i].A && a[i].B == b[i].B )continue;
   else
   {
    flag= true;
    break;
   }
  }

  if(flag)printf("NO\n");
  else printf("YES\n");
 }
 return 0;
}

0 0
原创粉丝点击