UVa 10763 - Foreign Exchange

題目鏈接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1704

題目描述:有n個學生想交換到其他學校學習。爲了簡單起見,規定每個想從A學校交換到B學校的學生必須找一個想從B交換到A的“搭檔”。如果每個人都能找到搭檔(一個人不能當多個人的搭檔),學校就回同意交換。每個學生用兩個整數A,B表示,你的任務是判斷交換是否可以進行。

代碼如下(vs2012運行通過):

// 10935.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <map>

using namespace std;

#define FILE

typedef pair<int,int> Partner;

int _tmain(int argc, _TCHAR* argv[])
{
	#ifdef FILE
		ifstream in("data.txt");
		ofstream out("output.txt");
		cin.rdbuf(in.rdbuf());
		cout.rdbuf(out.rdbuf());
	#endif
	int n;
	while(cin>>n)
	{
		if(n==0)
			break;
		map<Partner,int> data;
		for(int i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			if(!data.count(Partner(a,b)))
				data[Partner(a,b)] = 1;
			else
				data[Partner(a,b)]++;
		}
		bool matched = true;
		for(map<Partner,int>::iterator it=data.begin();it!=data.end();it++)
		{
			int a = it->first.first,b=it->first.second;
			map<Partner,int>::iterator pos = data.find(Partner(b,a));
			if(pos==data.end()||it->second!=pos->second)
			{
				matched = false;
				cout<<"NO"<<endl;
				break;
			}
		}
		if(matched==true)
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章