Codeforce--165D--Beard Graph(树链剖分)

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

题意:给出一个无向图,n-1条边,初始时每条边为黑色,三种操作,1,把第x条边变成黑色;2,把第x条边变成白色;3,查询x,y之间变得条数,如果有白边输出-1;

思路:树链剖分裸题,,,好像没什么坑,判断有没有白色的边见代码,加一个判断就行;

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 200005
using namespace std;
int n,m;
int head[maxn],fa[maxn],top[maxn],id[maxn],sz[maxn],dep[maxn],son[maxn];
int tot,num;
struct Edge
{
    int to;
    int next;
}edge[maxn*2];
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
struct E{int x;int y;}e[maxn];
void dfs1(int u,int f,int deep)
{
    fa[u]=f;
    dep[u]=deep;
    sz[u]=1;
    son[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==fa[u]) continue;
        dfs1(ff,u,deep+1);
        sz[u]+=sz[ff];
        if(sz[ff]>sz[son[u]]) son[u]=ff;
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=num++;
    if(son[u]) dfs2(son[u],tp);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==fa[u]||ff==son[u]) continue;
        dfs2(ff,ff);
    }
}
struct node
{
    int l,r;
    int val;
}tree[maxn*4];
void pushup(int root)
{
    tree[root].val=tree[root<<1].val+tree[root<<1|1].val;
}
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    if(l==r){
        tree[root].val=1;
        return;
    }
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    pushup(root);
}
void update(int root,int p,int type)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(ll==rr)
    {
        if(type==1) tree[root].val=1;
        else tree[root].val=0;
        return;
    }
    int mid=(ll+rr)>>1;
    if(p<=mid) update(root<<1,p,type);
    else update(root<<1|1,p,type);
    pushup(root);
}
int query(int root,int l,int r)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(l<=ll&&rr<=r)
        return tree[root].val;
    int mid=(ll+rr)>>1;
    int ans=0;
    if(l<=mid) ans+=query(root<<1,l,r);
    if(r>mid) ans+=query(root<<1|1,l,r);
    return ans;
}
int Yougth(int u,int v)
{
    int tp1=top[u];
    int tp2=top[v];
    int ans=0,tmp;
    while(tp1!=tp2)
    {
        if(dep[tp1]<dep[tp2]){
            swap(tp1,tp2);
            swap(u,v);
        }
        tmp=query(1,id[tp1],id[u]);
        if(tmp<id[u]-id[tp1]+1) return -1;//和下面还有一个判断是有点点区别的,体会一下
        ans+=tmp;
        u=fa[tp1];
        tp1=top[u];
    }
    if(u==v) return ans;
    if(dep[u]>dep[v]) swap(u,v);
    tmp=query(1,id[son[u]],id[v]);
    if(tmp<id[v]-id[u]) return -1;
    ans+=tmp;
    return ans;
}
void init()
{
    tot=num=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&e[i].x,&e[i].y);
            add_edge(e[i].x,e[i].y);
            add_edge(e[i].y,e[i].x);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        for(int i=1;i<n;i++)
            if(id[e[i].x]<id[e[i].y]) swap(e[i].x,e[i].y);
        build(1,1,n);
        scanf("%d",&m);
        while(m--)
        {
            int op,x,y;
            scanf("%d%d",&op,&x);
            if(op==1){
                update(1,id[e[x].x],1);
            }
            else if(op==2){
                update(1,id[e[x].x],2);
            }
            else{
                scanf("%d",&y);
                printf("%d\n",Yougth(x,y));
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值