Codeforces Round #112 (Div. 2) D. Beard Graph

地址:http://codeforces.com/problemset/problem/165/D

题目:

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 integers ab (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.

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.

思路: 树链剖分模板题。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=2e5+7;
12 const int mod=1e9+7;
13 
14 vector<int>mp[K];
15 int top[K],sz[K],fa[K],son[K],id[K],hid[K],deep[K];
16 int cnt,sum[4*K];
17 PII edge[K];
18 int query(int o,int l,int r,int nl,int nr)
19 {
20     if(l==nl&&r==nr)    return sum[o];
21     int mid=l+r>>1;
22     if(nr<=mid) return query(o<<1,l,mid,nl,nr);
23     if(nl>mid) return query(o<<1|1,mid+1,r,nl,nr);
24     return query(o<<1,l,mid,nl,mid)+query(o<<1|1,mid+1,r,mid+1,nr);
25 }
26 int update(int o,int l,int r,int x,int v)
27 {
28     if(l==r)
29        return sum[o]=v;
30     int mid=l+r>>1;
31     if(x<=mid) update(o<<1,l,mid,x,v);
32     else if(x>mid) update(o<<1|1,mid+1,r,x,v);
33     return sum[o]=sum[o<<1]+sum[o<<1|1];
34 }
35 void dfs1(int x,int f)
36 {
37     sz[x]=1,fa[x]=f,son[x]=-1,deep[x]=deep[f]+1;
38     for(int i=0;i<mp[x].size();i++)
39     if(mp[x][i]!=f)
40     {
41         dfs1(mp[x][i],x);
42         sz[x]+=sz[mp[x][i]];
43         if(son[x]==-1||sz[son[x]]<sz[mp[x][i]])
44             son[x]=mp[x][i];
45     }
46 }
47 void dfs2(int x,int f)  ///每条边用深度低的节点的序号表示
48 {
49     top[x]=f,id[x]=++cnt,hid[id[x]]=x;
50     if(son[x]!=-1) dfs2(son[x],f);
51     for(int i=0;i<mp[x].size();i++)
52     if(mp[x][i]!=fa[x]&&mp[x][i]!=son[x])
53         dfs2(mp[x][i],mp[x][i]);
54 }
55 void tree_update(int x,int y,int v)
56 {
57     if(deep[x]<deep[y]) x=y;
58     update(1,1,cnt,id[x],v);
59 }
60 int tree_query(int x,int y)
61 {
62     int ret=0;
63     while(top[x]!=top[y])
64     {
65         if(deep[top[x]]<deep[top[y]]) swap(x,y);
66         ret+=deep[x]-deep[top[x]]+1;
67         if(query(1,1,cnt,id[top[x]],id[x])) return -1;
68         x=fa[top[x]];
69     }
70     if(x==y) return ret;
71     if(deep[x]>deep[y]) swap(x,y);
72     ret+=deep[y]-deep[son[x]]+1;
73     if(query(1,1,cnt,id[son[x]],id[y])) ret=-1;
74     return ret;
75 }
76 int main(void)
77 {
78     int n,q;
79     while(~scanf("%d",&n))
80     {
81         for(int i=1,x,y;i<n;i++)
82             scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x),edge[i]=MP(x,y);
83         dfs1(1,0),dfs2(1,1);
84         scanf("%d",&q);
85         int op,x,y;
86         while(q--)
87         {
88             scanf("%d%d",&op,&x);
89             if(op==3)
90                 scanf("%d",&y),printf("%d\n",tree_query(x,y));
91             else if(op==1)
92                 tree_update(edge[x].first,edge[x].second,0);
93             else
94                 tree_update(edge[x].first,edge[x].second,1);
95         }
96     }
97     return 0;
98 }

 

posted @ 2017-05-17 22:05  weeping  阅读(189)  评论(0编辑  收藏  举报