习题5-4 UVA 10763 Foreign Exchange

用map建立pair和int的映射即可。

#include 
using namespace std;
int main()
{
	int n, a, b;
	while(scanf("%d", &n) != EOF && n){
		map<pair<int, int>, int> mp;
		int ok = 1;
		for(int i = 0; i < n; i++){
			scanf("%d%d", &a, &b);
			if(a < b){
				if(mp.find(make_pair(a, b)) == mp.end()) mp[make_pair(a, b)] = 1;
				else mp[make_pair(a, b)]++;
			}else{
				swap(a, b);
				if(mp.find(make_pair(a, b)) == mp.end()) mp[make_pair(a, b)] = -1;
				else mp[make_pair(a, b)]--;
			}
		}
		for(map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); it++){
			if(it->second){
				ok = 0;
				break;
			}
		}
		if(ok) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
} 

你可能感兴趣的:(#,第五章)