1. 程式人生 > >Codeforces Round #434 (Div. 1) -E Arkady and a Nobody-men

Codeforces Round #434 (Div. 1) -E Arkady and a Nobody-men

Codeforces傳送門

洛谷傳送門

題目描述

Arkady words in a large company. There are nn employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.

Each employee has an integer rank. The CEO has rank equal to 1 1 , each other employee has rank equal to the rank of his immediate manager plus 1

1 .

Arkady has a good post in the company, however, he feels that he is nobody in the company’s structure, and there are a lot of people who can replace him. He introduced the value of replaceability. Consider an employee a

a and an employee b b , the latter being manager of aa (not necessarily immediate). Then the replaceability r ( a , b ) r(a,b) of a a with respect to b b is the number of subordinates (not necessarily immediate) of the manager b b , whose rank is not greater than the rank of a a . Apart from replaceability, Arkady introduced the value of negligibility. The negligibility z a z_{a} of employee aa equals the sum of his replaceabilities with respect to all his managers, i.e. img, where the sum is taken over all his managers b b .

Arkady is interested not only in negligibility of himself, but also in negligibility of all employees in the company. Find the negligibility of each employee for Arkady.

輸入輸出格式

輸入格式:

The first line contains single integer n n ( 1 n 5 1 0 5 1\le n\le 5·10^{5} ) — the number of employees in the company.

The second line contains n n integers p 1 , p 2 , . . . , p n p_{1},p_{2},...,p_{n} ( 0 p i n 0\le p_{i}\le n ), where p i = 0 p_{i}=0 if the i i -th employee is the CEO, otherwise p i p_{i} equals the id of the immediate manager of the employee with id i i . The employees are numbered from 1 1 to n n . It is guaranteed that there is exactly one 0 0 among these values, and also that the CEO is a manager (not necessarily immediate) for all the other employees.

輸出格式:

Print n n integers — the negligibilities of all employees in the order of their ids: z 1 , z 2 , . . . , z n z_{1},z_{2},...,z_{n} .

輸入輸出樣例

輸入樣例#1:

4
0 1 2 1

輸出樣例#1:

0 2 4 2 

輸入樣例#2:

5
2 3 4 5 0

輸出樣例#2:

10 6 3 1 0 

輸入樣例#3:

5
0 1 1 1 3

輸出樣例#3:

0 3 3 3 5 

說明

Consider the first example:

  • The CEO has no managers, thus z 1 = 0 z_{1}=0 .
  • r ( 2 , 1 ) = 2 r(2,1)=2 (employees 2 2 and 4 4 suit the conditions, employee 3 3 has too large rank). Thus z 2 = r ( 2 , 1 ) = 2 z_{2}=r(2,1)=2 .
  • Similarly,$ z_{4}=r(4,1)=2$ .
  • r ( 3 , 2 ) = 1 r(3,2)=1 (employee 3 3 is a subordinate of 2 2 and has suitable rank). r ( 3 , 1 ) = 3 r(3,1)=3 (employees 2 2 , 3 3 , 4 4 suit the conditions). Thus z 3 = r ( 3 , 2 ) + r ( 3 , 1 ) = 4 z_{3}=r(3,2)+r(3,1)=4 .

題目大意

給你一棵有根樹, 設 b b a a 的祖先節點, r ( a , b ) r(a,b) 表示在 b b 子樹中不超過 a a 深度的點的個數(不包括 a a 自己), 求對於所有點的 z ( i ) = j a n c e s t o r ( i ) r ( i , j ) z(i)=\sum_{j\in ancestor(i)}r(i,j)

解題分析

很容易發現每個點 a a 對於其他點 b b ( d e p [ a ] d e p [ b ] dep[a]\le dep[b] )的貢獻就是 d e p [ l c a ( a , b ) ] dep[lca(a,b)] , 所以將所有點按深度從小到大排序, 每次處理同一深度的點, 在其到根節點的路徑上區間 + 1 +1 , 然後查詢其到根節點的路徑上的權值和即可, 總複雜度 O ( n l o g 2 ( n ) ) O(nlog^2(n)) , 卡卡常就過了。

這裡有一種 O ( n l o g ( n ) ) O(nlog(n)) 的做法。我們可以發現 z ( i ) = z ( f a t [ i ] ) + d e p [ i ] + d e p [ i ] = d e p [ j ] d e p [ l c a ( i , j ) ] z(i)=z(fat[i])+dep[i]+\sum_{dep[i]=dep[j]}dep[lca(i,j)] ,那麼實際上我們只需要考慮同深度的點之間的貢獻。

將同深度的點按 D F S DFS 序排序, 那麼可以發現對於一個排在第 i i 位的點, 其前面的點從 1 1 開始, 它們的 l c a lca

相關推薦

Codeforces Round #434 (Div. 1) -E Arkady and a Nobody-men

Codeforces傳送門 洛谷傳送門 題目描述 Arkady words in a large company. There are nn employees working in a system of a strict hierarchy. Namely, each e

Codeforces Round #190 (Div. 1): E. Ciel and Gondolas(決策單調性DP+wqs二分)

題意: 同一道題目,但是bzoj可能需要讀入掛 思路: wqs二分 沒什麼可講的了 #include<stdio.h> #include<string.h> #include<algorithm>

#DFS序+二分+字首和# Codeforces Round #381 (Div. 1) B. Alyona and a tree

題目連結 B. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  standard input output&

Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 字首和

  B. Alyona and a tree 題目連線: http://codeforces.com/contest/739/problem/B Description Alyona has a tree with n vertices. The root of the

Educational Codeforces Round #54 (Div. 2) E. Vasya and a Tree 技巧題

題目連線:http://codeforces.com/contest/1076/problem/E   本題大意:   給一棵根節點為“1”樹,給m個操作,每個操作三個整數,v,d,x,意思是從節點1,往下深度d,遍及的節點的值都加上x,d可能是0,就是隻加在自己上。結束m個操作後輸出每個節點的值。 &

Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem(貪心+樹形dp)

E. Ehab and a component choosing problem time limit per test 1 second memory limit per test 256 megabytes input standard input output

Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(樹形dp)

E. Ehab and a component choosing problem time limit per test 1 second memory limit per test 256 megabytes input standard input output

Codeforces Round #250 (Div. 1)E. The Child and Binary Tree

tor base sig target pri inline class long long inf 題意:有一個集合,求有多少形態不同的二叉樹滿足每個點的權值都屬於這個集合並且總點權等於i 題解:先用生成函數搞出來\(f(x)=f(x)^2*c(x)+1\) 然後轉化一下

[Codeforces Round #250 (Div. 1) -E] The Child and Binary Tree

Codeforces傳送門 洛谷傳送門 題目翻譯 我們的小朋友很喜歡電腦科學,而且尤其喜歡二叉樹。 考慮一個含有 n n

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

signed sss fail loop char s puts ack a* ons 題意:有n個串,詢問每個串有多少子串在n個串中出現了至少k次. 題解:sam,每個節點開一個set維護該節點的字符串有哪幾個串,啟發式合並set,然後在sam上走一遍該串,對於每個可行的

Codeforces Round #263 (Div.1) B. Appleman and Tree

ace apple n+1 test right art [0 pan target 題目地址:http://codeforces.com/contest/461/problem/B 題目大意:給一棵樹。每一個點為白色或黑色。切斷一些邊,使得每一個連通塊有且僅有一個黑點

[Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)

solution and other main ems scanf homework max urn Description Pashmak‘s homework is a problem about graphs. Although he always tries

(博弈sg) Codeforces Round #417 (Div. 2) E Sagheer and Apple Tree

paths 相同 friend pri 產生 chan star ren have Sagheer is playing a game with his best friend Soliman. He brought a tree with n nodes numbered

【找規律】【遞推】【二項式定理】Codeforces Round #419 (Div. 1) B. Karen and Test

main turn logs pow 分享 string ren () 奇數 打個表出來看看,其實很明顯。 推薦打這倆組 11 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000

Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩陣)

clu blank his 題目 pri 狀態 oid c++ class 題目鏈接:Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 題意: 在一個二維方格子裏有n條線段,有三種走法 (x?+?1,?y?

Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree(樹上Nim)

ide vector 等於 如果 ace ble 技術 方式 復雜度 題目鏈接:Codeforces Round #417 (Div. 2) E. Sagheer and Apple Tree 題意: 給你一棵樹,每個節點有a[i]個蘋果,有兩個人要在這個樹上玩遊戲。 兩個

Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function(預處理+二分)

class [1] can 行處理 long 個數 分享 amp abs 題目鏈接:點我點我 題意:公式:,給出n個數,從a[1]到a[n],和m個數(b數組),然後從b數組裏挑出連續的n個數(也就m-n+1中選擇),按公式計算,使得f最小, 還有q次對a數組的操作(某

Codeforces Round #384 (Div. 2) E. Vladik and cards 狀壓dp

答案 clu test none urn 數字 ble inline return Codeforces Round #384 (Div. 2) E. Vladik and cards 狀壓dp 大致題意: 給定一個序列an,序列中只有1~8的8個整數,讓你選出一個子序列

Codeforces Round #321 (Div. 2) E - Kefa and Watch

back fin put 我們 esp bool cond can push_back 題目大意:給你一個由0-9組成的字符串,有m個詢問,兩種操作,第一種將l到r的字符全部變成c,第二種問l到r這段 字符串的循環節是不是d。 思路:首先我們要知道怎麽判斷字符串的循環節的長

Codeforces Round #379 (Div. 2) E. Anton and Tree

vector #define clu cto bits air int 向上 fin 題意:給你一棵樹, 每個點要麽是黑色要麽是白色, 有一種操作是將同一個顏色的連通塊變成相反的顏色,問你最少變換幾次, 整顆樹變成一種顏色。 思路: 縮點, 加求樹的直徑, 答案為樹