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;
}

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