about summary refs log tree commit diff
path: root/codechef/sj1.py
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-04-15 17:20:41 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-04-15 17:20:41 +0700
commit887c286cc8228e13f85b587ab92b37e920161eb9 (patch)
tree6528ca8873c4f80b6d13114550615c7a1f1f4afe /codechef/sj1.py
parentebb7f33582fb24fe70f13796b88e93b99c243425 (diff)
downloadcp-887c286cc8228e13f85b587ab92b37e920161eb9.tar.gz
Codechef celebrate 4.20 a bit early this year
Diffstat (limited to 'codechef/sj1.py')
-rwxr-xr-xcodechef/sj1.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/codechef/sj1.py b/codechef/sj1.py
new file mode 100755
index 0000000..475f114
--- /dev/null
+++ b/codechef/sj1.py
@@ -0,0 +1,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))