B - Three Vertices Editorial by evima Editorial for printing
Consider two distinct \(3\)-vertex paths \(P_1,P_2\) that share exactly \(2\) vertices. Let \(x\) be the vertex contained only in \(P_1\), and \(y\) the vertex contained only in \(P_2\). Then, by performing the operation on each of \(P_1\) and \(P_2\), we can flip the colors of only the two vertices \(x,y\). Let us call two vertices in such a relation a “good pair.”
Let \(H\) be the graph with the same vertex set as \(G\) in which every good pair in \(G\) is connected by an edge. By repeatedly flipping good pairs, we can flip any two vertices in the same connected component of \(H\). Therefore, if the number of black vertices in a connected component of \(H\) is even, all vertices in that component can be made white.
No matter which \(3\)-vertex path of \(G\) is chosen, the combination of connected components of \(H\) to which its \(3\) vertices belong is the same, counting multiplicity.
Suppose the combination of components changes when we move to another \(3\)-vertex path by replacing one vertex. Then the two exchanged vertices must belong to different components. However, these two vertices form a good pair and thus belong to the same component, which is a contradiction. Since \(G\) is connected, any two \(3\)-vertex paths can be linked by successively following paths that share an edge.
Every vertex is contained in some \(3\)-vertex path, and the combination of components does not depend on the path, so every path intersects all components of \(H\). Hence, there are at most \(3\) components, and the breakdown of any \(3\)-vertex path is one of the following.
- All \(3\) vertices belong to the same component.
- \(2\) vertices belong to the same component, and the remaining vertex belongs to another component.
- All \(3\) vertices belong to different components.
Every operation changes the parity of each component in the same way, so the parities after the operations are determined by whether the number of operations is even or odd. Thus, fixing any one \(3\)-vertex path \(P\), the coloring is good if and only if the number of black vertices in every component can be made even by flipping \(P\) zero times or once.
All that remains is to find the connected components of \(H\) used for this check. Using only the good pairs obtained from two paths that share an edge, we already get the same connected components as \(H\).
If the common vertices \(a,b\) of the paths forming a good pair \(x,y\) are adjacent, both paths can be taken to contain the edge \((a,b)\). If they are not adjacent, by following \((a,x,b)\to(x,a,y)\to(a,y,b)\), we can link \(x\) and \(y\) through exchanges between paths sharing an edge.
Corresponding to the cases where two paths sharing an edge have the same center and different centers, perform the following merges with Union-Find.
- For each vertex of degree \(3\) or greater, choose one adjacent vertex and merge all the other adjacent vertices into it.
- For each edge \((u,v)\) whose endpoints both have degree \(2\) or greater, choose one vertex adjacent to \(u\) other than \(v\) and one vertex adjacent to \(v\) other than \(u\), and merge them.
In step \(2\), even when \(u\) or \(v\) has degree \(3\) or greater and there are multiple candidates, all the candidates have already been merged in step \(1\), so it suffices to merge with any one of them.
Compute the parity of the number of black vertices in each component from the initial state, and for each query, flip only the parity of the component to which the specified vertex belongs. There are \(O(M)\) merges in total, and the check for each query takes \(O(1)\) time since it examines at most \(3\) components. The overall time complexity is \(O((N+M)\alpha(N)+Q)\).
posted:
last update: