1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| int n; map<int, int>edge[maxn];
struct f { int fa[maxn]; int getfa(int x) { if (x == fa[x])return x; else { fa[x] = getfa(fa[x]); return fa[x]; } } }A, L, R;
int deg[maxn]; int flag[maxn], ans[maxn], pos[maxn]; vector<int>nums[maxn]; vector<int>path;
void dfs(int x) { while (edge[x].size()) { int id = edge[x].begin()->first; int to = edge[x].begin()->second; edge[x].erase(id); edge[to].erase(-id); dfs(to); path.push_back(id); } }
void solve() { sa(n); repp(i, 1, n) { A.fa[i] = i; } repp(i, 1, n) { int x, y; sa(x), sa(y); edge[x][i] = y; edge[y][-i] = x; deg[x] ++; deg[y] ++; A.fa[A.getfa(x)] = A.getfa(y); } repp(i, 1, n) { nums[A.getfa(i)].push_back(i); } int cnt = 0; int m = n; repp(i, 1, n) { if (nums[i].size() == 0)continue; vector<int>odd; rep(k, 0, nums[i].size()) { if (deg[nums[i][k]] % 2) { odd.push_back(nums[i][k]); } } while (odd.size()>2) { int k1 = odd.back(); odd.pop_back(); int k2 = odd.back(); odd.pop_back(); m++; edge[k1][m] = k2; edge[k2][-m] = k1; } int start = i; if (odd.size())start = odd[0]; dfs(start); while (path.size() != 0) { int id = path.back(); path.pop_back(); if (abs(id) <= n) { if (id<0) { flag[abs(id)] = 1; } ans[++cnt] = abs(id); } } } repp(i, 1, n) { L.fa[i] = i; R.fa[i] = i; pos[ans[i]] = i; } repp(i, 1, n - 1) { int x; sa(x); x = pos[x]; x = L.getfa(x); if (x == 1) { int y = R.getfa(x) + 1; printf("%d %d\n", ans[x], ans[y]); R.fa[R.getfa(x)] = y; L.fa[L.getfa(y)] = x; } else { int y = L.getfa(x - 1); printf("%d %d\n", ans[y], ans[x]); R.fa[R.getfa(y)] = x; L.fa[L.getfa(x)] = y; } } repp(i, 1, n) { if (i>1)printf(" "); printf("%d", flag[i]); } }
|