Tree Decorations

시간 제한2초메모리 제한2048 MB

요약
M개의 초록 노드로 시작한 루트 트리에 미지의 루트 트리 D의 각 부분 트리 복사본을 붙여 만든 최종 트리가 주어질 때, 가능한 D의 구조적 가짓수를 센다.
난이도

어려움10점 중 9점

유형
트리, 동적 계획법, 해시맵, 조합론
정답자
아직 제출이 없습니다

문제

Mateo recently found the perfect decorations for his Christmas tree — more trees!

Specifically, his Christmas tree is a rooted tree TT initially with MM nodes, all painted green. He has another rooted tree DD that he uses as a reference for his decorations. Mateo uses the following process to put on all of his decorations:

  • For each node ii in DD, he creates a copy of the subtree rooted at ii. Let this copy be C_iC\_i. Then, he paints the nodes of C_iC\_i red. Finally, he chooses some green node in TT to be the parent of the root of C_iC\_i by connecting them with an edge.

After applying all the decorations, TT ends up containing NN nodes. Unfortunately, he realized that he had forgotten to record what DD is! To make things worse, he accidentally spilled water on TT, washing off all the colour from the nodes. After all that, he labels the root of TT as 11, and then labels the rest of the nodes from 22 to NN.

The only information he currently has is the final state of TT, as well as MM. Help him find the number of possible DD that he could have started with, where two possibilities are considered different if they are structurally distinct.

Rooted trees AA and BB are said to be structurally identical if and only if they have the same number of nodes SS, and there is a way to label AA’s nodes from 11 to SS and BB’s nodes from 11 to SS such that:

  • Their roots are labeled the same.
  • Nodes labeled xx and yy in AA are connected by an edge if and only if nodes labeled xx and yy in BB are connected by an edge.

Otherwise, AA and BB are considered structurally distinct.

입력

The first line of input contains two space-separated integers NN and MM.

The next N−1N − 1 lines each contain two space-separated integers u_iu\_i and v_iv\_i (1≤u_i,v_i≤N1 ≤ u\_i , v\_i ≤ N, u_i≠v_iu\_i \ne v\_i), describing an edge in TT connecting nodes u_iu\_i and v_iv\_i. Note that TT is rooted at node 11.

출력

Output the number of possible DD that he could have started with, where two possibilities are considered different if they are structurally distinct.

예제2

  1. 예제 1

    입력
    8 3
    1 2
    1 3
    1 4
    2 5
    2 6
    3 7
    3 8
    
    예상 출력
    1
    
  2. 예제 2

    입력
    14 5
    1 2
    1 3
    3 4
    3 5
    1 6
    6 7
    7 8
    7 9
    2 10
    10 11
    10 12
    10 13
    10 14
    
    예상 출력
    2