CF1044F.DFS
省选/NOI-
通过率:0%
时间限制:6.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let T be a tree on n vertices. Consider a graph G0, initially equal to T. You are given a sequence of q updates, where the i-th update is given as a pair of two distinct integers ui and vi.
For every i from 1 to q, we define the graph Gi as follows:
- If Gi−1 contains an edge ui,vi, then remove this edge to form Gi.
- Otherwise, add this edge to Gi−1 to form Gi.
Formally, Gi:=Gi−1△ui,vi where △ denotes the set symmetric difference.
Furthermore, it is guaranteed that T is always a subgraph of Gi. In other words, an update never removes an edge of T.
Consider a connected graph H 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 H. 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 w good if one can order the neighbors of each vertex in such a way that the depth-first search started from w produces T as the spanning tree. For every i from 1 to q, find and report the number of good vertices.
设 T 是一个包含 n 个顶点的树。考虑图 G0,其初始值等于 T。给定一个长度为 q 的更新序列,其中第 i 个更新以一对互异的整数 ui 和 vi 给出。
对每个从 1 到 q 的 i,我们按如下方式定义图 Gi:
- 若 Gi−1 中已存在边 {ui,vi},则将该边从 Gi−1 中删除,得到 Gi;
- 否则,将该边加入 Gi−1,得到 Gi。
形式化地,Gi:=Gi−1△{ui,vi},其中 △ 表示集合的对称差。
此外,题目保证 T 始终是 Gi 的子图。换言之,任意一次更新均不会删除 T 中的边。
考虑一个连通图 H 并在其上执行深度优先搜索(DFS)。可以观察到:树边(即在遍历过程中通向尚未访问过的顶点的边)构成图 H 的一棵生成树。该生成树通常并非由图 H 唯一确定——它依赖于 DFS 的起始顶点,以及每个顶点的邻居被遍历的顺序。
若存在一种方式,使得对每个顶点的邻居指定某种遍历顺序,并从顶点 w 出发执行 DFS,所得生成树恰好为 T,则称顶点 w 是好的(good)。
对每个从 1 到 q 的 i,求出并报告此时“好”顶点的个数。
输入格式
The first line contains two integers n and q (3≤n≤2⋅105, 1≤q≤2⋅105) — the number of nodes and the number of updates, respectively.
Each of the next n−1 lines contains two integers u and v (1≤u,v≤n, u=v) — vertices connected by an edge in T. It is guaranteed that this graph is a tree.
Each of the next q lines contains two integers u and v (1≤u,v≤n, u=v) — the endpoints of the edge that is added or removed. It is guaranteed that this edge does not belong to T.
第一行包含两个整数 n 和 q(3≤n≤2⋅105,1≤q≤2⋅105)—— 分别表示节点数量和更新操作次数。
接下来的 n−1 行,每行包含两个整数 u 和 v(1≤u,v≤n,u=v)—— 表示树 T 中由一条边连接的两个顶点。保证该图是一棵树。
再接下来的 q 行,每行包含两个整数 u 和 v(1≤u,v≤n,u=v)—— 表示被添加或删除的边的两个端点。保证该边不属于 T。
输出格式
For each update, print one integer k — the number of good vertices w after the corresponding update.
每次更新后,输出一个整数 k —— 表示对应更新后好顶点 w 的数量。
输入输出样例
输入#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, G contains all three possible edges. The result of a DFS is as follows:
- Let the starting vertex be 1. We have two choices of ordering the neighbors of 1, either [2,3] or [3,2].
- If we choose the former, then we reach vertex 2. Regardless of the ordering of its neighbors, the next visited vertex will be 3. Thus, the spanning tree generated by this DFS will contain edges 1,2 and 2,3, which does not equal to T.
- If we choose the latter, we obtain a spanning tree with edges 1,3 and 2,3.Hence, there is no way of ordering the neighbors of vertices such that the DFS produces T, and subsequently 1 is not a good vertex.
- Let the starting vertex be 2. We have two choices of traversing its neighbors. If we visit 3 first, then the spanning tree will consist of edges 2,3 and 1,3, which is not equal to T. If we, however, visit 1 first, then we can only continue to 3 from here, and the spanning tree will consist of edges 1,2 and 1,3, which equals to T. Hence, 2 is a good vertex.
- The case when we start in the vertex 3 is symmetrical to starting in 2, and hence 3 is a good vertex.
Therefore, the answer is 2.
After the second update, the edge between 2 and 3 is removed, and G=T. It follows that the spanning tree generated by DFS will be always equal to T independent of the choice of the starting vertex. Thus, the answer is 3.
In the second sample, the set of good vertices after the corresponding query is:
- 2,3,5
- 3,5
- 3,4,5
- 4,5
- 4,5,6
- 5,6
第一个样例如下图所示。

第一次更新后,图 G 包含全部三条可能的边。一次 DFS 的结果如下:
- 设起始顶点为 1。对于顶点 1 的邻接点,我们有两种排序方式:[2,3] 或 [3,2]。
- 若选择前者,则首先访问顶点 2;无论其邻接点如何排序,下一个被访问的顶点均为 3。因此,该 DFS 生成的生成树包含边 {1,2} 和 {2,3},不等于 T。
- 若选择后者,则生成树包含边 {1,3} 和 {2,3}。因此,不存在一种对各顶点邻接点的排序方式,使得 DFS 生成的生成树恰好为 T,故 1 不是好顶点。
- 设起始顶点为 2。我们有两种遍历其邻接点的顺序。若先访问 3,则生成树包含边 {2,3} 和 {1,3},不等于 T;若先访问 1,则只能从 1 继续访问 3,此时生成树包含边 {1,2} 和 {1,3},恰等于 T。因此,2 是好顶点。
- 起始顶点为 3 的情况与起始顶点为 2 对称,故 3 也是好顶点。
因此,答案为 2。
第二次更新后,边 (2,3) 被移除,此时 G=T。于是,无论选择哪个顶点作为起始点,DFS 生成的生成树恒为 T。因此,答案为 3。
第二个样例中,每次查询后的好顶点集合分别为:
- {2,3,5}
- {3,5}
- {3,4,5}
- {4,5}
- {4,5,6}
- {5,6}
输入解题思路,AI测评打分。不知道怎么写?