Submission #7000087


Source Code Expand

Copy
<?php
fscanf(STDIN, '%d %d', $n, $q);
for ($i  = 0; $i < $n - 1; $i++) {
    fscanf(STDIN, '%d %d', $a, $b);
    $a--;
    $b--;
    $g[$a][] = $b;
}
$r = array_fill(0, $n, 0);
for ($i  = 0; $i < $q; $i++) {
    fscanf(STDIN, '%d %d', $p, $x);
    $r[$p - 1] += $x;
}
$point = array_fill(0, $n, 0);
dfs($g, 0, 0, $r, $point);
$ans = implode(' ', $point);
echo $ans . PHP_EOL;

function dfs($g, $v = 0, $x = 0, $r, &$point) {
    if (isset($r[$v])) $x += $r[$v];
    $point[$v] += $x;

    if (!isset($g[$v])) return;
    foreach ($g[$v] as $next_v) {
        dfs($g, $next_v, $x, $r, $point);
    }
}

Submission Info

Submission Time
Task D - Ki
User hideyuk1
Language PHP7 (7.0.15)
Score 400
Code Size 626 Byte
Status
Exec Time 332 ms
Memory 176632 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 a01, a02
All 400 / 400 a01, a02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14
Case Name Status Exec Time Memory
a01 9 ms 4604 KB
a02 9 ms 4604 KB
b03 9 ms 4604 KB
b04 332 ms 176632 KB
b05 324 ms 176632 KB
b06 232 ms 32372 KB
b07 222 ms 35260 KB
b08 283 ms 102900 KB
b09 272 ms 65216 KB
b10 320 ms 131576 KB
b11 315 ms 107000 KB
b12 302 ms 90616 KB
b13 318 ms 176632 KB
b14 226 ms 32372 KB