uva 10763 Foreign Exchange

题目:Foreign Exchange


题意:用(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;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值