算法竞赛入门经典 习题5-4

UVa 10763

Foreign Exchange

公益性组织iCORE(international Confederation of Revolver Enthusiasts)需要处理约500000国际交换生的交换问题,即如果A国家的学生想交换到B国家,那么就需要有一个B国家的学生要交换到A国家。

可以使用集合来记录和查找是否存在有效的配对,注意如果有多个A国家的学生都要交换到B国家,那么B国家也要有相同数量的学生交换到A国家才可以,因此需要使用map

#include <iostream>
#include <map>

using namespace std;

int main()
{
	int n = 0;
	while (cin >> n) {
		if (n == 0) break;
		map<pair<int, int>, size_t> locations;
		for (int i = 0; i < n; i++)
		{
			pair<int, int> location, exchange;
			cin >> location.first >> location.second;
			exchange.second = location.first;
			exchange.first = location.second;
			if (locations.find(exchange) == locations.end()) {
				locations[location]++;
			}
			else if (--locations[exchange] == 0) {
					locations.erase(exchange);
			}
			else;
		}
		if (locations.empty()) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
	return 0;
}
/*
10
1 2
2 1
3 4
4 3
100 200
200 100
57 2
2 57
1 2
2 1
10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
0
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值