Official

C - Swap on Tree Editorial by evima


Let’s consider when the sequences of \(a\) obtained by two sequences of operations are different.

First, consider the case where the sets of edges used in the two sequences of operations are different. Let \(e\) be an edge used only in one of the sequences. Let \(T_1\) and \(T_2\) be the two trees formed by removing \(e\) from the input tree \(T\). In the sequence of \(a\) obtained without using \(e\), the set of vertex numbers contained in \(T_1\) matches the set of \(a_n\) corresponding to the vertices in \(T_1\), but this is not the case in the sequence obtained by using \(e\). Therefore, we see that different sequences are obtained when the sets of edges used are different.

Assume that the sets of edges used in the two sequences of operations are the same. Then, the following two conditions are equivalent:

  1. The sequences obtained by the two sequences of operations match.
  2. For all vertices \(v\), the relative order of use of the edges incident to \(v\) is the same in the two sequences of operations.

(Proof) Hereinafter, when referring to “edges” in the spanning subgraph \(G(V, E)\) of the input tree \(T\), where \(E\) is the set of edges used, it means the edges existing in \(G(V, E)\).

(\(1. \to 2.\)) We take the contrapositive. If condition 2 does not hold, there is a vertex \(v\) such that the relative order of use of the edges incident to \(v\) differs between the two sequences of operations. Let the edges incident to \(v\) be \(e_1, e_2, \dots, e_k\), and let the endpoint of \(e_i\) that is not \(v\) be \(u_i\). Let \(C_i\) be the connected component containing \(u_i\) when \(v\) is removed from the connected component containing \(v\).
Without loss of generality, we assume that the edges incident to \(v\) are used in the order \(e_1, e_2, \dots, e_k\) in one of the sequences of operations. In this case, by performing operations according to that sequence, the following state is achieved:

  • Piece \(v\) is on a vertex in \(C_1\).
  • The pieces corresponding to the vertex numbers in \(C_1\) are all on \(C_1\) except for one piece on \(C_2\).
  • The pieces corresponding to the vertex numbers in \(C_2\) are all on \(C_2\) except for one piece on \(C_3\).
  • \(\vdots\)
  • The pieces corresponding to the vertex numbers in \(C_{k-1}\) are all on \(C_{k-1}\) except for one piece on \(C_k\).
  • The pieces corresponding to the vertex numbers in \(C_k\) are all on \(C_k\) except for one piece on \(v\).

When the relative order of use of the edges incident to \(v\) differs, it can be confirmed that the relative relationships regarding \(v, C_1, C_2, \dots, C_k\) as described above do not match. Thus, the claim is shown.

(\(2. \to 1.\)) We prove by induction on the number of edges. The proposition clearly holds when there are zero edges. Assuming the proposition holds when there are \(M\) edges, we consider what happens when there are \(M+1\) edges.
Here, we can confirm that there is at least one edge \(e\) that satisfies the following condition:

  • Let \(u\) and \(v\) be the endpoints of \(e\). The edge that comes last in the relative order of use of the edges incident to \(u\) and the edge that comes last in the relative order of use of the edges incident to \(v\) are both \(e\).
    • (Proof) In \(G(V, E)\), for every vertex \(w\) with incident edges, paint red the edge that comes last in the relative order of use of the edges incident to \(w\). Since \(G(V, E)\) is a forest, the number of vertices with degrees at least \(1\) is greater than the number of edges, so there is at least one edge painted red more than once by the pigeonhole principle. (End of proof)

Now, let’s assume there are two sequences of operations \(S_1\) and \(S_2\) that satisfy condition \(2.\) with \(M+1\) edges. From here, we take one edge \(e\) that satisfies the above condition and consider the sequences of operations \(S'_1\) and \(S'_2\) obtained by removing \(e\) from \(S_1\) and \(S_2\). Then, it can be proven from the induction process that the sequences obtained from \(S'_1\) and \(S'_2\) match. Also, when \(u\) and \(v\) are the endpoints of \(e\), the sequence obtained from \(S_1\) is the sequence obtained from \(S'_1\) with \(a_u\) and \(a_v\) swapped. Therefore, the sequences obtained from \(S_1\) and \(S_2\) match.

Thus, the proposition is proven. (End of proof)

Furthermore, it can be proven that when the relative order of use of the edges incident to each vertex \(v\) is arbitrarily fixed, there is at least one corresponding sequence of operations.

  • (Proof) For a fixed order, it can be proven similarly to the proof of \(2. \to 1.\) that there is always an edge \(e\) that comes first in the relative order of use of the incident edges at both endpoints. Therefore, by greedily repeating the use of the edge that comes first in the relative order of unused edges at both endpoints, it is possible to find one sequence of operations for the fixed order. (End of proof)

Therefore, we see that the number of sequences obtained for a fixed set of used edges equals the product of \(\deg(v)!\), the numbers of ways to assign orders for vertices \(v\). Thus, the answer is the sum of \(\prod_{v \in V} \deg(v)!\) for all spanning subgraphs. This can be calculated in \(\mathrm{O}(N^2)\) by DP (or \(\mathrm{O}(N \log^2 N)\) with optimization using convolution).

posted:
last update: