Submission #11300557


Source Code Expand

Copy
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int, read().split())
AB = zip(m, m)

MOD = 10 ** 9 + 7

graph = [[] for _ in range(N + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)

root = 1
parent = [0] * (N + 1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

fact = [1] * (N + 10)
for n in range(1, N + 10):
    fact[n] = n * fact[n - 1] % MOD
fact_inv = [1] * (N + 10)
fact_inv[-1] = pow(fact[-1], MOD - 2, MOD)
for n in range(N + 9, 0, -1):
    fact_inv[n - 1] = fact_inv[n] * n % MOD

size_d = [0] * (N + 1)
dp_d = [1] * (N + 1)

for v in order[::-1]:
    dp_d[v] *= fact[size_d[v]]
    dp_d[v] %= MOD
    p = parent[v]
    s = size_d[v] + 1
    size_d[p] += s
    dp_d[p] *= fact_inv[s] * dp_d[v]
    dp_d[p] %= MOD

size_u = [N - 2 - x for x in size_d]
dp_u = [1] * (N + 1)

for v in order[1:]:
    p = parent[v]
    x = dp_d[p]
    x *= dp_u[p]
    x *= fact_inv[size_d[p]]
    x *= fact[size_d[v] + 1]
    x *= pow(dp_d[v], MOD - 2, MOD)
    x *= fact[size_u[v]]
    x *= fact_inv[size_u[p] + 1]
    dp_u[v] = x % MOD

for xd, xu, sd, su in zip(dp_d[1:], dp_u[1:], size_d[1:], size_u[1:]):
    su += 1
    x = xd * xu * fact[sd + su] * fact_inv[sd] * fact_inv[su] % MOD
    print(x)

Submission Info

Submission Time
Task F - Distributing Integers
User maspy
Language Python3 (3.4.3)
Score 600
Code Size 1549 Byte
Status
Exec Time 2227 ms
Memory 88308 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All 600 / 600 line_01.txt, line_02_shuffled.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06_shuffled.txt, random_07_shuffled.txt, random_08_shuffled.txt, random_09_shuffled.txt, random_10.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, star_01.txt, star_02_shuffled.txt
Case Name Status Exec Time Memory
line_01.txt 371 ms 23728 KB
line_02_shuffled.txt 478 ms 23800 KB
random_01.txt 2209 ms 88308 KB
random_02.txt 2227 ms 87732 KB
random_03.txt 2139 ms 87920 KB
random_04.txt 2191 ms 88176 KB
random_05.txt 2162 ms 87688 KB
random_06_shuffled.txt 2140 ms 87852 KB
random_07_shuffled.txt 2135 ms 88052 KB
random_08_shuffled.txt 2150 ms 87788 KB
random_09_shuffled.txt 2201 ms 87848 KB
random_10.txt 2221 ms 87692 KB
sample_01.txt 18 ms 3192 KB
sample_02.txt 18 ms 3192 KB
sample_03.txt 18 ms 3192 KB
sample_04.txt 18 ms 3192 KB
star_01.txt 1389 ms 81676 KB
star_02_shuffled.txt 1824 ms 87948 KB