Cattle off winter training camp 4 - F

Topic portal
This problem is actually a send sub-themes ... but I was stuck on God (gai) Qi (si) of the title E, no time to write this water problem of ...
E problem is so god (e) odd ( xin) that I really do not want to write it alone a summary of ... (not that a greedy + high-precision)

Read the question again, drawing a little look at it is to know that you seek the tree has a few of the distance between the points is even a little look can be found in the depth of an odd number of nodes to the depth of the distance between nodes in a certain odd even, even the depth of empathy is (remember when little knowledge of it, you never know when life-saving?) then this problem can be passed Shui

AC Code:

#include <iostream>
#include <queue>
#include <cstring>
#define MAX 1000010 
#define ll long long 
using namespace std;
ll num;
int c[MAX];
struct node{
	int to;
	int nxt;
}edge[MAX];

int head[MAX];
int cnt=0;

void addedge(int from,int to)
{
	edge[cnt]={to,head[from]};
	head[from]=cnt++;
}

void bfs(int s)
{
	queue<int> que;
	que.push(s);
	c[s]=1;
	while(!que.empty())
	{
		int t=que.front();
		que.pop();
		for(int i=head[t];i!=-1;i=edge[i].nxt)
		{
			int to=edge[i].to;
			c[to]=c[t]+1;
			que.push(to);
		}
	}
}

void init()
{
	
	memset(head,-1,sizeof(head));
}

int main()
{
	init();
	ll ans=0;
	int n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int a;
		cin>>a;
		addedge(a,i+1);
	}
	bfs(1);
	for(int i=1;i<=n;i++)
	{
		if(c[i]%2==1)
		num++;
	}
	ans+=num*(num-1);
	num=n-num;
	ans+=(num-1)*num;
	cout<<ans;
	return 0;
	
}

And God (e) odd (xin) of the title E ratio, which is simply a question angel ah (unfortunately did not catch)

Published 30 original articles · won praise 9 · views 1297

Guess you like

Origin blog.csdn.net/Zhang_sir00/article/details/104269033