2020牛客寒假算法基础集训营4--树上博弈

此题按层分析,分析得出,如果两人起初的层数差值是偶数的话,牛牛肯定赢

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
int depth[1000010];
int num[1000010];
ll dp[1000010];
int main(){
	int n;
	scanf("%d",&n);
	depth[1]=1;
	num[1]++;
	int id,maxn=0;
	for(int i=2;i<=n;i++){
		scanf("%d",&id);
		depth[i]=depth[id]+1;
		num[depth[i]]++;//记录每个深度含有的节点数 
		maxn=max(maxn,depth[i]);//记录最大深度 
	}
	//cout<<"maxn="<<maxn<<endl;
	dp[0]=0;dp[1]=1;
	for(int i=2;i<=maxn;i++){
		dp[i]=dp[i-2]+num[i];//越两层求和 
	}
	ll sum=0;
	//推导式的展开循环式 
	/*for(int i=maxn;i>=1;i--){
        if((maxn-i)&1)
		sum+=(num[i]*(dp[maxn-1]-1));
        else sum+=(num[i]*(dp[maxn]-1));
		//sum+=(num[i]*(dp[i-1]-1));
	}*/
	//printf("%lld\n",sum);
	//会用推导式dp数组的类型必须是long long,防止乘法爆int 
    cout<<(dp[maxn]*(dp[maxn]-1)+dp[maxn-1]*(dp[maxn-1]-1))<<endl;
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
发布了186 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43746332/article/details/104312885