目录

CF165D-Beard Graph

CF165D-Beard Graph

题目:

题目描述:

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 $ .

输入格式:

The first line of the input contains an integer $ n $ ( $ 2<=n<=10^{5} $ ) — the number of vertices in the graph. Next $ n-1 $ lines contain edges described as the numbers of vertices $ v_{i} $ , $ u_{i} $ ( $ 1<=v_{i},u_{i}<=n $ , $ v_{i}≠u_{i} $ ) 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·10^{5} $ ) — 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.

输出格式:

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.

样例:

样例输入 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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

样例输出 1:

1
2
3
4
5
6
1
2
1
1
-1
-1

样例输入 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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

样例输出 2:

1
2
3
4
3
-1
3
2

思路:

实现:

  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
#include "ybwhead/ios.h"
#define maxn 800001
#define int long long
#define L(x) (x << 1)
#define R(x) ((x << 1) | 1)
int tree[maxn], tag[maxn];
int rev[maxn], dep[maxn], size[maxn], seg[maxn], top[maxn], son[maxn], father[maxn];
int n, m, root, x, y, z, a[maxn], tot, mode;
int cnt, from[maxn], to[maxn], Next[maxn], head[maxn];
int Gx, Gy, Gz, Gd;
int fa[maxn], X[maxn], Y[maxn], Z[maxn];
void add(int x, int y)
{
    cnt++;
    from[cnt] = x;
    to[cnt] = y;
    Next[cnt] = head[x];
    head[x] = cnt;
}
void update(int node, int begin, int end, int x, int y, int val)
{
    if (begin > y || end < x)
        return;
    if (begin >= x && end <= y)
    {
        tree[node] = val;
        return;
    }
    else
    {
        int mid = (begin + end) >> 1;
        if (x <= mid)
            update(L(node), begin, mid, x, y, val);
        if (y > mid)
            update(R(node), mid + 1, end, x, y, val);
        tree[node] = tree[L(node)] + tree[R(node)];
    }
}
int query(int node, int begin, int end, int x, int y)
{
    if (begin >= x && end <= y)
    {
        return tree[node];
    }
    else
    {
        int mid = (begin + end) >> 1, sum = 0;
        if (x <= mid)
            sum += query(L(node), begin, mid, x, y);
        if (y > mid)
            sum += query(R(node), mid + 1, end, x, y);
        return sum;
    }
}
int dfs1(int x)
{
    size[x] = 1;
    dep[x] = dep[father[x]] + 1;
    for (int i = head[x]; i != -1; i = Next[i])
    {
        int v = to[i], big = 0;
        if (father[x] == v)
            continue;
        father[v] = x;
        big = dfs1(v);
        size[x] += big;
        if (big > size[son[x]])
            son[x] = v;
    }
    return size[x];
}
void dfs2(int x)
{
    if (son[x])
    {
        seg[son[x]] = ++seg[0];
        top[son[x]] = top[x];
        rev[seg[0]] = son[x];
        dfs2(son[x]);
    }
    for (int i = head[x]; i != -1; i = Next[i])
    {
        int v = to[i];
        if (!top[v])
        {
            seg[v] = ++seg[0];
            top[v] = v;
            rev[seg[0]] = v;
            dfs2(v);
        }
    }
}
int linkquery(int x, int y)
{
    int fx = top[x], fy = top[y], ans = 0;
    while (fx != fy)
    {
        if (dep[fx] < dep[fy])
            swap(x, y), swap(fx, fy);
        ans += query(1, 1, seg[0], seg[fx], seg[x]);
        x = father[fx];
        fx = top[x];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    ans += query(1, 1, seg[0], seg[x], seg[y]);
    ans -= query(1, 1, seg[0], seg[x], seg[x]);
    return ans;
}
void change(int x, int y)
{
    x *= 2;
    x = dep[from[x]] > dep[to[x]] ? from[x] : to[x];
    update(1, 1, seg[0], seg[x], seg[x], y);
}
signed main()
{
    memset(head, -1, sizeof(head));
    scanf("%lld", &n);
    root = 1;
    for (int i = 1; i <= n - 1; i++)
    {
        scanf("%lld%lld", &x, &y);
        add(x, y), add(y, x);
    }
    dfs1(root);
    seg[root] = ++seg[0];
    rev[seg[0]] = root;
    top[root] = root;
    dfs2(root);
    scanf("%lld", &m);
    for (int i = 1; i <= n - 1; i++)
        change(i, 1);
    for (int i = 1; i <= m; i++)
    {
        scanf("%lld%lld", &mode, &x);
        if (mode == 1)
            change(x, 1);
        if (mode == 2)
            change(x, 100000);
        if (mode == 3)
        {
            scanf("%lld", &y);
            int ans = 0;
            ans = linkquery(x, y);
            if (ans >= 100000)
                printf("-1\n");
            else
                printf("%lld\n", ans);
        }
    }
}