uva 10763 Foreign Exchange 교환 학생

5126 단어 uva
원제: Your non-profit organization(iCORE-international Confederation of Revolver Enthusiasts)coor-dinates 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 can- didate’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 loca- tion 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 1 3 4 3 100 200 100 57 2 57 1 2 1 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 Sample Output YES NO 문제 대의: 학생 n명을 주고 학생 한 명당 자신이 있는 학교의 번호와 교환하고 싶은 학생의 번호를 입력한다.만약에 a가 b학교에서 c학교에 가고 싶다면 반드시 c학교에서 b학교에 가고 싶은 학생이 있어야 한다.마지막으로 교환 계획을 완성할 수 있느냐고 물었다.사고방식은 코드 아래를 보십시오.
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
using namespace std;
const int N=500001;
bool ans[N];
int max(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
int main()
{
    ios::sync_with_stdio(false);
    int n,a,b,index,tem,flag;
    while(cin>>n,n)
    {
        index=1;tem=0;flag=0;
        memset(ans,false,sizeof(ans));
        for(int i=1;i<=n;i++)
        {
            cin>>a>>b;
            if(!ans[a])
            ans[a]=1;
            else
            ans[a]=0;
            if(!ans[b])
            ans[b]=1;
            else
            ans[b]=0;
            tem=max(a,b);
            index=max(index,tem);
        }
        for(int i=1;i<=index;i++)
        {
            if(ans[i])
            {
                flag=1;
                break;
            }
        }
        if(flag)
        printf("NO
"
); else printf("YES
"
); } return 0; }

사고방식: 너에게 두 개의 수를 주는 것과 같고, 두 개의 스위치의 번호와 같다.만약 이 스위치가 on이라면 우리는 그것을 끄고, 만약 off라면 우리는 on이다.마지막으로 불이 켜져 있는지 없는지 봅시다~사실도 하나의 그림입니다. 출도와 입도가 같을지 아닌지를 판단할 뿐입니다~~

좋은 웹페이지 즐겨찾기