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
| #include <cstdio> #include <algorithm> 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 << 3) + (x << 1) + c - '0'; if (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) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 1e5 + 10; int n, m, C, q, v[N]; struct LCT { struct Node { int p, s[2], tag, v, d; } tr[N]; void pushup (int x) { tr[x].v = max(v[x], max(tr[tr[x].s[0]].v, tr[tr[x].s[1]].v)); } void reverse (int x) { tr[x].tag ^= 1; swap(tr[x].s[0], tr[x].s[1]); } void pushdown (int x) { if (!tr[x].tag) return; reverse(tr[x].s[0]), reverse(tr[x].s[1]); tr[x].tag = 0; } void update (int x) { if (!isrt(x)) update(tr[x].p); pushdown(x); } bool isrt (int x) { return tr[tr[x].p].s[0] ^ x && tr[tr[x].p].s[1] ^ x; } void rotate (int x) { int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; if (!isrt(y)) tr[z].s[tr[z].s[1] == y] = x; tr[x].p = z; tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y; tr[x].s[k ^ 1] = y, tr[y].p = x; pushup(y), pushup(x); } void splay (int x) { update(x); for (; !isrt(x); rotate(x)) { int y = tr[x].p, z = tr[y].p; if (!isrt(y)) rotate((y == tr[z].s[1]) ^ (x == tr[y].s[1]) ? x : y); } } void access (int t) { for (int x = t, y = 0; x; y = x, x = tr[x].p) splay(x), tr[x].s[1] = y, pushup(x); splay(t); } void mkrt (int x) { access(x); reverse(x); } int findrt (int x) { access(x); for (; tr[x].s[0]; x = tr[x].s[0]) pushdown(x); splay(x); return x; } void link (int x, int y) { mkrt(x); if (findrt(y) ^ x) tr[x].p = y, ++tr[x].d, ++tr[y].d; } bool cut (int x, int y) { mkrt(x); if (findrt(y) == x && tr[y].p == x && !tr[y].s[0]) { tr[y].p = tr[x].s[1] = 0; pushup(x); --tr[x].d, --tr[y].d; return true; } return false; } void Op0 (int x, int y) { mkrt(x); v[x] = y; pushup(x); } void Op2 (int u, int v) { if (findrt(u) ^ findrt(v)) return void(puts("-1")); mkrt(u), access(v); write(tr[v].v), puts(""); } } lct[20]; int main () { read(n, m, C, q); for (int i = 1; i <= n; ++i) { read(v[i]); for (int j = 0; j < C; ++j) lct[j].pushup(i); } for (int a, b, c; m; --m) read(a, b, c), lct[c].link(a, b); for (int op; q; --q) { read(op); if (op == 0) { int x, y; read(x, y); for (int i = 0; i < C; ++i) lct[i].Op0(x, y); } else if (op == 1) { int u, v, w; read(u, v, w); int t = -1; for (int i = 0; i < C && !~t; ++i) if (lct[i].cut(u, v)) lct[i].link(u, v), t = i; if (!~t) { puts("No such edge."); continue; } if (w ^ t && (lct[w].tr[u].d == 2 || lct[w].tr[v].d == 2)) { puts("Error 1."); continue; } if (w ^ t && (lct[w].findrt(u) == lct[w].findrt(v))) { puts("Error 2."); continue; } lct[t].cut(u, v); lct[w].link(u, v); puts("Success."); } else if (op == 2) { int c, u, v; read(c, u, v); lct[c].Op2(u, v); } } return 0; }
|