题目描述:
有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;
}