题目链接:Foreign Exchange UVA - 10763
题目:有 n(1<=n<=500000) n ( 1 <= n <= 500000 ) 个学生想交换到其他学校学习。为了简单起见,规定每个想从A学校交换到B学校的学生必须找到一个想从B交换到A的“搭档”。如果每个人都能找到搭档(一个人不能当多个人的搭档),学校就会同意他们交换。每个学生用整数A、B表示,你的任务是判断交换是否可以进行。
思路:每个人即是一个二元组,用pair存,读取一个二元组就在map中映射一次,如果反向元存在则加1,否则如果正向元组存在则减1,如果两者都不存在则建立新的映射,并把ID记为1;存完了就开始循环找map中是否存在奇数或者小于1的数;
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<iomanip>
#include<set>
#include<sstream>
#define ll long long
using namespace std;
typedef pair<int,int> student;
student stu,stub;
map< student, int> IDcache;
vector<student>stucache;
int main()
{
int n;
while(1) {
scanf("%d",&n);
if(n == 0) break;
IDcache.clear(); stucache.clear();
int A,B;
for(int i = 0; i < n; i++) {
scanf("%d%d",&A, &B);
stu = make_pair(A, B);
stub = make_pair(B,A);
if(IDcache.count(stub))IDcache[stub]++;
else if(IDcache.count(stu))IDcache[stu]--;
else {stucache.push_back(stu); IDcache[stu]=1;}
}
bool yes = true;
for(int i = 0; i < stucache.size(); i++)
{
int id = IDcache[stucache[i]];
if(id<1 || id%2){ yes = false; break; }
}
printf("%s\n",yes?"YES":"NO");
}
return 0;
}