There are nn friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.

For each friend the value fifi is known: it is either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi.

You want to fill in the unknown values (fi=0fi=0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.

If there are several answers, you can print any.

大意是有n个人相互给礼物(限定一个人只能收到一份,赠送一份),已知其中一部分人希望把礼物送给谁,要求补全这个关系图,只需要输出任何一种可能的情况。其中不能自己送给自己礼物。
自己的暴力做法是:将所有人分成三类。第一类:有送有收。第二类:有送无收。第三类:无送有收。第四类:无送无收。
首先建立一个优先队列用于输出,第一类已经彻底明确了的直接丢进去。易知第二类和第三类人数应该是一样的,这时候讨论第四类:
1.若第四类人数为0:
只需要将第二类的收和第三类的送对应起来。一一对应即可。
2.若第四类人数为1:
第二类第三类前面n-1对一一对应,最后一对之间插一个唯一的第四类。(因为要求不能自己送自己)。
3.第四类人数大于1:
第四类内部对应即可,内部构成一个环。
#include<bits/stdc++.h>
using namespace std;
int n; struct point
{
int num;
int to;
int from;
}f[]; bool operator<(point a, point b){ return a.num>b.num ;} priority_queue<point> q;
vector<point> v1,v2,v3;//v1仅有from v2仅有to v3双无
int main()
{
cin>>n;
int i;
for(i=;i<=n;i++)
{
f[i].num=i;
f[i].to=;
f[i].from=;
} for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
f[i].to=temp;
f[temp].from=i;
} for(i=;i<=n;i++)
{
if(f[i].from!=&&f[i].to!=)
{
q.push(f[i]);
}
else if(f[i].from==&&f[i].to!=)
{
v2.push_back(f[i]);
}
else if(f[i].from!=&&f[i].to==)
{
v1.push_back(f[i]);
}
else
{
v3.push_back(f[i]);
}
} //cout<<v1.size()<<' '<<v2.size()<<' '<<v3.size()<<' '<<q.size()<<endl; if(v3.size()!=)
{
for(i=;i<v2.size();i++)
{
v2[i].from=v1[i].num;
v1[i].to=v2[i].num;
q.push(v1[i]);
q.push(v2[i]);
}
if(v3.size()==)goto label;
for(i=;i<v3.size()-;i++)//注意 不能自己给自己
{
v3[i].to=v3[i+].num;
q.push(v3[i]); }
v3[v3.size()-].to=v3[].num;
q.push(v3[v3.size()-]);
}
else
{
for(i=;i<v2.size()-;i++)
{
v2[i].from=v1[i].num;
v1[i].to=v2[i].num;
q.push(v1[i]);
q.push(v2[i]);
}
v2[v2.size()-].from=v3[].num;
v1[v2.size()-].to=v3[].num;
v3[].to=v2[v2.size()-].num;
q.push(v1[v2.size()-]);
q.push(v2[v2.size()-]);
q.push(v3[]); }
label:;
while(!q.empty())
{
point temp=q.top();
cout<<temp.to<<' ';
q.pop();
} return ;
}
 
 

最新文章

  1. 手机游戏渠道SDK接入工具项目分享(二)万事开头难
  2. BZOJ3670 [Noi2014]动物园
  3. SQL Server中截取字符串常用函数
  4. ROS学习笔记(七)——节点
  5. echarts引入及应用
  6. TCP3次握手和4次挥手
  7. 开源App之MyHearts(二)
  8. 存储过程中的where in实现
  9. Windows Azure公有云服务相关方案
  10. aliyun云服务器硬件性能测试
  11. 【C++深入探索】Copy-and-swap idiom详解和实现安全自我赋值
  12. C#中IList&lt;T&gt;与List&lt;T&gt;的区别
  13. jquery为某div下的所有textbox的赋值
  14. 简单http文件服务器
  15. 使用Go语言+Protobuf协议完成一个多人聊天室
  16. java中int和Integer比较大小
  17. resetBuffer方法与reset方法的使用场景:解决生成HTML或者文件下载时的首部空白行的问题
  18. Android studio 添加admob googgle play services
  19. Maven clean命令不能执行问题Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project
  20. python为何需要虚拟环境--Python虚拟环境的安装和配置-virtualenv

热门文章

  1. Pacemaker+ISCSI实现Apache高可用-环境准备
  2. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法
  3. Redis 基本数据类型以及相应操作
  4. bugku 矛盾 30
  5. 寒假安卓app开发学习记录(5)
  6. Git - git bash 在 windows 下创建软连接
  7. 老大难的 Java ClassLoader,到了该彻底理解它的时候了
  8. * ./common/http.js in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-opt
  9. 解决idea无法下载通过maven添加的jar包以及下载网速过慢的问题
  10. linux下使用bower时提示bower ESUDO Cannot be run with sudo解决办法