luogu#P5043. 【模板】树同构 / [BJOI2015] 树的同构
【模板】树同构 / [BJOI2015] 树的同构
Problem Description
A tree is a very common data structure.
We call a connected undirected graph with vertices and edges a tree.
If we choose a vertex as the root and traverse from the root, then every other vertex has a predecessor. This tree becomes a rooted tree.
For two trees and , if we can relabel all vertices of so that and become exactly the same, then these two trees are isomorphic. That is, they have the same shape.
Now you are given unrooted trees. Please partition them into several equivalence classes according to the isomorphism relation.
Input Format
The first line contains an integer .
The next lines each contain several integers describing one tree. The first integer is , the number of vertices. Then follow integers, which in order give the parent vertex number of each vertex numbered from to . The parent number of the root vertex is .
Output Format
Output lines. Each line contains one integer, representing the smallest index among the trees that are isomorphic to the corresponding tree.
4
4 0 1 1 2
4 2 0 2 3
4 0 1 1 1
4 0 1 2 3
1
1
3
1
Hint
Trees numbered , , and are isomorphic. The tree numbered is only isomorphic to itself.
For of the testdata, it is guaranteed that .
Translated by ChatGPT 5