ural 1339. Babies

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

Tags: graph theory  (hide tags for unsolved problems)

Difficulty: 966

题意:有n个男孩女孩,每个男孩只有至多一个喜欢的女孩,每个女孩只有至多一个喜欢的男孩。男孩喜欢的女孩互不相同,女孩喜欢的男孩互不相同。

求一个配对方案使得每一对人要么至少其中一个人喜欢对方,或两个人都没有喜欢的人。

分析:

发现这是一个每个点都只有一个出度、入度的图。

如果是环的话就随便连就好,题目保证不会有奇怪的情况。

如果不是环,就从没有人喜欢的那个人开始连。

注意奇链。

这又是ural上难度虚高的题目。

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define For(i, s, t) for(int i = (s); i <= (t); i++)
 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
 22 #define Rep(i, t) for(int i = (0); i < (t); i++)
 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
 24 #define rep(i, x, t) for(int i = (x); i < (t); i++)
 25 #define MIT (2147483647)
 26 #define INF (1000000001)
 27 #define MLL (1000000000000000001LL)
 28 #define sz(x) ((int) (x).size())
 29 #define clr(x, y) memset(x, y, sizeof(x))
 30 #define puf push_front
 31 #define pub push_back
 32 #define pof pop_front
 33 #define pob pop_back
 34 #define ft first
 35 #define sd second
 36 #define mk make_pair
 37 inline void SetIO(string Name)
 38 {
 39     string Input = Name+".in",
 40     Output = Name+".out";
 41     freopen(Input.c_str(), "r", stdin),
 42     freopen(Output.c_str(), "w", stdout);
 43 }
 44
 45
 46 inline int Getint()
 47 {
 48     int Ret = 0;
 49     char Ch = ‘ ‘;
 50     bool Flag = 0;
 51     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
 52     {
 53         if(Ch == ‘-‘) Flag ^= 1;
 54         Ch = getchar();
 55     }
 56     while(Ch >= ‘0‘ && Ch <= ‘9‘)
 57     {
 58         Ret = Ret * 10 + Ch - ‘0‘;
 59         Ch = getchar();
 60     }
 61     return Flag ? -Ret : Ret;
 62 }
 63
 64 const int N = 250010;
 65 int n, Boy[N], Girl[N];
 66 int Fa[N * 2], Next[N * 2];
 67 int Ans[N * 2];
 68
 69 inline void Input()
 70 {
 71     scanf("%d", &n);
 72     For(i, 1, n) scanf("%d", &Boy[i]);
 73     For(i, 1, n) scanf("%d", &Girl[i]);
 74 }
 75
 76 inline void Solve()
 77 {
 78     For(i, 1, n)
 79     {
 80         if(Boy[i])
 81         {
 82             Next[i] = Boy[i] + n;
 83             Fa[Boy[i] + n] = i;
 84         }
 85         if(Girl[i])
 86         {
 87             Next[i + n] = Girl[i];
 88             Fa[Girl[i]] = i + n;
 89         }
 90     }
 91
 92     For(i, 1, 2 * n)
 93         if(!Fa[i])
 94         {
 95             int x = i;
 96             while(Next[x] && !Ans[x])
 97             {
 98                 Ans[x] = Next[x];
 99                 Ans[Next[x]] = x;
100                 x = Next[x];
101                 x = Next[x];
102             }
103         }
104
105     For(i, 1, 2 * n)
106         if(!Ans[i])
107         {
108             int x = i;
109             while(Next[x] && !Ans[x] && !Ans[Next[x]])
110             {
111                 Ans[x] = Next[x];
112                 Ans[Next[x]] = x;
113                 x = Next[x];
114                 x = Next[x];
115             }
116         }
117
118     int x = n + 1;
119     For(i, 1, n)
120         if(!Ans[i])
121         {
122             while(Ans[x]) x++;
123             Ans[i] = x;
124             Ans[x] = i;
125         }
126
127     For(i, 1, n - 1) printf("%d ", Ans[i] - n);
128     printf("%d\n", Ans[n] - n);
129 }
130
131 int main()
132 {
133     #ifndef ONLINE_JUDGE
134     SetIO("B");
135     #endif
136     Input();
137     Solve();
138     return 0;
139 }

时间: 2024-10-10 14:58:13

ural 1339. Babies的相关文章

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th

Ural 1091 Tmutarakan Exams

Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 109164-bit integer IO format: %lld      Java class name: (Any) University of New Tmutarakan trains the first-class specialists in mental arith