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
| #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 = 4e5 + 10, M = 20; vector <int> g[N]; int n, tot, m, q, lg[N], p[N], d[N], w[N], v[N][M]; int fa (int x) { return p[x] == x ? x : p[x] = fa(p[x]); } namespace LCA { int stmp, id[N], st[N][M]; int dmin (int a, int b) { return d[a] < d[b] ? a : b; } void dfs (int x) { st[id[x] = ++stmp][0] = x; for (int i : g[x]) { d[i] = d[x] + 1; dfs(i), st[++stmp][0] = x; } } void init () { stmp = 0; int rt = fa(1); d[rt] = 0; dfs(rt); for (int k = 0; k < lg[stmp]; ++k) for (int i = stmp + 1 - (1 << k + 1); i; --i) st[i][k + 1] = dmin(st[i][k], st[i + (1 << k)][k]); } int lca (int a, int b) { a = id[a], b = id[b]; if (a > b) swap(a, b); int k = lg[b - a + 1]; return dmin(st[a][k], st[b - (1 << k) + 1][k]); } } int main () { int T; read(T); for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1; while (T--) { read(n, m, q); tot = n; for (int i = 1; i <= n; ++i) p[i] = i, g[i].clear(); for (int i = 1, a, b; i <= m; ++i) { read(a, b); a = fa(a), b = fa(b); if (a == b) continue; p[a] = p[b] = ++tot; p[tot] = tot, g[tot].clear(); w[tot] = i; g[tot].pb(a), g[tot].pb(b); } LCA::init(); for (int i = 1; i < n; ++i) v[i][0] = w[LCA::lca(i, i + 1)]; for (int k = 0; k < lg[n - 1]; ++k) for (int i = n - (1 << k + 1); i; --i) v[i][k + 1] = max(v[i][k], v[i + (1 << k)][k]); for (int l, r; q; --q) { read(l, r); if (l == r) write(0); else { int k = lg[r - l]; write(max(v[l][k], v[r - (1 << k)][k])); } putchar(' '); } puts(""); } return 0; }
|