题目链接:http://codeforces.com/problemset/problem/165/D
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.
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
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
2
1
1
-1
-1
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
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.
思路:树链剖分的模板题。只要会树链剖分的模板,这题就是一道水题了。只是码量有点大。详见代码。推荐几篇我参考的博客。
http://www.cnblogs.com/jinkun113/p/4683299.html
http://blog.csdn.net/u014141559/article/details/43866459
http://blog.csdn.net/squee_spoon/article/details/41458753
附上AC代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 100005;
// 分别表示以当前节点作为根的子树的节点数目,
// 树上各个节点的初始值,当前节点的重儿子
int sizev[maxn], num[maxn], son[maxn];
// 分别表示树链上深度最小的节点,当前节点的深度,
// 原节点在剖分后的时间戳,即新的编号
int top[maxn], deep[maxn], pos[maxn];
// 分别表示当前时间戳对应的原节点编号,
// 当前节点的父节点
int level[maxn], p[maxn];
// 判断该节点是否被访问过了
bool vis[maxn];
// 分别表示节点数,询问数和时间戳计数
int n, q, cnt;
// 存储树的各个节点所连接的边
vector<int> edge[maxn];
pii id[maxn<<2];
int sumv[maxn<<2], minv[maxn<<2];
void add_edge(int u, int v){
edge[u].push_back(v);
edge[v].push_back(u);
}
void dfs1(int u, int root){
vis[u] = true;
sizev[u] = 1;
p[u] = root;
deep[u] = deep[root]+1;
int siz = edge[u].size();
for (int i=0; i<siz; ++i){
int v = edge[u][i];
if (v!=p[u] && !vis[v]){
dfs1(v, u);
sizev[u] += sizev[v];
if (son[u] == 0)
son[u] = v;
else if (sizev[son[u]] < sizev[v])
son[u] = v;
}
}
}
void dfs2(int u, int root){
vis[u] = true;
pos[u] = ++cnt;
level[cnt] = u;
top[u] = root;
if (son[u])
dfs2(son[u], root);
int siz = edge[u].size();
for (int i=0; i<siz; ++i){
int v = edge[u][i];
if (v!=p[u] && v!=son[u] && !vis[v])
dfs2(v, v);
}
}
void push_up(int rt){
sumv[rt] = sumv[lrt]+sumv[rrt];
minv[rt] = min(minv[lrt], minv[rrt]);
}
void build(int l, int r, int rt){
sumv[rt] = r-l+1;
minv[rt] = 1;
if (l == r)
return ;
int m = (l+r)>>1;
build(lson);
build(rson);
}
void modify(int p, int val, int l, int r, int rt){
if (l == r){
sumv[rt] = val;
minv[rt] = val;
return ;
}
int m = (l+r)>>1;
if (p <= m)
modify(p, val, lson);
else
modify(p, val, rson);
push_up(rt);
}
int query(int ql, int qr, int l, int r, int rt, bool ok){
if (ql<=l && r<=qr)
return ok ? sumv[rt] : minv[rt];
int m = (l+r)>>1;
int sumr = ok ? 0 : 1;
if (ql <= m){
int t = query(ql, qr, lson, ok);
sumr = ok ? sumr+t : min(sumr, t);
}
if (qr > m){
int t = query(ql, qr, rson, ok);
sumr = ok ? sumr+t : min(sumr, t);
}
return sumr;
}
int seek(int x, int y, bool ok){
int ans = ok ? 0 : 1;
while (top[x] != top[y]){
if (deep[top[x]] < deep[top[y]])
swap(x, y);
int t = query(pos[top[x]], pos[x], 1, cnt, 1, ok);
ans = ok ? ans+t : min(ans, t);
x = p[top[x]];
}
if (x == y)
return ans;
if (deep[x] > deep[y])
swap(x, y);
int t = query(pos[son[x]], pos[y], 1, cnt, 1, ok);
return ok ? ans+t : min(ans, t);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int op, a, b;
for (int i=1; i<n; ++i){
cin >> a >> b;
add_edge(a, b);
id[i] = mp(a, b);
}
dfs1(1, 0);
memset(vis, false, sizeof(vis));
dfs2(1, 1);
build(1, cnt, 1);
for (int i=1; i<n; ++i)
if (deep[id[i].f] < deep[id[i].s])
swap(id[i].f, id[i].s);
cin >> q;
while (q--){
cin >> op >> a;
if (3 == op){
cin >> b;
int t = seek(a, b, false);
//cout << t << endl;
if (t == 0)
cout << -1 << endl;
else
cout << seek(a, b, true) << endl;
}
else
modify(pos[id[a].f], op==1 ? 1 : 0, 1, cnt, 1);
}
return 0;
}