Submission #63062150


Source Code Expand

Copy
import sys, heapq
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1; b -= 1
g[a].append(b)
g[b].append(a)
cand_flag = [False] * n
for i in range(n):
if len(g[i]) >= 4:
cand_flag[i] = True
if not any(cand_flag):
print(-1)
sys.exit(0)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import sys, heapq
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

cand_flag = [False] * n
for i in range(n):
    if len(g[i]) >= 4:
        cand_flag[i] = True
if not any(cand_flag):
    print(-1)
    sys.exit(0)


H = {i: [] for i in range(n) if cand_flag[i]}
for i in H:
    for j in g[i]:
        if cand_flag[j]:
            H[i].append(j)


down = {}

def dfs_down(v, parent):
    child_vals = []
    for w in H[v]:
        if w == parent:
            continue
        child_vals.append(dfs_down(w, v))
    limit = 4 if parent is None else 3

    best = heapq.nlargest(limit, child_vals)
    res = 1 + sum(best)
    down[v] = res
    return res


def dfs_reroot(v, parent, up_val):
    limit = 4 if parent is None else 3
    cur_list = []
    if up_val is not None:
        cur_list.append(up_val)
    for w in H[v]:
        if w == parent:
            continue
        cur_list.append(down[w])
    cur_dp = 1 + sum(heapq.nlargest(limit, cur_list))
    best = cur_dp
    for w in H[v]:
        if w == parent:
            continue
        temp = []
        if up_val is not None:
            temp.append(up_val)
        for x in H[v]:
            if x == parent or x == w:
                continue
            temp.append(down[x])
        new_up = 1 + sum(heapq.nlargest(3, temp))
        best = max(best, dfs_reroot(w, v, new_up))
    return best

visited = set()
ans_m = 0
for v in H:
    if v in visited:
        continue
    comp = []
    stack = [v]
    while stack:
        cur = stack.pop()
        if cur in visited:
            continue
        visited.add(cur)
        comp.append(cur)
        for w in H[cur]:
            if w not in visited:
                stack.append(w)
    r = comp[0]
    dfs_down(r, None)
    comp_best = dfs_reroot(r, None, None)
    ans_m = max(ans_m, comp_best)

print(3 * ans_m + 2)

Submission Info

Submission Time
Task F - Alkane
User juten
Language Python (PyPy 3.10-v7.3.12)
Score 500
Code Size 2089 Byte
Status AC
Exec Time 1523 ms
Memory 779224 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 59
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All hand00.txt, hand01.txt, hand02.txt, hand03.txt, sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt
Case Name Status Exec Time Memory
hand00.txt AC 65 ms 76656 KB
hand01.txt AC 64 ms 76500 KB
hand02.txt AC 64 ms 76396 KB
hand03.txt AC 63 ms 76516 KB
sample00.txt AC 65 ms 76396 KB
sample01.txt AC 61 ms 76660 KB
sample02.txt AC 63 ms 76440 KB
testcase00.txt AC 63 ms 76652 KB
testcase01.txt AC 64 ms 76808 KB
testcase02.txt AC 62 ms 76536 KB
testcase03.txt AC 63 ms 76364 KB
testcase04.txt AC 549 ms 132956 KB
testcase05.txt AC 573 ms 131292 KB
testcase06.txt AC 312 ms 103780 KB
testcase07.txt AC 573 ms 131476 KB
testcase08.txt AC 299 ms 101828 KB
testcase09.txt AC 603 ms 132092 KB
testcase10.txt AC 822 ms 556120 KB
testcase11.txt AC 1523 ms 779224 KB
testcase12.txt AC 370 ms 108284 KB
testcase13.txt AC 595 ms 131264 KB
testcase14.txt AC 463 ms 121936 KB
testcase15.txt AC 566 ms 131320 KB
testcase16.txt AC 548 ms 133668 KB
testcase17.txt AC 588 ms 131356 KB
testcase18.txt AC 593 ms 131508 KB
testcase19.txt AC 703 ms 131688 KB
testcase20.txt AC 391 ms 110184 KB
testcase21.txt AC 618 ms 131800 KB
testcase22.txt AC 176 ms 98688 KB
testcase23.txt AC 238 ms 106036 KB
testcase24.txt AC 229 ms 105180 KB
testcase25.txt AC 244 ms 105388 KB
testcase26.txt AC 154 ms 94684 KB
testcase27.txt AC 237 ms 105724 KB
testcase28.txt AC 224 ms 104836 KB
testcase29.txt AC 238 ms 105800 KB
testcase30.txt AC 226 ms 101504 KB
testcase31.txt AC 240 ms 105800 KB
testcase32.txt AC 427 ms 107152 KB
testcase33.txt AC 519 ms 120136 KB
testcase34.txt AC 250 ms 94472 KB
testcase35.txt AC 596 ms 122552 KB
testcase36.txt AC 365 ms 103492 KB
testcase37.txt AC 578 ms 121132 KB
testcase38.txt AC 413 ms 107740 KB
testcase39.txt AC 554 ms 120996 KB
testcase40.txt AC 467 ms 113256 KB
testcase41.txt AC 561 ms 121004 KB
testcase42.txt AC 359 ms 102528 KB
testcase43.txt AC 558 ms 120528 KB
testcase44.txt AC 515 ms 114880 KB
testcase45.txt AC 570 ms 119428 KB
testcase46.txt AC 319 ms 99704 KB
testcase47.txt AC 537 ms 119004 KB
testcase48.txt AC 307 ms 96660 KB
testcase49.txt AC 549 ms 121344 KB
testcase50.txt AC 543 ms 119100 KB
testcase51.txt AC 513 ms 118220 KB


2025-05-05 (Mon)
15:13:53 +09:00