Tree With One Edge

시간 제한1초메모리 제한1024 MB

요약
루트 트리에서 앨리스가 한 번만 쓸 수 있는 유향 간선 (u,v)를 하나 추가할 때, 토큰을 리프로 내려보내는 게임에서 앨리스가 이기는 쌍의 수를 센다.
난이도

어려움10점 중 8점

유형
트리, 게임 이론, 동적 계획법, DFS
정답자
아직 제출이 없습니다

문제

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)(u, v) to the tree. Then, during the game, if the token is on vertex uu and the extra edge is present, the current player can choose to move the token to vertex vv and delete the extra edge (preventing it from being used multiple times in one game).

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

입력

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

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

The next line of each test case contains n−1n-1 integers p_2,,p_3,,…,,p_np\_2,\\,p\_3,\\,\ldots,\\,p\_n (1≤p_i<i1 \le p\_i < i) --- the parents of each vertex in the tree, except the root.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

출력

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

힌트

For the first test test case, here are the 44 edges Alice can add to give herself the winning strategy:

예제1

  1. 예제 1

    입력
    4
    3
    1 2
    6
    1 1 3 3 5
    6
    1 2 3 4 5
    5
    1 1 1 1
    
    예상 출력
    4
    33
    27
    25