UVA10763(Foreign Exchange )

2018-06-08  本文已影响0人  myleosu

UVA10763传送门
题目大意:参照紫书,题目给出多行两个数字A,B然后一个(A,B)需要和一个(B,A)对应,若全部都有对应则输出YES,否则输出NO。
思路:根据题意然后利用STL的map暴力模拟即可,就是对于每一个(A,B)看map有没有对应的(B,A),有则将(B,A)数量减一,减至零则删去(B,A)。没有则将(A,B)放进map。
AC代码:

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;
typedef pair<int,int>two_data; 
map<two_data,int>check;

int main()
{
    //freopen("text.txt","r",stdin);
    int n;
    while(cin>>n && n){
        int x,y;
        for(int i = 0;i<n;i++){
            scanf("%d%d",&x,&y);
            two_data temp = make_pair(y,x);
            two_data temp2 = make_pair(x,y);
            if(check.count(temp)){
                if(--check[temp]==0)
                    check.erase(temp);
            }
            else{
                if(check.count(temp2))
                    check[temp2]++;
                else
                    check[temp2] = 1;
            }
        }
        if(check.empty())   printf("YES\n");
        else                printf("NO\n"),check.clear();
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读