CF165D.Beard Graph

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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.

我们定义一个包含 nn 个顶点和 n−1n-1 条边的无向连通图称为“胡须图”(beard),当且仅当其中至多有一个顶点的度数大于 22(即其余所有顶点的度数均为 11 或 22)。请回顾:一个顶点的度数是指与其相连的边的数量。

每条边被染成黑色或白色。初始时,所有边均为黑色。

你将获得该胡须图的描述。你的任务是处理以下三类查询:

  • 将第 ii 条边染为黑色。此处“第 ii 条边”指输入描述中编号为 ii 的边。保证执行该操作时,第 ii 条边当前为白色;
  • 将第 ii 条边染为白色。保证执行该操作时,第 ii 条边当前为黑色;
  • 求仅经过黑色边时,顶点 aa 到顶点 bb 的最短路径长度;若二者之间不存在仅由黑色边构成的路径,则输出不存在。路径长度定义为路径所含边的数量。

顶点编号为 11 至 nn 的整数,边编号为 11 至 n−1n-1 的整数。

输入格式

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 v__i, u__i (1 ≤ v__i, u__i ≤ n, v__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 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.

输入的第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5)—— 图中顶点的数量。接下来的 n−1n-1 行描述了边,每行包含两个顶点编号 viv_i、uiu_i(1≤vi,ui≤n1 \leq v_i, u_i \leq n,且 vi≠uiv_i \neq u_i),表示该边所连接的两个顶点。保证给定图是连通的,构成一棵“胡须图”(beard graph),且不含自环或重边。

接下来一行包含一个整数 mm(1≤m≤3⋅1051 \leq m \leq 3 \cdot 10^5)—— 请求的数量。随后 mm 行为请求,格式如下:每行首先是一个整数 typetype,其取值范围为 11 到 33,表示请求类型。

若 type=1type = 1,则当前请求为将某条边涂黑。此时,除 typetype 外,该行还需包含一个整数 idid(1≤id≤n−11 \leq id \leq n-1),表示待涂黑边的编号。

若 type=2type = 2,则当前请求为将某条边涂白,其格式与上一请求类似。

若 type=3type = 3,则当前请求为查询距离。此时,除 typetype 外,该行还需包含两个整数 aa、bb(1≤a,b≤n1 \leq a, b \leq n,且 aa 可等于 bb)—— 表示需计算它们之间距离的两个顶点的编号。

所有行中的数字均以恰好一个空格分隔。边的编号按其在输入中出现的顺序依次为 11 到 n−1n-1。

输出格式

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.

对于每个“求顶点 a 与顶点 b 之间的距离”的查询,请输出对应结果。若顶点 a 与顶点 b 之间不存在仅由黑色边构成的路径,则输出 -1(不带引号)。请按查询接收的顺序输出结果,各数字之间用空格或换行符分隔。

输入输出样例

  • 输入#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 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 和 2 由编号为 1 的边相连,顶点 2 和 3 由编号为 2 的边相连。在将编号为 2 的边重涂色之前,任意两个顶点之间均存在一条仅由黑边构成的路径。具体而言,顶点 1 与 3 之间的最短路径恰好经过这两条边。

若我们将编号为 2 的边涂成白色,则顶点 3 将与其他顶点断开连接,即不存在一条仅由黑边构成的、从顶点 3 到其他任意顶点的路径。

输入解题思路,AI测评打分。不知道怎么写?

首页