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

   日期:2020-08-24     浏览:147    评论:0    
核心提示:题目链接代码:#include #include #include using namespace std;typedef pair p; // A-->B (A想去B)int main(){ int n; while(cin>>n && n) { int a,b,cnt=n; //cnt表示目前n个需求 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;
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服