1339. Babiestime limit:1.0 Second
Memory limit:64 MB
O tempora! O mores!Present-day babies progress quickly. There is exactly
kBoys and
kGirls in the kindergarten. Some Boys like Some girls. Boys is still knights, so, if some boy like some girl then he likes the only girl and moreover one D The same girl can ' t be liked to more than one boy. And the girls in this is true ladies. So, if a girl likes a boy she likes the only one, and different girls like different boys. The children is ingenuous. Their secret amorousness is well-known to the nurse. Once the group decided to go-a walk and the nurse made up and the fall of the children in pairs so, if there is a Boy or a girl-in-a pair then the likes he pair-mate or the girl likes the boy. Help the nurse to arrange the described pairs. Assume that either the boys or the girls enumerated with positive integers from 1 to
k. Inputthe first line contains the integer
k-the Number of boys (1≤
k≤250 000). The second line consists of the numbers of girls that is liked by boys:if the
I' th boy likes some girls.
I' th position; If the
I' Th boy likes Nobody, there was 0 at the
I' th position. The numbers is separated with a space. The third line consists of the analogous information about the girls. Outputyou should output the sequence of
kIntegers. The
I' th element of the sequence is the number of a girl which is a pair-mate of the
I' th boy. The numbers is separated with a space. Sample
input |
Output |
33 0 00) 2 0 |
3 2 1 |
problem Author:Magaz Asanov
problem Source:USU Championship 2004
Tags:Graph theory()difficulty:966 Test Instructions: There are n boys and girls, each boy only at most one likes the girl, each girl only at most one likes the boy. Boys like the girls are different, the girls like the boys are different. Ask for a pairing scheme that makes every pair of people or at least one of them like each other, or two people who don't like them. Analysis: Found that this is a figure with only one out of each point, in degrees. If it's a ring, just make it right, and the question is guaranteed to be no strange situation. If it's not a ring, start connecting with someone nobody likes. Note the odd chain. This is again Ural on the difficulty of high problem.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineFor (I, S, T) for (int i = (s); I <= (t); i++) + #defineFord (I, S, T) for (int i = (s); I >= (t); i--) A #defineRep (i, T) for (int i = (0); i < (t); i++) at #defineREPN (i, T) for (int i = (t)-1); I >= (0); i--) - #defineRep (i, X, T) for (int i = (x); i < (t); i++) - #defineMIT (2147483647) - #defineINF (1000000001) - #defineMLL (1000000000000000001LL) - #defineSZ (x) ((int) (x). Size ()) in #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front to #definePub push_back + #definePOF Pop_front - #definePOB pop_back the #defineFT first * #defineSD Second $ #defineMk Make_pairPanax NotoginsengInlinevoidSetio (stringName) - { the stringInput = name+". in", +Output = name+". out"; AFreopen (Input.c_str (),"R", stdin), theFreopen (Output.c_str (),"W", stdout); + } - $ $InlineintGetint () - { - intRet =0; the CharCh =' '; - BOOLFlag =0;Wuyi while(! (Ch >='0'&& Ch <='9')) the { - if(Ch = ='-') Flag ^=1; WuCh =GetChar (); - } About while(Ch >='0'&& Ch <='9') $ { -RET = RET *Ten+ Ch-'0'; -Ch =GetChar (); - } A returnFlag? -Ret:ret; + } the - Const intN =250010; $ intN, Boy[n], girl[n]; the intFa[n *2], Next[n *2]; the intAns[n *2]; the theInlinevoidInput () - { inscanf"%d", &n); thefor (I,1, N) scanf ("%d", &boy[i]); thefor (I,1, N) scanf ("%d", &girl[i]); About } the theInlinevoidSolve () the { +for (I,1, N) - { the if(Boy[i])Bayi { theNext[i] = Boy[i] +N; theFa[boy[i] + N] =i; - } - if(Girl[i]) the { theNext[i + N] =Girl[i]; theFa[girl[i]] = i +N; the } - } the thefor (I,1,2*N) the if(!Fa[i])94 { the intx =i; the while(Next[x] &&!Ans[x]) the {98ANS[X] =Next[x]; AboutANS[NEXT[X]] =x; -x =Next[x];101x =Next[x];102 }103 }104 thefor (I,1,2*N)106 if(!Ans[i])107 {108 intx =i;109 while(Next[x] &&! ANS[X] &&!Ans[next[x]]) the {111ANS[X] =Next[x]; theANS[NEXT[X]] =x;113x =Next[x]; thex =Next[x]; the } the }117 118 intx = n +1;119for (I,1, N) - if(!Ans[i])121 {122 while(Ans[x]) x + +;123Ans[i] =x;124ANS[X] =i; the }126 127for (I,1N1) printf ("%d", Ans[i]-n); -printf"%d\n", Ans[n]-n);129 } the 131 intMain () the {133 #ifndef Online_judge134Setio ("B");135 #endif136 Input ();137 Solve ();138 return 0;139}
View Code
Ural 1339. Babies