UVA 10763-Foreign Exchange(求入度出度是否相等)

Foreign Exchange

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

Submit Status

Description

Problem E

Foreign Exchange

Input: standard input

Output: standard output

Time Limit: 1 second

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

题意:交换生现在是很受欢迎的,现在又一个负责这个的组织,经常会收到一大批的申请表, 申请内容是从A国家到B国家的。对于一批申请表, 会有各个不同国家申请到另外各个不同的国家, 假设有任意一个申请A到B的,但是没有B到A的申请, 那么这批申请表都不能被处理。

思路:这其实就是让你查找某个点的入度和出度相加是否为0;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int N=500010;
int in[N];
int main()
{
    int n,a,b,i;
    int flag;
    while(~scanf("%d",&n)){
        if(n==0) break;
        memset(in,0,sizeof(in));
        while(n--){
            scanf("%d %d",&a,&b);
            in[a]--;
            in[b]++;
        }
        flag=0;
        for(i=0;i<500010;i++){
            if(in[i]!=0){
               flag=1;
               break;
            }
        }
        if(flag)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}
时间: 2024-10-22 21:01:41

UVA 10763-Foreign Exchange(求入度出度是否相等)的相关文章

紫书第五章训练 uva 10763 Foreign Exchange by crq

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

uva 10763 Foreign Exchange

给定n对信息,是1-->2有一对交换生,能交换的条件是2-->1也有一对交换生,问能否顺利交换. 思路:用有向图模拟,如果1-->2有一对,那么就优先判断2-->1有没人交换,有的话,就不用加边了,直接标记那条边用了即可. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using n

uva 10763 Foreign Exchange(排序+二分查找)

这道题是我第一次算出来应该用什么复杂度写的题,或许是这一章刚介绍过,500000的数据必须用nlogn,所以我就 想到了二分,它的复杂度是logn,再对n个数据进行遍历,正好是nlogn,前两次TLE了,然后我就对我的做法没信心 了...看到一篇博客上说就应该这种方法,然后我就坚定的改自己的算法去了,哈哈,专注度没有达到五颗星,最多 三颗... 思路: 我用的是结构体保存的,先对每一对序列排序,然后对第二个元素在第一个元素中二分搜索,用到了 lower_bound,upper_bound,注释里

uva:10763 - Foreign Exchange(排序)

题目:10763 - Foreign Exchange 题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列.如果这个新的序列和原来的序列相同就符合要求.因为  (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b). 代码: #include <stdio.h> #include <string.h> #inclu

Foreign Exchange

 10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinatesa very successful foreign student exchange program. Over the last few years, demand hassky-rocketed and now you need assistan

UVA Foreign Exchange

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 th

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac

Foreign Exchange(交换生换位置)

 Foreign Exchange 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

uva 12230 - Crossing Rivers(求数学期望)

利用了数学期望的线性性质:有线个随机变量之和的数学期望的关于每个随机变量的期望之和: 由于过每条河的时间为L / V和3L / V的均匀分布,因此期望过河时间为2L / V. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n;double d; int main() { int kase=0; while(scanf("%d%lf",&a