【 UVA - 10763 】Foreign Exchange (交换学生)pair+map

题目链接

代码:

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef pair<int,int> p; // A-->B (A想去B)
int main()
{
    
    
	int n;
	while(cin>>n && n)
	{
    
    
		int a,b,cnt=n; //cnt表示目前n个需求
		map<p,int> m; //记录是否存在xx想去xx
		for(int i=0;i<n;i++)
		{
    
    
			cin>>a>>b;
			if(!m[{
    
    b,a}]) m[{
    
    a,b}]++; //如果不存在对方想来这边的,记录下这边的情况
			else m[{
    
    b,a}]--,cnt-=2; //如果存在对方想来这边,可以配对,减去一次对面到这边的情况,总需求减2
		}
		if(!cnt) cout<<"YES"<<endl; //总需求为0,都配对了
		else cout<<"NO"<<endl; 
	}
   	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/108184282
今日推荐