The 5th National Championship java test question 7: General G

General G


General G has a well-trained army. Except for General G, each soldier has a direct superior (may be other soldiers, or General G). Now General G will accept a special task. It is necessary to send some soldiers (at least one) to form a death squad. In order to increase the independence of the death squad members, it is required that if a soldier is in the death squad, his immediate superior cannot be in the death squad.
Excuse me, how many methods does General G have to dispatch death squads? Note that General G can also enter the death squad as a soldier.
Input format
The first line of input contains an integer n, which represents the number of troops including General G. The soldiers of the army are numbered from 1 to n, and General G is numbered 1.
The next n-1 numbers respectively represent the direct superior numbers of the soldiers numbered 2, 3, …, n, and the direct superior numbers of the soldiers numbered i are less than i.
Output format
Output an integer, indicating the number of plans to send out the death squad. Since the number may be large, you only need to output the remainder of this number divided by 10007.
Sample input 1
3
1 1
Sample output 1
4
Sample description
The four methods are:
choose 1;
choose 2;
choose 3;
choose 2, 3.
Sample input 2
7
1 1 2 2 3 3
Sample output 2
40
Data scale and convention
For 20% of data, n ≤ 20;
for 40% of data, n ≤ 100;
for 100% of data, 1 ≤ n ≤ 100000.
Method one
idea:
We can use dfs. Each soldier has two choices, whether to go or not. The condition for the end of the search is to traverse all the soldiers, and then get a solution, but the final result needs to be reduced by 1, because There is a plan that all soldiers do not go.
Procedure :

count=0
def dfs(nu):
    global count
    if n+1==nu:
        count+=1
        return
    if ids[k[nu]]==0:
        ids[nu]=0
        dfs(nu+1)
        ids[nu]=1
        dfs(nu+1)
    else:
        ids[nu] = 0
        dfs(nu + 1)
        ids[nu] = 1

n=int(input())
k=(list(map(int,input().split())))
ids=[i for i in range(0,n+1)]
k.insert(0,0)
k.insert(1,0)
dfs(1)
print((count-1)%10007)

Method 2
:
We can do it in the form of a dp graph, we can regard it as a tree, and then the input data is stored in an adjacency list, and dp[u][ is used to traverse all the vertices of the tree using dfs. 1],dp[u][1] means to go and not to go respectively, and then use the idea of ​​dynamic programming to count all times, when dp[u][1]=dp[u][1] dp[j][ 0] When dp[u][1] goes, his subordinates dp[j][0] don't go, and then multiply the number of times he went to him is equal to the total number of dp[u][1]. The opposite is dp[u][0]=dp[u][0] (dp[j][0]+dp[j][1]), because it is a recursive form and the final result is dp[1][0] +dp[1][1]-1, minus one indicates the possibility of not going to all.
Procedure :

n=int(input())
dp=[[0 for i in range(2) ]for i in range(n+1)] 

def dfs(u):
    dp[u][0]=dp[u][1]=1
    i=h[u]
    while i!=0:
        j=e[i]
        dfs(j)
        dp[u][1]=(dp[u][1]*dp[j][0])%10007
        dp[u][0]=(dp[u][0]*(dp[j][0]+dp[j][1]))%10007
        i=nx[i]
h=[0 for i in range(n+1)]
e=[0 for i in range(n+1)]
nx=[0 for i in range(n+1)]
idx=1
x=list(map(int,input().split()))
for i in range(0,n-1):
    e[idx]=i+2
    nx[idx]=h[x[i]]
    h[x[i]]=idx
    idx+=1
dfs(1)
print((dp[1][0]+dp[1][1]-1)%10007)

Reprinting is prohibited. Only for self-study. No responsibility for program errors.

Guess you like

Origin blog.csdn.net/weixin_46640345/article/details/112602391