0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

yukicoder contest 308 参戦記

Posted at

yukicoder contest 308 参戦記

A 1636 森

N頂点の木の辺の数はN-1なので、答えは(N-1)2です.

N = int(input())

print((N - 1) * (N - 1))

B 1637 Easy Tree Query

素直に部分木の頂点にコストを足してまわると O(NQ) で TLE です. 増えるコストは部分木の頂点数にxを掛けたものなので、最初に各部分木の頂点数を DFS で求めておけば O(1) でクエリに回答できます.

ABC138D - Kiを思い出しましたが、こっちのほうがやや簡単かな.

from sys import setrecursionlimit, stdin

readline = stdin.readline
setrecursionlimit(10 ** 6)

N, Q = map(int, readline().split())

links = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, readline().split())
    links[a - 1].append(b - 1)
    links[b - 1].append(a - 1)

visited = [False] * N
subnodes = [0] * N


def f(i):
    visited[i] = True
    result = 1
    for j in links[i]:
        if visited[j]:
            continue
        result += f(j)
    subnodes[i] = result
    return result


f(0)

result = []
t = 0
for _ in range(Q):
    p, x = map(int, readline().split())
    t += subnodes[p - 1] * x
    result.append(t)
print(*result, sep='\n')
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?