zoukankan      html  css  js  c++  java
  • UVa 10763 Foreign Exchange(map)

    Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates a very successfulforeign student exchange program. Over the last few years, demand hassky-rocketed and now you need assistance with your task.

    The program yourorganization runs works as follows: All candidates are asked for their originallocation and the location they would like to go to. The program works out onlyif every student has a suitable exchange partner. In other words, if a studentwants to go from A to B, there must be another student who wants to go from Bto A. This was an easy task when there were only about 50 candidates, howevernow there are up to 500000 candidates!

    Input
    The input filecontains multiple cases. Each test case will consist of a line containing n -the number of candidates(1≤n≤500000), followed by n linesrepresenting the exchange information for each candidate. Each of these lineswill contain 2 integers, separated by a single space,representing the candidate's original location and the candidate's targetlocation respectively. Locations will be represented by nonnegative integernumbers. You may assume that no candidate will have his or her originallocation being the same as his or her target location as this would fall intothe domestic exchange program. The input is terminated by a case where n = 0;this case should not be processed.

    Output
    For each testcase, print "YES" on a single line if there is a wayfor the exchange program to work out, otherwise print"NO".

    Sample Input
    10
    1 2
    2 1
    3 4
    4 3
    100 200
    200 100
    57 2
    2 57
    1 2
    2 1
    10
    1 2
    3 4
    5 6
    7 8
    9 10
    11 12
    13 14
    15 16
    17 18
    19 20
    0

    Sample Output
    YES
    NO

    题意

    有n个交换生,规定A想和B交换,必须确定B想和A交换,判断是否所有学生都可以交换

    题解

    这个题看似很简单,其实小细节很多

    1.采用数组换过来再换回去最后判断是否全部对应来做(数组模拟不行)

    例子解释

    2

    1 2   SWAP(S[1],S[2])

    2 1 SWAP(S[2],S[1])

    YES

    最后S[1]=1,S[2]=2不变

    反例

    4

    1 2

    1 3

    2 1

    3 1

    YES

    2.A可以和B交换后,又出现A和B交换,这是可以的(map<int,int>不行)

    反例

    3

    1 2

    2 1

    1 2

    NO

    3.A可以想和多个人交换B,C,D,E(甚至重复),但是A只能与1个人交换(map<int,vector<int> >不行)

    反例

    3

    1 2

    1 2

    2 1

    NO

    所以我们只能考虑map<pair<int,int>,int>把A和A想和的人变成1个整体映射成1个数字(数字代表重复次数)

    如果找到匹配,就减1

    最后判断map里的所有值是否都为0

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef pair<int,int> pi;
     4 int main()
     5 {
     6     //freopen("in.txt","r",stdin);
     7     //freopen("out.txt","w",stdout);
     8     int n,a,b;
     9     while(cin>>n,n)
    10     {
    11         map<pi,int> stu;
    12         for(int i=0;i<n;i++)
    13         {
    14             cin>>a>>b;
    15             stu[pi(a,b)]++;
    16             if(stu[pi(b,a)]!=0&&stu[pi(a,b)]!=0)
    17             {
    18                 stu[pi(a,b)]--;
    19                 stu[pi(b,a)]--;
    20             }
    21         }
    22         int F=1;
    23         for(map<pi,int>::iterator it=stu.begin();it!=stu.end();it++)
    24         {
    25             if(it->second!=0)
    26             {
    27                 F=0;
    28                 break;
    29             }
    30         }
    31         if(F)cout<<"YES"<<endl;
    32         else cout<<"NO"<<endl;
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    Oracle分析函数
    oracle row_number的使用
    lru缓存测试类
    注解测试类
    lucene测试类
    SVN中检出(check out) 跟导出(export) 的区别
    Lucene原理与代码分析
    Lucene入门基础教程
    linux的less命令
    day4 大纲笔记
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8453370.html
Copyright © 2011-2022 走看看