시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
5 초 128 MB 96 20 13 34.211%

문제

n개의 정점으로 이루어진 루트가 있는 트리가 주어진다. 각 정점에는 1번부터 n번까지 번호가 매겨져 있다. 같은 번호를 가지는 정점은 없고, 힙을 형성하고 있다. 즉, 각 정점에 붙여있는 숫자는 부모의 정점에 붙어잇는 숫자보다 작다. 이렇게 번호를 붙이는 방법의 수를 구하는 프로그램을 작성하시오. 이 수는 매우 클 수 있기 때문에, m으로 나눈 나머지를 출력한다.

입력

입력의 첫째 줄에는 테스트 케이스의 개수 t (t ≤ 250)가 주어진다. 각 테스트 케이스의 첫째 줄에는 트리의 크기 n (1 ≤ n ≤ 500000)과 m (2 ≤ m ≤ 109)이 주어진다. 다음 n-1개 줄의 i번째 줄에는 i+1번 정점의 부모의 번호 p(i+1)가 주어진다. (1 ≤ p(i+1) ≤ i) 1번 정점은 항상 트리의 루트이다. 입력의 크기는 50MB를 넘지 않는다.

출력

각 테스트 케이스에 대해서, 입력으로 주어지는 조건을 만족하는 힙의 개수를 출력한다.

예제 입력 1

4
3 1000000
1
1
4 1000000
1
1
1
5 1000000
1
2
3
4
5 1000000
1
1
3
3

예제 출력 1

2
6
1
8

힌트

마지막 예제의 경우에 아래와 같이 8가지가 가능하다.

W3sicHJvYmxlbV9pZCI6Ijc4ODkiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWQ3OTkgXHVjMTM4XHVhZTMwIiwiZGVzY3JpcHRpb24iOiI8cD5uXHVhYzFjXHVjNzU4IFx1YzgxNVx1YzgxMFx1YzczY1x1Yjg1YyBcdWM3NzRcdWI4ZThcdWM1YjRcdWM5YzQgXHViOGU4XHVkMmI4XHVhYzAwIFx1Yzc4OFx1YjI5NCBcdWQyYjhcdWI5YWNcdWFjMDAgXHVjOGZjXHVjNWI0XHVjOWM0XHViMmU0LiBcdWFjMDEgXHVjODE1XHVjODEwXHVjNWQwXHViMjk0IDFcdWJjODhcdWJkODBcdWQx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lRoZSA4IHBvc3NpYmxlIGxhYmVsbGluZ3MgZnJvbSB0aGUgbGFzdCBleGFtcGxlIHRlc3QgY2FzZSBhcmUgYXMgZm9sbG93czo8XC9wPlxyXG5cclxuPHA+PGltZyBhbHQ9XCJcIiBzcmM9XCJcL3VwbG9hZFwvaW1hZ2VzXC9jaGVhcC5wbmdcIiBzdHlsZT1cImhlaWdodDoyMDdweDsgd2lkdGg6NDgwcHhcIiBcLz48XC9wPlxyXG4iLCJvcmlnaW5hbCI6IjEiLCJwcm9ibGVtX2xhbmdfY29kZSI6Ilx1YzYwMVx1YzViNCJ9XQ==

출처

ACM-ICPC > Regionals > Europe > Central European Regional Contest > CERC 2008 I번

  • 문제의 오타를 찾은 사람: bachjs
  • 문제를 번역한 사람: baekjoon