紫书第五章训练 uva 10763 Foreign Exchange by crq

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.
The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to 500000 candidates!

Input

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

Output

For each test case, print "YES" on a single line if there is a way for 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

题意分析:给出一系列交换位置的需求,如果同时存在(a b)交换和(b a)交换,则两者交换成功,因此直接判断是否存在一对交换即可。一开始使用map存储:

map<int, int> mp;

第一次碰到a和b交换时,记录mp[a] = b;后面碰到(b, a)交换时,匹配成功,并将其从map中删除。但后面发现关键字并不唯一,比如:

1 2
1 3
3 1
2 1
这种情况下,前面的记录会被覆盖,因此采用multimap来存储。

multimap<int, int> mp;

根据关键字从mp中查找后还要继续遍历找到对应的一对交换,找到则删除,否则加入mp。

最后如果mp中为空,则表明全部交换成功,否则说明有不成功的情况。

AC源码

 1 #include <iostream>
 2 #include <string>
 3 #include <list>
 4 #include <sstream>
 5 #include <map>
 6 using namespace std;
 7 
 8 int main()
 9 {
10 //    freopen("d:\data1.in","r",stdin);
11     int n;
12     while(cin>>n, n)
13     {
14         multimap<int, int> mp;
15         for(int i=0;i<n;i++)
16         {
17             int x, y;
18             cin>>x>>y;
19             int num = mp.count(y);//找到元
20             multimap<int,int>::iterator it = mp.find(y);
21             int flag = 0;
22             for (int j = 0;j!=num; j++, it++)
23             {
24                 int  v = it->second;
25                 if(v==x)
26                 {
27                     mp.erase(it);
28                     flag = 1;
29                     break;
30                 }    
31             }
32             if(flag==0)
33             {
34                 mp.insert(make_pair(x, y));
35             }
36         }
37         if(mp.size()==0)
38             cout<<"YES"<<endl;
39         else
40             cout<<"NO"<<endl;
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/tzcacm/p/6846154.html