UVa 10763 交换学生(Foreign Exchange)

题目描述:
有n(1≤n≤500000)个学生想交换到其他学校学习。为了简单起见,规定每个想从A学
校换到B学校的学生必须找一个想从B换到A的“搭档”。如果每个人都能找到搭档(一个人不
能当多个人的搭档),学校就会同意他们交换。每个学生用两个整数A、B表示,你的任务
是判断交换是否可以进行。

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

下面贴代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;
const int N=1000;
int n,x,y;
map<pair<int,int>,int> num;
bool flag=true;

int main()
{
    while(cin>>n&&n!=0)
    {
        for(int i=1;i<=n;++i)
        {
            cin>>x>>y;
            if(x>y){
                num[make_pair(x,y)]++;
            }
            else 
            num[make_pair(y,x)]--;
        }
        for(map<pair<int,int>,int>::iterator i=num.begin();i!=num.end();++i)
        {
            if(i->second==0)continue;
            else
            {
                flag=false;
                cout<<"NO"<<endl;
                break;
            }
        }
        if(flag==true)
        {
            cout<<"YES"<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值