Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)D. Peculiar apple-tree
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences.
Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
3
1 1
1
5
1 2 2 2
3
18
1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4
4 题意:一棵树上1~N个节点,1为基部节点,给定从2~N的节点的每个节点连接的下一个节点。现在每个节点产生一个苹果,每过一秒向下滚动一个节点,如果有偶数个的苹果同时汇入同一节点则碰撞消失,问总共能收集多少苹果。
因为最后的汇点唯一,所以同一层的苹果必然会在下面的某一个节点相遇,那么只需要从下向上做一次bfs,再统计每一层苹果数量,偶数则答案加一。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll; const int maxn = 1e5+;
int dep = ;
struct edge{
int to, next;
}e[maxn], _e[maxn];
int head[maxn], tot;
int n, cur[maxn], cnt[maxn]; void init()
{
tot = ;
memset(head, -, sizeof head);
} void addedge(int u,int v)
{
e[tot].to = v;
e[tot].next = head[u];
head[u] = tot ++;
} void bfs()
{
queue <int> que;
cur[] = ;
que.push(); while(!que.empty())
{
int a = que.front();
que.pop();
dep = max(dep, cur[a]);
cnt[cur[a]]++; for(int i=head[a]; i!=-; i=e[i].next)
{
int v = e[i].to;
cur[v] = cur[a] + ;
que.push(v);
}
}
} int solve()
{
int res = ;
for(int i=;i<=dep;i++)
if(cnt[i]&)
res ++;
return res;
} int main()
{
init();
scanf("%d",&n);
int x;
for(int i=;i<n-;i++)
{
scanf("%d",&x);
addedge(x, i+);
}
bfs(); printf("%d\n", solve());
}
Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)D. Peculiar apple-tree的更多相关文章
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)B. World Cup
The last stage of Football World Cup is played using the play-off system. There are n teams left in ...
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)
A.B都是暴力搞一搞. A: #include<bits/stdc++.h> #define fi first #define se second #define mk make_pair ...
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)C. Laboratory Work
Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some v ...
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)A. Friends Meeting
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)
A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
随机推荐
- 第一次训练 密码:acmore
#include <cstdio> #include <cstring> #define M 100010 #define INF 0x7FFFFFFF #define Min ...
- SQL Server 通过备份文件初始化复制 – 听风吹雨
一.背景 MySQL在对有历史数据的数据库进行搭建复制(Master/Slave)的时候,可以通过在Master服务器备份历史数据,利用这个备份文件 在Slave进行还原:这样做的好处是可以更加快速的 ...
- fzu 2136
#include<stdio.h> #define inf 1000000000 #define N 110000 int a[N]; struct node { int start,en ...
- windows集成身份验证
集成windows身份验证 这种验证方式里面也分为两种情况 NTLM验证 这种验证方式需要把用户的用户名和密码传送到服务端,服务端验证用户名和密码是否和服务器的此用户的密码一致.用户名用明码传送,但是 ...
- C#版winform实现UrlEncode
在Asp.net中可以使用Server.HTMLEncode和Server.URLEncode 将文本或URL的特殊字符编码,但在控制台或Winform程序中没有办法使用到这些方法, 解决办法:右击项 ...
- HDU 3987 && DINIC
很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流 ...
- HDU 1198 Farm Irrigation (并查集优化,构图)
本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...
- WinForm使用CefSharp内嵌chrome浏览器
先贴运行图:亲测可用!以图为证! 开始!1.创建winform程序,使用.NET 4.5.2或以上(vs2010最高支持.NET 4.0,我使用的是vs2017).这一步容易忽略,简单的说就是将项目. ...
- 在Maven中设置Nexus私有服务为中央工厂
在Maven中设置Nexus私有服务为中央工厂(repository) 2015-12-12 17:45 168人阅读 评论(0) 收藏 举报 分类: Maven(17) 版权声明:本文为博主原创 ...
- 曲线控件我一直用codeproject上的那几个(C++ 100款开源界面库)
Alberl#23楼[楼主] 2013-11-04 11:47 Alberl @baita00引用看了大神的教程,真的不错,学习了很多东西,^_^.这节教程里,大神好像在找曲线控件,是吗?大神有什特殊 ...