Submission #12152078


Source Code Expand

Copy
import sys
input = lambda: sys.stdin.readline().rstrip()
N = int(input())
C = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
for i in range(N-1):
x, y = map(int, input().split())
X[x-1].append(y-1)
X[y-1].append(x-1)
def EulerTour(n, X, i0):
f = lambda k: k * (k + 1) // 2
USED = [0] * n
ORG = [0] * n
TMP = [0] * n
P = [-1] * n
Q = [~i0, i0]
ct = -1
ET1 = [0] * n
ET2 = [0] * n
ANS = [f(n)] * n
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import sys
input = lambda: sys.stdin.readline().rstrip()
N = int(input())
C = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

def EulerTour(n, X, i0):
    f = lambda k: k * (k + 1) // 2
    USED = [0] * n
    ORG = [0] * n
    TMP = [0] * n
    P = [-1] * n
    Q = [~i0, i0]
    ct = -1
    ET1 = [0] * n
    ET2 = [0] * n
    ANS = [f(n)] * n
    while Q:
        i = Q.pop()
        if i < 0:
            i = ~i
            ET2[i] = ct
            USED[C[i]] += 1 + TMP[i]
            if i:
                p = P[i]
                k = ET2[i] - ET1[i] + 1 - USED[C[p]] + ORG[i]
                ANS[C[p]] -= f(k)
                TMP[p] += k
            continue
        if i >= 0:
            if i: ORG[i] = USED[C[P[i]]]
            ct += 1
            if ET1[i] == 0: ET1[i] = ct
        for a in X[i][::-1]:
            if a != P[i]:
                P[a] = i
                for k in range(len(X[a])):
                    if X[a][k] == i:
                        del X[a][k]
                        break
                Q.append(~a)
                Q.append(a)
    for i in range(n):
        ANS[i] -= f(n - USED[i])
    return ANS

print(*EulerTour(N, X, 0), sep = "\n")

Submission Info

Submission Time
Task F - path pass i
User Kiri8128
Language Python (3.8.2)
Score 600
Code Size 1349 Byte
Status AC
Exec Time 1132 ms
Memory 76532 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 5
AC × 24
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
All line_01.txt, line_3_01.txt, random_01.txt, random_02.txt, random_03.txt, random_100_01.txt, random_100_02.txt, random_10_01.txt, random_10_02.txt, random_1_01.txt, random_1_02.txt, random_2_01.txt, random_2_02.txt, random_2_03.txt, random_3_01.txt, random_3_02.txt, random_3_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, star_01.txt, star_3_01.txt
Case Name Status Exec Time Memory
line_01.txt AC 887 ms 76532 KB
line_3_01.txt AC 695 ms 62872 KB
random_01.txt AC 1132 ms 64676 KB
random_02.txt AC 1117 ms 64604 KB
random_03.txt AC 1112 ms 64476 KB
random_100_01.txt AC 1017 ms 57044 KB
random_100_02.txt AC 1013 ms 57044 KB
random_10_01.txt AC 1005 ms 55944 KB
random_10_02.txt AC 1021 ms 55952 KB
random_1_01.txt AC 1008 ms 55220 KB
random_1_02.txt AC 1007 ms 55080 KB
random_2_01.txt AC 1006 ms 55396 KB
random_2_02.txt AC 1028 ms 55588 KB
random_2_03.txt AC 1028 ms 55512 KB
random_3_01.txt AC 1017 ms 55516 KB
random_3_02.txt AC 1000 ms 55408 KB
random_3_03.txt AC 1001 ms 55628 KB
sample_01.txt AC 17 ms 9060 KB
sample_02.txt AC 17 ms 9228 KB
sample_03.txt AC 19 ms 9224 KB
sample_04.txt AC 17 ms 9356 KB
sample_05.txt AC 19 ms 9052 KB
star_01.txt AC 721 ms 62240 KB
star_3_01.txt AC 678 ms 56872 KB


2025-04-23 (Wed)
08:48:24 +00:00