about summary refs log tree commit diff
path: root/codechef/sj1.py
blob: 475f11442288247461c97ed9250f59c9b7300c1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3
from collections import defaultdict, deque
from math import gcd

for t in range(int(input())):
    n, edges = int(input()), defaultdict(list)
    for _ in range(1, n):
        x, y = (int(i) - 1 for i in input().split())
        edges[x].append(y)
        edges[y].append(x)
    v = [int(i) for i in input().split()]
    m = [int(i) for i in input().split()]

    queue, count = deque([0]), defaultdict(int, {0: 1})
    while queue:
        i = queue.popleft()
        for j in edges[i]:
            count[j] += 1
            if count[j] > 1: continue
            v[j] = gcd(v[i], v[j])
            queue.append(j)
    print(*(m[i] - gcd(v[i], m[i]) for i in range(n) if count[i] == 1))