[CSP-S Simulation Test]: Decorative (like pressure DP)

Title Portal (internal title 114)


Input Format

  The first line of a positive integer $ n $.
  The next line $ n-1 $ positive integer, the number of $ I $ $ f_ {i + 1} $ .
  The next line $ $ n-number, if the number of $ I $ $ $ 0 indicates Lin Xiansen $ I $ desirable dot lights are turned off, when the first number is $ I $ $ $ 1 $ indicates Lin Xiansen hope i $ dot lights are turned on.


Output Format

  Output line An integer representing a minimum of Linxian Sen takes a few seconds to see that he expected to see the tree.


Sample

Sample input 1:

4
1 2 3
0 1 1 0

Sample output 1:

2

Sample input 2:

7
1 1 2 2 3 3
0 1 1 1 0 0 1

Sample Output 2:

3


Data range and tips

Sample explained:

  The first sample:
  $ \ $ a first bullet seconds Lin Xiansen toggle switch point $ 3 $ number, status lights becomes $ 0 \ 0 \ 1 \ 0 $.
  $ \ bullet $ second seconds Linxian Sen do nothing, lantern state becomes $ 0 \ 1 \ 1 \ 0 $, meet the requirements.
  The second sample:
  $ \ $ a first bullet seconds Lin Xiansen toggle switch point $ 4 $ number, status lights becomes $ 0 \ 0 \ 0 \ 1 \ 0 \ 0 \ 0 $.
  $ \ $ second bullet seconds Lin Xiansen toggle switch point $ 7 $ number, status lights becomes $ 0 \ 1 \ 0 \ 1 \ 0 \ 0 \ 1 $.
  $ \ $ third bullet seconds Lin Xiansen number toggle switch point $ 1 $, $ 2 cancel out the effect of the toggle transmission $ point number, status lights becomes $ 0 \ 1 \ 1 \ 1 \ 0 \ 0 \ 1 $, satisfying Claim.

data range:

  For $ 30 \% $ data, $ n \ leqslant 8 $.
  For another $ 30 \% $ of data, Linxian Sen would like to see all the lights are lit.
  For $ 100 \% $ data, $ 1 \ leqslant n \ leqslant 16 $.


answer

Smaller data range, considering shaped pressure $ DP $.

Because the answer to a maximum of $ n $, so we can enumerate the number of seconds.

Provided $ dp [i] [state] $ $ I $ denotes the second, state State $ $ feasible.

Since the switch only negative is the same, but not with specific is on or off has nothing to do, so we can assume that the final state to the initial state, and then think of ways to transfer $ 0 $ state (in fact, a little lower complexity of code only).

Pretreatment would point out each other's point of contribution to make in the next second.

Time complexity: $ \ Theta (2 ^ nn ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,state;
int fa[20],Map[20][20];
bool dp[20][100000];
int main()
{
	scanf("%d",&n);
	for(int i=0;i<=n;i++)Map[1][i]=1;
	for(int i=2;i<=n;i++)
	{
		Map[i][0]=1<<(i-1);
		scanf("%d",&fa[i]);
		int now=i;
		for(int j=1;j<=n;j++)
		{
			now=fa[now];
			if(now)Map[i][j]=Map[i][j-1]|(1<<(now-1));
			else Map[i][j]=Map[i][j-1];
		}
	}
	for(int i=1;i<=n;i++){int x;scanf("%d",&x);state|=x<<(i-1);}
	dp[0][state]=1;
	for(int i=0;i<=n;i++)
	{
		if(dp[i][0]){printf("%d",i);break;}
		for(int s=1;s<(1<<n);s++)
			if(dp[i][s])
			{
				dp[i+1][s]=1;
				for(int j=1;j<=n;j++)
					dp[i+1][s^Map[j][i]]=1;
			}
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11782687.html