Skip to content

Instantly share code, notes, and snippets.

@tice0-2
Created October 29, 2018 03:45
Show Gist options
  • Save tice0-2/e40781db4deda000c555f083d5b99bd6 to your computer and use it in GitHub Desktop.
Save tice0-2/e40781db4deda000c555f083d5b99bd6 to your computer and use it in GitHub Desktop.
SEERC Divide and Conquer

first conclusion: no more than 3 edges are removed

Proof: by contra, assume otherwise, implying every degree of node >= 4; then there exists >= 2n edges in the graph. Because this graph is composed of 2 trees, then only 2n - 2 edges. Contradiction.

Then if we have the # of edges to remove as 2 <= e <= 3, we can reach the conclusion that both edges from A and edges from B are removed. Then if e == 2, it must be true that one A edge is removed, one B edge is removed. If e == 3, then either A or B must only have one edge removed.

By that, observe that if we treat one tree (WLOG Tree A with only 1 edge removed) as our main tree, it is equivalent to slice this tree into two halves, and calculating the # of edges from tree B that crosses the two sides of tree A.

where the red line is where to cut, and the # of removed edges is 1 (from A) + 2 = 3.

The final problem is: how to solve for the # of crossing edges from tree B, given the two parts of tree A?

Then we use 树上差分(no idea how to translate this, it is like binary lifting, LCA, etc. I am not sure). One of the 2 parts of A must be A's subtree, which means we can use DFS.

Moreover, it does not change the result if the two points connected by B's edge belong to part of A.

Thus we do the following operations on edges of B: (formula not copied). Then we only need to count A's subtrees s. t. it has sum of mk (honestly I don't understand the original Chinese sentence), then we can know how many edges of tree B belongs to the two parts connecting tree A.

Note

If e == 2, we just output the # of ways to do it. If e == 3, we need to exchange A and B, and recalculate the # of ways, because if e == 3, # of ways might increase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment