Foreign Exchange UVA - 10763

题目链接:Foreign Exchange UVA - 10763

题目:有 n1<=n<=500000 n ( 1 <= n <= 500000 ) 个学生想交换到其他学校学习。为了简单起见,规定每个想从A学校交换到B学校的学生必须找到一个想从B交换到A的“搭档”。如果每个人都能找到搭档(一个人不能当多个人的搭档),学校就会同意他们交换。每个学生用整数A、B表示,你的任务是判断交换是否可以进行。

思路:每个人即是一个二元组,用pair存,读取一个二元组就在map中映射一次,如果反向元存在则加1,否则如果正向元组存在则减1,如果两者都不存在则建立新的映射,并把ID记为1;存完了就开始循环找map中是否存在奇数或者小于1的数;

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<iomanip>
#include<set>
#include<sstream>
#define ll long long

using namespace std;


typedef pair<int,int> student;

student stu,stub;

map< student, int> IDcache;
vector<student>stucache;

int main()
{
    int n;
    while(1) {
     scanf("%d",&n);
     if(n == 0) break;
     IDcache.clear(); stucache.clear();
     int A,B;
     for(int i = 0; i < n; i++) {
        scanf("%d%d",&A, &B);
        stu = make_pair(A, B);
        stub = make_pair(B,A);
        if(IDcache.count(stub))IDcache[stub]++;
            else if(IDcache.count(stu))IDcache[stu]--;
                else {stucache.push_back(stu); IDcache[stu]=1;}
     }
     bool yes = true;
     for(int i = 0; i < stucache.size(); i++)
     {
        int id = IDcache[stucache[i]];
        if(id<1 || id%2){ yes = false; break; }
    }
    printf("%s\n",yes?"YES":"NO");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值