Codeforces 165D Beard Graph 【树链剖分】

题目链接:Codeforces 165D Beard Graph

D. Beard Graph
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define a non-oriented connected graph of n vertices and n - 1 edges as a beard, if all of its vertices except, perhaps, one, have the degree of 2 or 1 (that is, there exists no more than one vertex, whose degree is more than two). Let us remind you that the degree of a vertex is the number of edges that connect to it.

Let each edge be either black or white. Initially all edges are black.

You are given the description of the beard graph. Your task is to analyze requests of the following types:

paint the edge number i black. The edge number i is the edge that has this number in the description. It is guaranteed that by the moment of this request the i-th edge is white
paint the edge number i white. It is guaranteed that by the moment of this request the i-th edge is black
find the length of the shortest path going only along the black edges between vertices a and b or indicate that no such path exists between them (a path’s length is the number of edges in it)
The vertices are numbered with integers from 1 to n, and the edges are numbered with integers from 1 to n - 1.

Input
The first line of the input contains an integer n (2 ≤ n ≤ 105) — the number of vertices in the graph. Next n - 1 lines contain edges described as the numbers of vertices vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) connected by this edge. It is guaranteed that the given graph is connected and forms a beard graph, and has no self-loops or multiple edges.

The next line contains an integer m (1 ≤ m ≤ 3·105) — the number of requests. Next m lines contain requests in the following form: first a line contains an integer type, which takes values ​​from 1 to 3, and represents the request type.

If type = 1, then the current request is a request to paint the edge black. In this case, in addition to number type the line should contain integer id (1 ≤ id ≤ n - 1), which represents the number of the edge to paint.

If type = 2, then the current request is a request to paint the edge white, its form is similar to the previous request.

If type = 3, then the current request is a request to find the distance. In this case, in addition to type, the line should contain two integers a, b (1 ≤ a, b ≤ n, a can be equal to b) — the numbers of vertices, the distance between which must be found.

The numbers in all lines are separated by exactly one space. The edges are numbered in the order in which they are given in the input.

Output
For each request to “find the distance between vertices a and b” print the result. If there is no path going only along the black edges between vertices a and b, then print “-1” (without the quotes). Print the results in the order of receiving the requests, separate the numbers with spaces or line breaks.

Examples
input
3
1 2
2 3
7
3 1 2
3 1 3
3 2 3
2 2
3 1 2
3 1 3
3 2 3
output
1
2
1
1
-1
-1
input
6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 3
3 3 1
output
3
-1
3
2
Note
In the first sample vertices 1 and 2 are connected with edge number 1, and vertices 2 and 3 are connected with edge number 2. Before the repainting edge number 2 each vertex is reachable from each one along the black edges. Specifically, the shortest path between 1 and 3 goes along both edges.

If we paint edge number 2 white, vertex 3 will end up cut off from other vertices, that is, no path exists from it to any other vertex along the black edges.

题意:给定一无向图(其实推敲下就是一棵树),有m次操作。
1 x 把第x条边染成黑色
2 x 把第x条边染成白色
3 x y 查询x-y之间的黑边数,存在白边输出-1。

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 +10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Tree {
    int l, r, sum, Min;
};
Tree tree[MAXN<<2];
void PushUp(int o) {
    tree[o].sum = tree[ll].sum + tree[rr].sum;
    tree[o].Min = min(tree[ll].Min, tree[rr].Min);
}
void Build(int o, int l, int r) {
    tree[o].l = l; tree[o].r = r;
    if(l == r) {
        tree[l].sum = tree[l].Min = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    Build(ll, l, mid); Build(rr, mid+1, r);
    PushUp(o);
}
void Update(int o, int pos, int v) {
    if(tree[o].l == tree[o].r) {
        tree[o].sum = tree[o].Min = v;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(pos <= mid) Update(ll, pos, v);
    else Update(rr, pos, v);
    PushUp(o);
}
int Query(int o, int L, int R, int op) {
    if(tree[o].l == L && tree[o].r == R) {
        return op == 1 ? tree[o].sum : tree[o].Min;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid) return Query(ll, L, R, op);
    else if(L > mid) return Query(rr, L, R, op);
    else {
        if(op == 1) {
            return Query(ll, L, mid, op) + Query(rr, mid+1, R, op);
        }
        else {
            return min(Query(ll, L, mid, op), Query(rr, mid+1, R, op));
        }
    }
}
struct Edge {
    int from, to, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
void init() { edgenum = 0; CLR(head, -1); }
void addEdge(int u, int v) {
    Edge E = {u, v, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
int son[MAXN], num[MAXN];
int top[MAXN], pos[MAXN], id;
int dep[MAXN], pre[MAXN];
void DFS1(int u, int fa, int d) {
    dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1;
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(v == fa) continue;
        DFS1(v, u, d+1);
        num[u] += num[v];
        if(son[u] == -1 || num[son[u]] < num[v])
            son[u] = v;
    }
}
void DFS2(int u, int T) {
    top[u] = T; pos[u] = ++id;
    if(son[u] == -1) return ;
    DFS2(son[u], T);
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(v == pre[u] || v == son[u]) continue;
        DFS2(v, v);
    }
}
int GetSum(int u, int v) {
    int f1 = top[u], f2 = top[v];
    int ans = 0;
    while(f1 != f2) {
        if(dep[f1] < dep[f2]) {
            swap(u, v);
            swap(f1, f2);
        }
        ans += Query(1, pos[f1], pos[u], 1);
        u = pre[f1], f1 = top[u];
    }
    if(u == v) return ans;
    if(dep[u] > dep[v]) swap(u, v);
    return ans += Query(1, pos[son[u]], pos[v], 1);
}
int GetMin(int u, int v) {
    int f1 = top[u], f2 = top[v];
    int ans = 1;
    while(f1 != f2) {
        if(dep[f1] < dep[f2]) {
            swap(u, v);
            swap(f1, f2);
        }
        ans = min(Query(1, pos[f1], pos[u], 2), ans);
        u = pre[f1], f1 = top[u];
    }
    if(u == v) return ans;
    if(dep[u] > dep[v]) swap(u, v);
    return min(ans, Query(1, pos[son[u]], pos[v], 2));
}
int s[MAXN], e[MAXN];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF) {
        init();
        for(int i = 1; i <= n-1; i++) {
            scanf("%d%d", &s[i], &e[i]);
            addEdge(s[i], e[i]);
            addEdge(e[i], s[i]);
        }
        DFS1(1, -1, 1); id = 0; DFS2(1, 1); Build(1, 1, id);
        for(int i = 1; i <= n-1; i++) {
            if(dep[s[i]] > dep[e[i]]) swap(s[i], e[i]);
            Update(1, pos[e[i]], 1);
        }
        int Q; scanf("%d", &Q);
        while(Q--) {
            int op, x, y;
            scanf("%d", &op);
            if(op == 1) {
                scanf("%d", &x);
                Update(1, pos[e[x]], 1);
            }
            else if(op == 2) {
                scanf("%d", &x);
                Update(1, pos[e[x]], 0);
            }
            else {
                scanf("%d%d", &x, &y);
                if(GetMin(x, y) == 0) {
                    printf("-1\n");
                }
                else {
                    printf("%d\n", GetSum(x, y));
                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(Codeforces 165D Beard Graph 【树链剖分】)