Problem 165D - Beard Graph 树链剖分

来源:互联网 发布:linux usr local bin 编辑:程序博客网 时间:2024/06/10 17:47

D. Beard Graph
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 viui (1 ≤ vi, ui ≤ nvi ≠ 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 integersab (1 ≤ a, b ≤ na 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.

Sample test(s)
input
31 22 373 1 23 1 33 2 32 23 1 23 1 33 2 3
output
1211-1-1
input
61 56 42 33 55 663 3 42 53 2 63 1 22 33 3 1
output
3-132
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 1and 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.


关于边的树链剖分,用边的时间戳较大的点来标记此条边,然后此题是单点更新,区间查询,用BIT即可。

代码:

#include<cstdio>#include<iostream>#include<cstring>#define Maxn 100010using namespace std;struct edge{    int to,next;}p[Maxn<<1];int head[Maxn],tot;void addedge(int a,int b){    p[tot].to=b;    p[tot].next=head[a];    head[a]=tot++;}int eg[Maxn<<1][2];int n,c[Maxn];void update(int x,int d){    while(x<=n){        c[x]+=d;        x+=x&-x;    }}int sum(int x){    int res=0;    while(x){        res+=c[x];        x-=x&-x;    }    return res;}int get(int l,int r){    return sum(r)-sum(l-1);}int sz[Maxn],son[Maxn],dep[Maxn],fa[Maxn];void dfs1(int u,int pre){    sz[u]=1;    son[u]=0;    fa[u]=pre;    dep[u]=dep[pre]+1;    for(int i=head[u];i!=-1;i=p[i].next){        int v=p[i].to;        if(v==pre) continue;        dfs1(v,u);        sz[u]+=sz[v];        if(sz[v]>sz[son[u]]) son[u]=v;    }}int top[Maxn],dfn[Maxn],tmpdfn;void dfs2(int u,int pre){    top[u]=pre;    dfn[u]=++tmpdfn;    if(son[u]) dfs2(son[u],pre);    for(int i=head[u];i!=-1;i=p[i].next){        int v=p[i].to;        if(v!=fa[u]&&v!=son[u])            dfs2(v,v);    }}void init(){    tmpdfn=0;    dfs1(1,0);    dfs2(1,0);    memset(c,0,sizeof c);}int solve(int a,int b){    int dis=0;    while(top[a]!=top[b]){        if(dep[top[a]]<dep[top[b]]) swap(a,b);        if(get(dfn[top[a]],dfn[a])) return -1;        dis+=dfn[a]-dfn[top[a]]+1;        a=fa[top[a]];    }    if(a==b) return dis;    if(dep[a]>dep[b]) swap(a,b);    if(get(dfn[son[a]],dfn[b])) return -1;    dis+=dfn[b]-dfn[a];    return dis;}int main(){    int m,a,b,op;    while(~scanf("%d",&n)){        tot=0;        memset(head,-1,sizeof head);        for(int i=1;i<n;i++){            scanf("%d%d",&eg[i][0],&eg[i][1]);            addedge(eg[i][0],eg[i][1]);            addedge(eg[i][1],eg[i][0]);        }        init();        scanf("%d",&m);        for(int i=0;i<m;i++){            scanf("%d%d",&op,&a);            if(op!=3){                if(dfn[eg[a][0]]<dfn[eg[a][1]])                    swap(eg[a][0],eg[a][1]);                if(op==1) update(dfn[eg[a][0]],-1);                else update(dfn[eg[a][0]],1);            }            else{                scanf("%d",&b);                printf("%d\n",solve(a,b));            }        }    }return 0;}


0 0
原创粉丝点击