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
代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,a,b;
while(cin>>n,n)
{
map<pi,int> stu;
for(int i=;i<n;i++)
{
cin>>a>>b;
stu[pi(a,b)]++;
if(stu[pi(b,a)]!=&&stu[pi(a,b)]!=)
{
stu[pi(a,b)]--;
stu[pi(b,a)]--;
}
}
int F=;
for(map<pi,int>::iterator it=stu.begin();it!=stu.end();it++)
{
if(it->second!=)
{
F=;
break;
}
}
if(F)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
UVa 10763 Foreign Exchange(map)的更多相关文章
- uva 10763 Foreign Exchange <"map" ,vector>
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...
- UVA 10763 Foreign Exchange 出国交换 pair+map
题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...
- uva 10763 Foreign Exchange(排序比较)
题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...
- UVA 10763 Foreign Exchange
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Your non- ...
- uva:10763 - Foreign Exchange(排序)
题目:10763 - Foreign Exchange 题目大意:给出每一个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思 ...
- 【UVA】10763 Foreign Exchange(map)
题目 题目 分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢. 代码 #include < ...
- Foreign Exchange
10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver ...
- UVA Foreign Exchange
Foreign Exchange Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Your non ...
- [刷题]算法竞赛入门经典(第2版) 5-4/UVa10763 - Foreign Exchange
题意:有若干交换生.若干学校,有人希望从A校到B校,有的想从B到C.C到A等等等等.如果有人想从A到B也刚好有人想从B到A,那么可以交换(不允许一对多.多对一).看作后如果有人找不到人交换,那么整个交 ...
随机推荐
- VMware 安装Windows sever 2008 R2服务器
一. 安装包下载: Windows Server 2008 R2 简体中文企业版[server 2008 r2下载] 二. 新建虚拟机 三. 安装Window Server 2008 R2 四. 服务 ...
- ESXI 5.5加载 zabbix OVF 3.2.6操作
如果是虚拟机安装ZABBIX,ZABBIX的前台WEB时间,是由虚拟机的BIOS时间决定的. 一. 1.去官方下载vmdk磁盘镜像 链接地址为https://sourceforge.net/proje ...
- CentOS命令行性能检测工具
一.uptime Uptime命令的显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average).load average值分别记录了上个1分钟,5分钟和1 ...
- python中os常用方法
python中OS常用方法 Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问 ...
- node多进程
内容: 1.多进程与多线程 2.node中多进程相关模块的使用 1.多进程与多线程 多线程:性能高:复杂.考验程序员 多进程:性能略低:简单.对程序员要求低 Node.js中默认:单进程.单线程,但是 ...
- https Configure a Spring Boot app for HTTPS on Amazon AWS.
参考: https://geocolumbus.github.io/HTTPS-ELB-AWS-Spring-Boot/ 1. 在服务器端配置 证书 域名 映射 2. 导入依赖: <depe ...
- DEMO: springboot 与 mybatis 集成
之前一直在用springMVC,接触到springboot之后,感觉使用起来方便多了,没那多xml需要配置. 先来看看整个项目结构,当然是maven项目. 1.测试数据 DROP TABLE IF E ...
- 批量得到/修改word超链接
Alt+F9或者勾选下面 此时的超链接地址全部转换为文本形式进行显示; 然后可以用全局替换搜索来处理
- AES 加密256位 错误 java.security.InvalidKeyException: Illegal key size or default parameters
Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件. 官方网站提供了JCE无限制权限策略文件的下载: JDK8的下载地址: http:/ ...
- c++标准库中的string常用函数总结《转》
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...