Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign 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 to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to 500000 candidates!

Input

  The input file contains multiple cases. Each test case will consist of a line containing n – the number of candidates (1 ≤n≤ 500000), followed by n lines representing the exchange information for each candidate. Each of these lines will contain 2 integers, separated by a single space, representing the candidate’s original location and the candidate’s target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where n = 0; this case should not be processed.

Output

  For each test case, print ‘YES’ on a single line if there is a way 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
3
1 2
2 3
3 1
0

Sample Output

YES
NO
YES

HINT

  一个错误的题目,按照题目的意思就是只有A->B,然后B->A才是符合题意得,但实际上题目是按照只要离开学校和来到学校得一样多,最终一个学校得人数没有减少或者增加就好。

  这样直接使用map映射就可以,键就是学校,值就是人数变化。来开学校则键值--,进入学校就++,如果键值为0就删除。因此如果程序最后得map是空的说明是符合要求的。

Accepted

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<int, int>arr; int main()
{
int a, b,n;
while (cin >> n && n)
{
arr.clear();
while (n--)
{
cin >> a >> b;
arr[a]--;
if (arr[a] == 0)arr.erase(a);
if (arr.count(b))arr[b]++;
else arr[b] = 1;
if (arr[b] == 0)arr.erase(b);
}
if (arr.empty())cout << "YES" << endl;
else cout << "NO" << endl;
}
}

Foreign Exchange UVA - 10763的更多相关文章

  1. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  2. uva 10763 Foreign Exchange(排序比较)

    题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...

  3. uva:10763 - Foreign Exchange(排序)

    题目:10763 - Foreign Exchange 题目大意:给出每一个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思 ...

  4. uva 10763 Foreign Exchange <"map" ,vector>

    Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...

  5. UVA Foreign Exchange

    Foreign Exchange Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Your non ...

  6. Foreign Exchange

     10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver ...

  7. Foreign Exchange(交换生换位置)

     Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enth ...

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

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

  9. UVA 10763 Foreign Exchange

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description Your non- ...

随机推荐

  1. CentOS7安装ElasticSearch7.9.2

    1:下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar. ...

  2. Java多线程并发编程/锁的理解

    一.前言 最近项目遇到多线程并发的情景(并发抢单&恢复库存并行),代码在正常情况下运行没有什么问题,在高并发压测下会出现:库存超发/总库存与sku库存对不上等各种问题. 在运用了 限流/加锁等 ...

  3. 在 c++ 程序中出现CtrIsValidHeapPointer问题

    在c++程序中出现CtrIsValidHeapPointer问题, 我发现的原因是申请了大量动态数组但是并没有把他们初始化 为数组赋初始值便可以很好解决这一问题.

  4. WPF -- 一种圆形识别方案

    本文介绍一种圆形的识别方案. 识别流程 判断是否为封闭图形: 根据圆的方程,取输入点集中的1/6.3/6.5/6处的三个点,求得圆的方程,获取圆心及半径: 取点集中的部分点,计算点到圆心的距离与半径的 ...

  5. const成员函数可以将非const指针作为返回值吗?

    先给出一段代码 class A { int *x; public: int *f() const { return x; } }; 成员函数f返回指向私有成员 x 的非常量指针,我认为这会修改成员x ...

  6. 微信小程序一周时间表

    <view class="dateView"> <image class="dateLeft" bindtap="prevWeek& ...

  7. MySQL深入研究--学习总结(2)

    前言 接上文,继续学习后续章节. 第四章&第五章<深入浅出索引> 这两章节主要介绍的索引结构及其如何合理建立索引,但是我觉得讲的比较简单. 总结回顾下吧,其实在我之前的文章< ...

  8. pytorch(03)tensor的操作

    张量操作 一.张量的拼接 torch.cat() 功能:将张量按维度dim进行拼接,且[不会扩张张量的维度] tensors:张量序列 dim:要拼接的维度 torch.cat(tensors, di ...

  9. MYSQL的replace into

    replace into t(id, update_time) values(1, now()); 或 replace into t(id, update_time) select 1, now(); ...

  10. Python开发环境从零搭建-03-安装Python解释器并配置

    想要从零开始搭建一个Python的开发环境说容易也容易 说难也能难倒一片开发人员,在接下来的一系列视频中,会详细的讲解如何一步步搭建python的开发环境 本文章是搭建环境的第3篇 讲解的内容是:安装 ...