305514: CF1044F. DFS
Memory Limit:512 MB
Time Limit:6 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
DFS
题意翻译
题意:给一颗有n个结点的初始树,然后有q次操作,每次操作一对点,如果这对点有边,就删除边(保证不删除初始的树边),否则,就加一条边,接下来你可以从某个点dfs搜索,如果搜索出来的边和初始树的边一样,那这个点就是好的点,求每次操作后有多少个好的点。 输入:先读入n,q,下面n-1行每行两个数x,y表示结点x,结点y之间有一条边,然后q行,每行两个数u,v,表示给结点u,结点v进行一次操作。 输出:q行,每行一个数,第i(1<=i<=q)行表示第i行的好点个数。题目描述
Let $ T $ be a tree on $ n $ vertices. Consider a graph $ G_0 $ , 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 $ u_i $ and $ v_i $ . For every $ i $ from $ 1 $ to $ q $ , we define the graph $ G_i $ as follows: - If $ G_{i-1} $ contains an edge $ \{u_i, v_i\} $ , then remove this edge to form $ G_i $ . - Otherwise, add this edge to $ G_{i-1} $ to form $ G_i $ . Formally, $ G_i := G_{i-1} \triangle \{\{u_i, v_i\}\} $ where $ \triangle $ denotes the set [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference). Furthermore, it is guaranteed that $ T $ is always a subgraph of $ G_i $ . 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.输入输出格式
输入格式
The first line contains two integers $ n $ and $ q $ ( $ 3 \le n \le 2\cdot 10^5 $ , $ 1 \le q \le 2 \cdot 10^5 $ ) — the number of nodes and the number of updates, respectively. Each of the next $ n-1 $ lines contains two integers $ u $ and $ v $ ( $ 1 \le u, v \le n $ , $ u \ne 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 \le u, v \le n $ , $ u \ne v $ ) — the endpoints of the edge that is added or removed. It is guaranteed that this edge does not belong to $ T $ .
输出格式
For each update, print one integer $ k $ — the number of good vertices $ w $ after the corresponding update.
输入输出样例
输入样例 #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