CF1044F.DFS

省选/NOI-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Let TT be a tree on nn vertices. Consider a graph G0G_0, initially equal to TT. You are given a sequence of qq updates, where the ii-th update is given as a pair of two distinct integers uiu_i and viv_i.

For every ii from 11 to qq, we define the graph GiG_i as follows:

  • If Gi−1G_{i-1} contains an edge ui,vi{u_i, v_i}, then remove this edge to form GiG_i.
  • Otherwise, add this edge to Gi−1G_{i-1} to form GiG_i.

Formally, Gi:=Gi−1△ui,viG_i := G_{i-1} \triangle {{u_i, v_i}} where △\triangle denotes the set symmetric difference.

Furthermore, it is guaranteed that TT is always a subgraph of GiG_i. In other words, an update never removes an edge of TT.

Consider a connected graph HH and run a depth-first search on it. One can see that the tree edges (i.e. the edges leading to a not yet visited vertex at the time of traversal) form a spanning tree of the graph HH. This spanning tree is not generally fixed for a particular graph — it depends on the starting vertex, and on the order in which the neighbors of each vertex are traversed.

We call vertex ww good if one can order the neighbors of each vertex in such a way that the depth-first search started from ww produces TT as the spanning tree. For every ii from 11 to qq, find and report the number of good vertices.

设 TT 是一个包含 nn 个顶点的树。考虑图 G0G_0,其初始值等于 TT。给定一个长度为 qq 的更新序列,其中第 ii 个更新以一对互异的整数 uiu_i 和 viv_i 给出。

对每个从 11 到 qq 的 ii,我们按如下方式定义图 GiG_i:

  • 若 Gi−1G_{i-1} 中已存在边 {ui,vi}\{u_i, v_i\},则将该边从 Gi−1G_{i-1} 中删除,得到 GiG_i;
  • 否则,将该边加入 Gi−1G_{i-1},得到 GiG_i。

形式化地,Gi:=Gi−1△{ui,vi}G_i := G_{i-1} \triangle \{u_i, v_i\},其中 △\triangle 表示集合的对称差。

此外,题目保证 TT 始终是 GiG_i 的子图。换言之,任意一次更新均不会删除 TT 中的边。

考虑一个连通图 HH 并在其上执行深度优先搜索(DFS)。可以观察到:树边(即在遍历过程中通向尚未访问过的顶点的边)构成图 HH 的一棵生成树。该生成树通常并非由图 HH 唯一确定——它依赖于 DFS 的起始顶点,以及每个顶点的邻居被遍历的顺序。

若存在一种方式,使得对每个顶点的邻居指定某种遍历顺序,并从顶点 ww 出发执行 DFS,所得生成树恰好为 TT,则称顶点 ww 是好的(good)。

对每个从 11 到 qq 的 ii,求出并报告此时“好”顶点的个数。

输入格式

The first line contains two integers nn and qq (3≤n≤2⋅1053 \le n \le 2\cdot 10^5, 1≤q≤2⋅1051 \le q \le 2 \cdot 10^5) — the number of nodes and the number of updates, respectively.

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — vertices connected by an edge in TT. It is guaranteed that this graph is a tree.

Each of the next qq lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — the endpoints of the edge that is added or removed. It is guaranteed that this edge does not belong to TT.

第一行包含两个整数 nn 和 qq(3≤n≤2⋅1053 \le n \le 2\cdot 10^5,1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)—— 分别表示节点数量和更新操作次数。

接下来的 n−1n-1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v)—— 表示树 TT 中由一条边连接的两个顶点。保证该图是一棵树。

再接下来的 qq 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v)—— 表示被添加或删除的边的两个端点。保证该边不属于 TT。

输出格式

For each update, print one integer kk — the number of good vertices ww after the corresponding update.

每次更新后,输出一个整数 kk —— 表示对应更新后好顶点 ww 的数量。

输入输出样例

  • 输入#1

    3 2
    1 2
    1 3
    2 3
    3 2

    输出#1

    2
    3
  • 输入#2

    6 6
    1 2
    2 3
    1 4
    4 5
    1 6
    2 5
    3 4
    5 2
    6 4
    3 4
    6 5

    输出#2

    3
    2
    3
    2
    3
    2

说明/提示

The first sample is depicted in the following figure.

After the first update, GG contains all three possible edges. The result of a DFS is as follows:

  • Let the starting vertex be 11. We have two choices of ordering the neighbors of 11, either [2,3][2, 3] or [3,2][3, 2].
    • If we choose the former, then we reach vertex 22. Regardless of the ordering of its neighbors, the next visited vertex will be 33. Thus, the spanning tree generated by this DFS will contain edges 1,2{1, 2} and 2,3{2, 3}, which does not equal to TT.
    • If we choose the latter, we obtain a spanning tree with edges 1,3{1, 3} and 2,3{2, 3}.Hence, there is no way of ordering the neighbors of vertices such that the DFS produces TT, and subsequently 11 is not a good vertex.
  • Let the starting vertex be 22. We have two choices of traversing its neighbors. If we visit 33 first, then the spanning tree will consist of edges 2,3{2,3} and 1,3{1,3}, which is not equal to TT. If we, however, visit 11 first, then we can only continue to 33 from here, and the spanning tree will consist of edges 1,2{1, 2} and 1,3{1,3}, which equals to TT. Hence, 22 is a good vertex.
  • The case when we start in the vertex 33 is symmetrical to starting in 22, and hence 33 is a good vertex.

Therefore, the answer is 22.

After the second update, the edge between 22 and 33 is removed, and G=TG = T. It follows that the spanning tree generated by DFS will be always equal to TT independent of the choice of the starting vertex. Thus, the answer is 33.

In the second sample, the set of good vertices after the corresponding query is:

  • 2,3,5{2, 3, 5}
  • 3,5{3, 5}
  • 3,4,5{3, 4, 5}
  • 4,5{4, 5}
  • 4,5,6{4, 5, 6}
  • 5,6{5, 6}

第一个样例如下图所示。

第一次更新后,图 GG 包含全部三条可能的边。一次 DFS 的结果如下:

  • 设起始顶点为 11。对于顶点 11 的邻接点,我们有两种排序方式:[2,3][2, 3] 或 [3,2][3, 2]。
    • 若选择前者,则首先访问顶点 22;无论其邻接点如何排序,下一个被访问的顶点均为 33。因此,该 DFS 生成的生成树包含边 {1,2}\{1, 2\} 和 {2,3}\{2, 3\},不等于 TT。
    • 若选择后者,则生成树包含边 {1,3}\{1, 3\} 和 {2,3}\{2, 3\}。因此,不存在一种对各顶点邻接点的排序方式,使得 DFS 生成的生成树恰好为 TT,故 11 不是好顶点。
  • 设起始顶点为 22。我们有两种遍历其邻接点的顺序。若先访问 33,则生成树包含边 {2,3}\{2, 3\} 和 {1,3}\{1, 3\},不等于 TT;若先访问 11,则只能从 11 继续访问 33,此时生成树包含边 {1,2}\{1, 2\} 和 {1,3}\{1, 3\},恰等于 TT。因此,22 是好顶点。
  • 起始顶点为 33 的情况与起始顶点为 22 对称,故 33 也是好顶点。

因此,答案为 22。

第二次更新后,边 (2,3)(2, 3) 被移除,此时 G=TG = T。于是,无论选择哪个顶点作为起始点,DFS 生成的生成树恒为 TT。因此,答案为 33。

第二个样例中,每次查询后的好顶点集合分别为:

  • {2,3,5}\{2, 3, 5\}
  • {3,5}\{3, 5\}
  • {3,4,5}\{3, 4, 5\}
  • {4,5}\{4, 5\}
  • {4,5,6}\{4, 5, 6\}
  • {5,6}\{5, 6\}

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

首页