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
| #include <cstdio> #include <vector> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 1e5 + 10; vector <int> g[N]; int n, m, d[N], p[N], sz[N]; int stmp, dfn[N], son[N], top[N]; struct Data { int lc, rc, s; }; const Data e = (Data){ 0, 0, -1 }; Data operator ~ (Data _) { swap(_.lc, _.rc); return _; } struct Node { int l, r, tag; Data k; } tr[N << 2]; Data pushup (Data l, Data r) { if (!~l.s) return r; if (!~r.s) return l; return (Data){ l.lc, r.rc, l.s + r.s + (l.rc == r.lc) }; } void cover (int x, int c) { tr[x].k.lc = tr[x].k.rc = c; tr[x].k.s = tr[x].r - tr[x].l; tr[x].tag = c; } void pushdown (int x) { if (!~tr[x].tag) return; cover(x << 1, tr[x].tag), cover(x << 1 | 1, tr[x].tag); tr[x].tag = -1; } void build (int l = 1, int r = n, int x = 1) { tr[x].l = l, tr[x].r = r, tr[x].tag = -1; if (l == r) return void(tr[x].k = (Data){ -l, -l, 0 }); int mid = l + r >> 1; build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1); tr[x].k = pushup(tr[x << 1].k, tr[x << 1 | 1].k); } void modify (int l, int r, int c, int x = 1) { if (l > tr[x].r || tr[x].l > r) return; if (tr[x].l >= l && tr[x].r <= r) return cover(x, c); pushdown(x); modify(l, r, c, x << 1), modify(l, r, c, x << 1 | 1); tr[x].k = pushup(tr[x << 1].k, tr[x << 1 | 1].k); } Data query (int l, int r, int x = 1) { if (l > tr[x].r || tr[x].l > r) return e; if (tr[x].l >= l && tr[x].r <= r) return tr[x].k; pushdown(x); return pushup(query(l, r, x << 1), query(l, r, x << 1 | 1)); } void dfs1 (int u) { sz[u] = 1, son[u] = 0; for (int v : g[u]) if (v ^ p[u]) { p[v] = u; d[v] = d[u] + 1; dfs1(v); sz[u] += sz[v]; if (sz[v] > sz[son[u]]) son[u] = v; } } void dfs2 (int u, int t) { top[u] = t; dfn[u] = ++stmp; if (!son[u]) return; dfs2(son[u], t); for (int v : g[u]) if (v ^ p[u] && v ^ son[u]) dfs2(v, v); } void ModifyPath (int a, int b, int c) { while (top[a] ^ top[b]) { if (d[top[a]] < d[top[b]]) swap(a, b); modify(dfn[top[a]], dfn[a], c); a = p[top[a]]; } if (d[a] > d[b]) swap(a, b); modify(dfn[a], dfn[b], c); } Data QueryPath (int a, int b) { Data da = e, db = e; while (top[a] ^ top[b]) { if (d[top[a]] < d[top[b]]) swap(a, b), swap(da, db); da = pushup(query(dfn[top[a]], dfn[a]), da); a = p[top[a]]; } if (d[a] > d[b]) swap(a, b), swap(da, db); return pushup(pushup(~da, query(dfn[a], dfn[b])), db); } int main () { int T; read(T); while (T--) { read(n, m); for (int i = 1; i <= n; ++i) g[i].clear(); for (int i = 1, a, b; i < n; ++i) read(a, b), g[a].pb(b), g[b].pb(a); stmp = 0; dfs1(1), dfs2(1, 1); build(); for (int i = 1, op, a, b; i <= m; ++i) { read(op, a, b); if (op == 1) ModifyPath(a, b, i); else if (op == 2) write(QueryPath(a, b).s), puts(""); } } return 0; }
|