python(not pypy), dfs, thread?

https://codeforces.com/contest/1336/problem/A

import sys
import threading
from collections import defaultdict
sys.setrecursionlimit(10**6)
threading.stack_size(10**8)
input = sys.stdin.readline

B = []
A = defaultdict(list)


def dfs(now, fa, depth):
    son = 1
    for to in A[now]:
        if to != fa:
            son += dfs(to, now, depth + 1)
    B.append(depth - son)
    return son


def main():
    n, m = map(int, input().split())
    for i in range(n - 1):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        A[x].append(y)
        A[y].append(x)
    dfs(0, -1, 1)
    print(sum(sorted(B, reverse=True)[:m]))


t = threading.Thread(target=main)
t.start()
t.join()

原文地址:https://www.cnblogs.com/reshuffle/p/12783497.html