CF165D.Beard Graph

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let's define a non-oriented connected graph of nn vertices and n1n-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 ii black. The edge number ii is the edge that has this number in the description. It is guaranteed that by the moment of this request the ii -th edge is white
  • paint the edge number ii white. It is guaranteed that by the moment of this request the ii -th edge is black
  • find the length of the shortest path going only along the black edges between vertices aa and bb 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 11 to nn , and the edges are numbered with integers from 11 to n1n-1 .

输入格式

The first line of the input contains an integer nn ( 2<=n<=1052<=n<=10^{5} ) — the number of vertices in the graph. Next n1n-1 lines contain edges described as the numbers of vertices viv_{i} , uiu_{i} ( 1<=vi,ui<=n1<=v_{i},u_{i}<=n , viuiv_{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 mm ( 1<=m<=31051<=m<=3·10^{5} ) — the number of requests. Next mm lines contain requests in the following form: first a line contains an integer typetype , which takes values ​​from 11 to 33 , and represents the request type.

If type=1type=1 , then the current request is a request to paint the edge black. In this case, in addition to number typetype the line should contain integer idid ( 1<=id<=n11<=id<=n-1 ), which represents the number of the edge to paint.

If type=2type=2 , then the current request is a request to paint the edge white, its form is similar to the previous request.

If type=3type=3 , then the current request is a request to find the distance. In this case, in addition to typetype , the line should contain two integers aa , bb ( 1<=a,b<=n1<=a,b<=n , aa can be equal to bb ) — 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 aa and bb " print the result. If there is no path going only along the black edges between vertices aa and bb , 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

    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
    1
    1
    -1
    -1
    
  • 输入#2

    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

    3
    -1
    3
    2
    

说明/提示

In the first sample vertices 11 and 22 are connected with edge number 11 , and vertices 22 and 33 are connected with edge number 22 . Before the repainting edge number 22 each vertex is reachable from each one along the black edges. Specifically, the shortest path between 11 and 33 goes along both edges.

If we paint edge number 22 white, vertex 33 will end up cut off from other vertices, that is, no path exists from it to any other vertex along the black edges.

首页