1339. Babies

Time limit: 1.0 second
Memory limit: 64 MB
O tempora! O mores!
Present-day babies progress quickly. There are exactly k boys and k girls in the kindergarten. Some boys like some girls. But in this age the boys are still knights, so, if some boy like some girl then he likes the only girl and moreover one and the same girl can’t be liked by more than one boy. And the girls in this age are true ladies. So, if a girl likes a boy she likes the only one, and different girls like different boys.
The children are ingenuous. Their secret amorousness is well-known to the nurse. Once the group decided to go for a walk and the nurse made up her mind to fall the children in pairs so that if there is a boy or a girl in love in a pair then the boy likes his pair-mate or the girl likes the boy. Help the nurse to arrange the described pairs. You may assume that either the boys or the girls enumerated with positive integers from 1 to k.

Input

The first line contains the integer k — the number of boys (1 ≤ k ≤ 250 000). The second line consists of the numbers of girls that are liked by boys: if the i'th boy likes some girls, her number is at the i'th position; if the i'th boy likes nobody, there is 0 at the i'th position. The numbers are separated with a space. The third line consists of the analogous information about the girls.

Output

You should output the sequence of k integers. The i'th element of the sequence is the number of a girl that is a pair-mate of the i'th boy. The numbers are separated with a space.

Sample

input output
3
3 0 0
0 2 0
3 2 1
Problem Author: Magaz Asanov
Problem Source: USU Championship 2004
Difficulty: 966 
 
题意:有n个男孩女孩,每个男孩只有至多一个喜欢的女孩,每个女孩只有至多一个喜欢的男孩。男孩喜欢的女孩互不相同,女孩喜欢的男孩互不相同。
求一个配对方案使得每一对人要么至少其中一个人喜欢对方,或两个人都没有喜欢的人。
分析:
发现这是一个每个点都只有一个出度、入度的图。
如果是环的话就随便连就好,题目保证不会有奇怪的情况。
如果不是环,就从没有人喜欢的那个人开始连。
注意奇链。
这又是ural上难度虚高的题目。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, Boy[N], Girl[N];
int Fa[N * ], Next[N * ];
int Ans[N * ]; inline void Input()
{
scanf("%d", &n);
For(i, , n) scanf("%d", &Boy[i]);
For(i, , n) scanf("%d", &Girl[i]);
} inline void Solve()
{
For(i, , n)
{
if(Boy[i])
{
Next[i] = Boy[i] + n;
Fa[Boy[i] + n] = i;
}
if(Girl[i])
{
Next[i + n] = Girl[i];
Fa[Girl[i]] = i + n;
}
} For(i, , * n)
if(!Fa[i])
{
int x = i;
while(Next[x] && !Ans[x])
{
Ans[x] = Next[x];
Ans[Next[x]] = x;
x = Next[x];
x = Next[x];
}
} For(i, , * n)
if(!Ans[i])
{
int x = i;
while(Next[x] && !Ans[x] && !Ans[Next[x]])
{
Ans[x] = Next[x];
Ans[Next[x]] = x;
x = Next[x];
x = Next[x];
}
} int x = n + ;
For(i, , n)
if(!Ans[i])
{
while(Ans[x]) x++;
Ans[i] = x;
Ans[x] = i;
} For(i, , n - ) printf("%d ", Ans[i] - n);
printf("%d\n", Ans[n] - n);
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("B");
#endif
Input();
Solve();
return ;
}

最新文章

  1. Java字节、十进制、十六进制、字符串之间的相互转换
  2. android 定义 程序 Scheme 接收特定URI开启Activity
  3. javascript 的事件--阻止冒泡
  4. linux NFS服务器安装与配置 思路
  5. 斯坦福大学CS224d基础1:线性代数回顾
  6. (WPF, MVVM) Textbox Binding
  7. 2.Sprng-IoC-Java反射例子
  8. 运行在YARN上的MapReduce应用程序(以MapReduce为例)
  9. Android 开机自启动
  10. C++primer拾遗(第二章:变量和基本类型)
  11. 51Nod--1051最大子矩阵和(DP入门)
  12. python模块 - 常用模块推荐
  13. SQL语句的CRUD
  14. RabbitMQ死信队列另类用法之复合死信
  15. .Net WebApi 初探
  16. 前端 CSS 介绍
  17. java 开发注意事项
  18. centos安装Django之二:pip3安装
  19. (A - 整数划分 HYSBZ - 1263)(数组模拟大数乘法)
  20. Python 入门基础5 --元组、字典、集合

热门文章

  1. 关于each
  2. php JS和JQ
  3. WindowManagerPolicy的后缀 解释
  4. Android init.rc执行顺序
  5. ASP.NET Web Api 安全性(转载)
  6. djcelery的细节篇
  7. android 入门-动画与容器
  8. PHPCMS-V9 获取一级栏目、二级栏目、三级栏目
  9. 老生常谈,正确使用memset
  10. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分