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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
| #include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; const int N = 1e5 + 500, INF = 1e9; struct Graph { vector<int> sons[N]; int ecnt = 0; void add_edge(int u, int v) { ecnt++; sons[u].push_back(v); } void clear(int n) { ecnt = 0; for (int i = 1; i <= n; i++) { sons[i].clear(); } } } at, bt; struct Seg { int minval, mincnt, atag; } segs[N * 4]; void merge(Seg& x, const Seg& a, const Seg& b) { if (a.minval == b.minval) { x.mincnt = a.mincnt + b.mincnt; x.minval = a.minval; } else if (a.minval < b.minval) { x.mincnt = a.mincnt; x.minval = a.minval; } else { x.mincnt = b.mincnt; x.minval = b.minval; } } void refresh(int x) { merge(segs[x], segs[x << 1], segs[x << 1 | 1]); } void tag_add(int x, int v) { segs[x].minval += v; segs[x].atag += v; } void pushdown(int x) { if (segs[x].atag) { tag_add(x << 1, segs[x].atag); tag_add(x << 1 | 1, segs[x].atag); segs[x].atag = 0; } } void rangeadd(int x, int l, int r, int al, int ar, int v) { if (al <= l && ar >= r) { tag_add(x, v); return; } pushdown(x); int mid = (l + r) >> 1; if (al <= mid) rangeadd(x << 1, l, mid, al, ar, v); if (ar > mid) rangeadd(x << 1 | 1, mid + 1, r, al, ar, v); refresh(x); } Seg query(int x, int l, int r, int ql, int qr) { if (ql <= l && qr >= r) { return segs[x]; } pushdown(x); int mid = (l + r) >> 1; Seg ans = (Seg){INF, 0, 0}; if (ql <= mid) merge(ans, ans, query(x << 1, l, mid, ql, qr)); if (qr > mid) merge(ans, ans, query(x << 1 | 1, mid + 1, r, ql, qr)); return ans; } int ves[N]; int dfn[N], arr[N], siz[N], dcnt; bool onpath[N]; void dfs(int u, int f) { dfn[u] = ++dcnt; arr[dcnt] = u; siz[u] = 1; ves[u] = ves[f] + 1; onpath[u] = true; for (int v : bt.sons[u]) { if (onpath[v]) { ves[u]--; } } for (int v : at.sons[u]) { if (v == f) continue; dfs(v, u); siz[u] += siz[v]; } onpath[u] = false; } void build(int x, int l, int r) { if (l == r) { segs[x].mincnt = 1; segs[x].atag = 0; segs[x].minval = ves[arr[l]]; return; } int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); refresh(x); } bool cmp(int a, int b) { return dfn[a] < dfn[b]; } bool in(int a, int b) { return dfn[b] >= dfn[a] && dfn[b] < dfn[a] + siz[a]; } void treeadd(int u, int v) { rangeadd(1, 1, dcnt, dfn[u], dfn[u] + siz[u] - 1, v); } void out() { for (int i = 1; i <= dcnt; i++) { cout << query(1, 1, dcnt, i, i).minval << " "; } cout << endl; }
int moveRoot(int u, int v, int x, int bi) { treeadd(v, -1 * x); while (bi < bt.sons[u].size() && dfn[bt.sons[u][bi]] < dfn[v] + siz[v]) { if (dfn[bt.sons[u][bi]] >= dfn[v]) treeadd(bt.sons[u][bi], 1 * x); bi++; }
treeadd(1, 1 * x); treeadd(v, -1 * x); for (int w : bt.sons[v]) { if (in(v, w)) continue; if (u == w) { treeadd(v, 1 * x); treeadd(1, -1 * x); } else if (in(w, u)) { int l = 0, r = at.sons[w].size() - 1; while (l < r) { int mid = (l + r + 1) >> 1; if (dfn[at.sons[w][mid]] <= dfn[u]) { l = mid; } else { r = mid - 1; } } int t = at.sons[w][l]; treeadd(t, 1 * x); treeadd(1, -1 * x); } else { treeadd(w, -1 * x); } } return bi; } int ans = 0; void solve(int u, int f) { ans += query(1, 1, dcnt, 1, dcnt).mincnt; sort(at.sons[u].begin(), at.sons[u].end(), cmp); sort(bt.sons[u].begin(), bt.sons[u].end(), cmp); int bi = 0; for (int v : at.sons[u]) { if (v == f) continue; moveRoot(u, v, 1, bi); solve(v, u); bi = moveRoot(u, v, -1, bi); } } void test() { memset(segs,0,sizeof(segs)); ans = 0; dcnt = 0; int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; bt.add_edge(u, v); bt.add_edge(v, u); } for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; at.add_edge(u, v); at.add_edge(v, u); } dfs(1, 0); build(1, 1, dcnt); solve(1, 0); cout << (ans - n) / 2 + n << endl; at.clear(n); bt.clear(n); } int main() { int t; cin >> t; while (t--) { test(); } }
|