Uva-10763-foreign Exchange

Source: Internet
Author: User
Tags integer numbers

UVA-10763Foreign Exchange
Time Limit: 3000MS Memory Limit: Unknown 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 E nthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand have sky-rocketed and now your need assistance with your task.

The program your organization runs works as Follows:all candidates is asked for their original location and the location They would like to go. The program works out only if every student have a suitable exchange partner. In the other words, if a student wants to go from a to B, there must is another student who wants to go from B to a. This is an easy task when the There were only about the candidates, however now there is up to 500000 candidates!

Input

The input file contains multiple cases. Each test case would 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 would contain 2 integers, separated by a single space, representing the candidate ' s original L Ocation and the candidate ' s target location respectively. Locations is represented by nonnegative integer numbers. Assume that no candidate'll has his or hers original location being the same as his or hers target location as th Is would fall to the domestic exchange program. The input is terminated by a case where n = 0; This case is should not being processed.

Output

For each test case, print ' YES ' on a ' if there is a ' a ', ' exchange program to work out, Otherwis E 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

Source

Root:: Competitive programming 3:the New Lower Bound of programming contests (Steven & Felix Halim):: Problem Solvi Ng Paradigms:: Greedy:: Involving sorting (Or the Input is already Sorted)
Root:: AOAPC i:beginning algorithm Contests (Rujia Liu):: Volume 4. Algorithm Design

Root:: Competitive programming 2:this increases the lower bound of programming contests. Again (Steven & Felix Halim):: Problem Solving Paradigms:: Greedy-standard
Root:: Competitive programming:increasing The Lower Bound of programming contests (Steven & Felix Halim):: Chapter 3. Problem solving Paradigms:: Greedy
Root:: AOAPC ii:beginning algorithm Contests (Second Edition) (Rujia Liu):: Chapter 5. C + + and STL:: Exercises
Root:: AOAPC i:beginning Algorithm Contests--Training Guide (Rujia Liu):: Chapter 1. Algorithm Design:: Designing Efficient Algorithms:: Exercises:beginner


Learn from Staginner's ideas


AC Code:


#include <cstdio> #include <cstring>using namespace std; int g[1000][1000], n;int check () {    int i,j;    for (i=0;i<1000;i++) for        (j=0;j<1000;j++)            if (g[i][j]!=0)                return 0;    return 1;} int main () {    int i, J, u, V;    while (1)    {        scanf ("%d", &n);        if (n==0) break            ;        memset (g, 0, sizeof (g));        for (i=0;i<n;i++)        {            scanf ("%d%d", &u,&v);            g[u][v]++;            g[v][u]--;        }        if (check ())            printf ("yes\n");        else            printf ("no\n");    }    return 0;    }


Uva-10763-foreign Exchange

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.