标签:
input | output |
---|---|
3 3 0 0 0 2 0 |
3 2 1 |
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 }
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/4999138.html