Foreign Exchange

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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".

解题:直接模拟交换好了,如果最后还与交换之前一样,则YES

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int d[maxn],n,u,v;
int main() {
while(scanf("%d",&n),n) {
for(int i = ; i <= n; ++i) d[i] = i;
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
swap(d[u],d[v]);
}
bool flag = true;
for(int i = ; i <= n; ++i)
if(d[i] != i) {
flag = false;
break;
}
puts(flag?"YES":"NO");
}
return ;
}

UVA Foreign Exchange的更多相关文章

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

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

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

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

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

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

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

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

  5. Foreign Exchange

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

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

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

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

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

  8. UVA 10763 Foreign Exchange

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

  9. Foreign Exchange UVA - 10763

      Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordin ...

随机推荐

  1. node工具是是什么东西

    Node到底是个啥? Node是一个服务器端JavaScript解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不深我还不知道,不过确实不浅 最近写 ...

  2. 发个ZKW线段树板子测试一下代码高亮

    是我,Long time no see          --Jim 先安利 Wolves  歌手:Madilyn Bailey http://music.163.com/song/524149464 ...

  3. HDU1061 - Rightmost Digit

    Given a positive integer N, you should output the most right digit of N^N. Input The input contains ...

  4. NOI 2011 阿狸的打字机 (AC自动机+dfs序+树状数组)

    题目大意:略(太长了不好描述) 良心LOJ传送门 先对所有被打印的字符串建一颗Trie树 观察数据范围,并不能每次打印都从头到尾暴力建树,而是每遍历到一个字符就在Trie上插入这个字符,然后记录每次打 ...

  5. [USACO15DEC]最大流Max Flow

    树剖LCA+树上差分. 树上差分的基本操作. #include <queue> #include <iostream> #include <cstdio> usin ...

  6. 转载一遍比较好的,django2.1搭建博客教程

    非常感谢这位博主,找了几个星期终于找到了 https://www.dusaiphoto.com/article/article-detail/4/

  7. [luogu] P3089 [USACO13NOV]POGO的牛Pogo-Cow

    P3089 [USACO13NOV]POGO的牛Pogo-Cow 题目描述 In an ill-conceived attempt to enhance the mobility of his pri ...

  8. 【BZOJ 1486】 [HNOI2009]最小圈

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们可以只想那个均值最小的环. 我们不知道那个环由哪些边构成 但我们可以把每条边都减掉mid 那个环受到的影响是什么呢? 如果这个均 ...

  9. 2015 Multi-University Training Contest 6 hdu 5361 In Touch

    In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  10. UVA 12507 Kingdoms

    D - Kingdoms Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu A kingdom ha ...