UVa 10763
公益性组织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
*/