UVa10763

/*
按照题意的理解,满足条件,需要a->b和b->a学生数量一样,
因此设计map<pair<int,int>,int>dic来记录a->b学生数量,如果有b->a的,那么就a->b数量减1,
最后字典中每个元素的value都是0的时候,即可满足交换条件,否则不满足。
本题学习的地方是如何构造满足自己需要的hash函数,进而设计自定义的unordered_map
 
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<utility>
#include<unordered_set>
#include<unordered_map>
#include<string.h>
#include<fstream>
#include<deque>
 
struct pair_hash {
    template <class T1, class T2>
    std::size_t operator () (const std::pair<T1,T2> &p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);
 
        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};
 
using namespace std;
using Vote = pair<int, int>;
using Unordered_map = unordered_map<Vote, int, pair_hash>;
 
 
 
 
int main()
{
    int n,a,b;
    while(scanf("%d",&n)!=EOF&&n){
        Unordered_map dic;
        for(int i=0;i<n;++i){
            scanf("%d%d",&a,&b);
            auto p=make_pair(a,b),q=make_pair(b,a);
            if(dic.find(p)!=dic.end())++dic[p];
            else if(dic.find(q)!=dic.end())--dic[q];
            else dic[p]=1;
        }
        int flag=1;
        for(auto &p:dic)if(p.second!=0){flag=0;break;}
        printf("%s\n",flag?"YES":"NO");
    }
    return 0;
}
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值