Anda di halaman 1dari 4

2016.09.16.

GravityTree|WeekofCode23Question|Contests|HackerRank

Domains Contests Rank Leaderboard Jobs

All Contests Week of Code 23 Gravity Tree

Gravity Tree
by adamant

Problem Submissions Leaderboard Discussions

Your submission will run against only preliminary test cases. Full test cases will run at the end of the day.

Sarah is young scientist in Neverland who just invented a gravity tree with the following properties:

It's a tree where each vertex is numbered from to and vertex always denotes the root. Each edge has a length of , so a node has a
distance of from its parent, a distance of from its grandparent, and so on.

Some vertex, , can be turned on, which results in all vertices including of the subtree rooted at beginning to attract the tree's other
vertices. The attractive forces exerted on some vertex, , is equal to the summation of squared distances between vertex and all vertices that
are currently switched on i.e., all nodes in the subtree rooted at . For example, if two nodes are switched on that have distances of and
from node , then the forces exerted on would be .

Sarah wants to test the tree by carrying out a series of experiments on pairs of vertices, and . In each experiment, she turns on vertex and
measures the forces acting on vertex before immediately turning back off. To avoid undesirable consequences, she wants to know the value of
this attractive forces in advance.

Given the definition of her gravity tree and experiments, print the value of the attractive forces between vertices and for each experiment on
a new line. Note that each experiment is independent and no experiment will affect the subsequent experiments.

Input Format

The first line contains a single integer, , denoting the number of vertices in the tree.
The second line contains spaceseparated integers where the integer denotes the parent vertex of vertex .
The next line contains an integer, , denoting the number of experiments she plans to perform.
Each of the subsequent lines contains two spaceseparated integers denoting the respective values of and for an experiment.

Constraints

Output Format

On a new line for each experiment, print a single integer denoting the expected value of the forces acting on vertex from all the nodes in the
subtree rooted at vertex .

Sample Input

5
1224
2
21
14

Sample Output

7
13

Explanation

The diagram below depicts the tree defined in the Sample Case:

https://www.hackerrank.com/contests/w23/challenges/gravity1/copyfrom/7002361 1/4
2016.09.16. GravityTree|WeekofCode23Question|Contests|HackerRank

Let be the shortest distance from some vertex to vertex , where is a turned on vertex in the subtree rooted at . Sarah will perform the
following experiments:

1. Turn on vertex and measure the forces acting on vertex . When vertex is on, all the vertices in the tree i.e., , , , , and generate
attractive forces. We then calculate the forces acting on vertex as:

Thus, we print on a new line.

2. Turn on vertex and measure the forces acting on vertex . When vertex is on, the vertices in the subtree rooted at i.e., and generate
attractive forces. We then calculate the forces acting on vertex as:

Thus, we print on a new line.

Contest ends in 2 days

Submissions: 834

Max Score: 60

Difficulty: Difficult

Rate This Challenge:


More

Current Buffer saved locally, editable Java 8


1 importjava.util.Scanner;
2 importjava.util.Stack;
3
4 publicclassSolution{
5
6 privatestaticVertex[]vertices;
7 privatestaticint[][]distanceTable;
8
9 publicstaticvoidmain(String[]args){
10 Scannerin=newScanner(System.in);
11
12 intn=in.nextInt();
13 vertices=newVertex[n];
14 distanceTable=newint[n][n];
15
16 for(inti=0;i<n;i++){
17 for(intj=i;j<n;j++){
18 distanceTable[i][j]=i==j?0:1;
19 }
20 }
21
22 for(inti=0;i<n;i++){
23 vertices[i]=newVertex();

https://www.hackerrank.com/contests/w23/challenges/gravity1/copyfrom/7002361 2/4
2016.09.16. GravityTree|WeekofCode23Question|Contests|HackerRank
24 vertices[i].setIndex(i);
25 }
26
27 for(inti=1;i<n;i++){
28 inta=in.nextInt();
29 vertices[i].setParent(vertices[a1]);
30 }
31
32 for(inti=0;i<n;i++){
33 for(intj=i;j<n;j++){
34 distanceTable[i][j]=i==j?0:1;
35 }
36 }
37
38 for(inti=0;i<n;i++){
39 intd=1;
40 for(Vertexparent=vertices[i].getParent();parent!=null;parent=parent.getParent(),d++){
41 setDistance(i,parent.getIndex(),d);
42 }
43 }
44
45 for(inti=0;i<n;i++){
46 for(intj=i;j<n;j++){
47 if(distanceTable[i][j]==1){
48 Stack<Vertex>vStack=newStack<>();
49
50 VertexcurrVertex=vertices[i];
51 while(getDistance(currVertex.getIndex(),j)==1){
52 vStack.push(currVertex);
53 currVertex=currVertex.getParent();
54 }
55
56 intd=getDistance(currVertex.getIndex(),j);
57 while(!vStack.isEmpty()){
58 setDistance(vStack.pop().getIndex(),j,++d);
59 }
60 }
61 }
62 }
63
64 intexperiments=in.nextInt();
65 for(inti=0;i<experiments;i++){
66 intu=in.nextInt();
67 intv=in.nextInt();
68
69 vertices[v1].setAttracted(true);
70 longattractiveness=0;
71 for(Vertexvertex:vertices){
72 if(vertex.isGeneratingAttractiveness()){
73 longd=getDistance(u1,vertex.getIndex());
74 attractiveness+=d*d;
75 }
76 }
77
78 clearAttracted();
79
80 System.out.println(attractiveness);
81 }
82 }
83
84 privatestaticvoidclearAttracted(){
85 for(Vertexvertex:vertices){
86 vertex.setAttracted(false);
87 }
88 }
89
90 privatestaticvoidsetDistance(inti,intj,intd){
91 distanceTable[Math.min(i,j)][Math.max(i,j)]=d;
92 }
93
94 privatestaticintgetDistance(inti,intj){
95 returndistanceTable[Math.min(i,j)][Math.max(i,j)];
96 }
97
98 publicstaticclassVertex{
99 Vertexparent;
100 intindex;
101 booleanattracted=false;

https://www.hackerrank.com/contests/w23/challenges/gravity1/copyfrom/7002361 3/4
2016.09.16. GravityTree|WeekofCode23Question|Contests|HackerRank
102
103 publicVertex(){
104 }
105
106 publicVertex(Vertexparent){
107 this.parent=parent;
108 }
109
110 publicVertexgetParent(){
111 returnparent;
112 }
113
114 publicvoidsetParent(Vertexparent){
115 this.parent=parent;
116 }
117
118 publicbooleanisAttracted(){
119 returnattracted;
120 }
121
122 publicvoidsetAttracted(booleanattracted){
123 this.attracted=attracted;
124 }
125
126 publicbooleanisGeneratingAttractiveness(){
127 for(Vertexchecked=this;checked!=null;checked=checked.getParent()){
128 if(checked.isAttracted()){
129 this.setAttracted(true);
130 returntrue;
131 }
132 }
133
134 returnfalse;
135 }
136
137 publicintgetIndex(){
138 returnindex;
139 }
140
141 publicvoidsetIndex(intindex){
142 this.index=index;
143 }
144 }
145 }

Line:1Col:1

Upload Code as File Test against custom input Run Code Submit Code

Join us on IRC at #hackerrank on freenode for hugs or bugs.


Contest Calendar | Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy | Request a Feature

https://www.hackerrank.com/contests/w23/challenges/gravity1/copyfrom/7002361 4/4

Anda mungkin juga menyukai