Logo
(追記) (追記ここまで)

32744번 - Tree With One Edge 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB999100.000%

문제

Alice and Bob are playing a game on a tree rooted at node 1. A token is placed on the root then the players take turns making moves with Alice moving first. During a move a player must move the token from the node its on to one of that node's children. If there are no legal moves available, then the player whose turn it is will lose.

Since Alice and Bob are too good at this game they decided to play a modified version of the game. Before the game begins, Alice can add a single directed edge $(u, v)$ to the tree. Then, during the game, if the token is on vertex $u$ and the extra edge is present, the current player can choose to move the token to vertex $v$ and delete the extra edge (preventing it from being used multiple times in one game).

Of the $n^2$ possible pairs $(u, v)$ Alice can choose, how many will allow Alice to win the game assuming both players play optimally? Note that $u=v$ is allowed, as are $(u, v)$ pairs that match an existing edge in the tree (in either direction).

입력

The first line of the input contains a single integer $t$ (1ドル\le t\le 10^4$) --- the number of test cases.

The first line of each test case contains a single integer $n$ (2ドル\leq n\leq 2\cdot 10^5$) --- the number of vertices in the tree.

The next line of each test case contains $n-1$ integers $p_2,,円p_3,,円\ldots,,円p_n$ (1ドル \le p_i < i$) --- the parents of each vertex in the tree, except the root.

It is guaranteed that the sum of $n$ over all test cases does not exceed 2ドル\cdot 10^5$.

출력

For each test case, print a single integer --- the number of $(u, v)$ pairs that will allow Alice to win, assuming both players play optimally.

제한

예제 입력 1

4
3
1 2
6
1 1 3 3 5
6
1 2 3 4 5
5
1 1 1 1

예제 출력 1

4
33
27
25

힌트

For the first test test case, here are the 4ドル$ edges Alice can add to give herself the winning strategy:

출처

University > Rutgers University > Rutgers Programming Contest Fall 2024 K번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /