题意:用(A,B)描述一个学生,学生(A,B)可以和(B,A)交换,且一个学生只能交换一次,问是否可以全部交换。
思路:用map存储学生(A,B)的数量,当学生(A,B)的数量与学生(B,A)的数量相同时,符合条件。
注意:当n为奇数时,不能直接判断不符合条件。
代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
using namespace std;
struct Student{
int a,b;
Student(){}
Student(int x,int y){
a=x,b=y;
}
bool operator <(const Student& other) const {
if(a<other.a||(a==other.a&&b<other.b)) return true;
return false;
}
};
int n;
map<Student,int> mp;
int main() {
while(scanf("%d",&n)==1&&n!=0) {
mp.clear();
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
Student aa=Student(x,y);
if(!mp.count(aa)) mp[aa]=0;
mp[aa]=mp[aa]+1;
}
bool flag=true;
for(map<Student,int>::iterator it=mp.begin();it!=mp.end();it++){
Student x=(it->first),y=Student(x.b,x.a);
if(!mp.count(y)||mp[x]!=mp[y]){
flag=false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}