Problem:


Score : 600 points

Problem Statement

Given an undirected tree, let the distance between vertices u and v be the
number of edges on the simple path from u to v.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree good if and only if its diameter is at most K.

You are given an undirected tree with N vertices numbered 1 through N.
For each i (1≦i≦N-1), there is an edge connecting vertices A_i and B_i.

You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

2≦N≦2000

1≦K≦N-1

1≦A_i≦N, 1≦B_i≦N

The graph defined by A_i and B_i is a tree.

Input

The input is given from Standard Input in the following format:

N K
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.

Sample Input 1

6 2
1 2
3 2
4 2
1 6
5 6

Sample Output 1

2

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

Sample Input 2

6 5
1 2
3 2
4 2
1 6
5 6

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.



Sample input:
6 2
1 2
3 2
4 2
1 6
5 6


Sample output:
2

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=[[]for i in range(N+1)]
l=[]
for i in range(N-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)
    l.append([a,b])
ans=N
if K%2==0:
    for i in range(1,N+1):
        C=[10000 for i in range(N+1)]
        Q=[[i,0]]
        cnt=1
        for q in range(10000):
            if q==len(Q):
                break
            if C[Q[q][0]]==10000:
                C[Q[q][0]]=Q[q][1]
                cnt+=1
                if Q[q][1]<K//2:
                    for k in range(len(L[Q[q][0]])):
                        if C[k]==10000:
                            Q.append([k,Q[q][1]+1])
        if N-cnt<ans:
            ans=N-cnt
    print(ans)
else:
    if K==1:
        print(N-2)
        exit()
    for a,b in l:
        C1=[10000 for i in range(N+1)]
        C2=[10000 for i in range(N+1)]
        X=[[a,0]]
        for q in range(10000):
            if q==len(X):
                break
            if C1[X[q][0]]==10000:
                C1[X[q][0]]=X[q][1]
                if X[q][1]<(K-1)//2:
                    for k in L[X[q][0]]:
                        if C1[k]==10000:
                            X.append([k,X[q][1]+1])
        Y=[[b,0]]
        for q in range(10000):
            if q==len(Y):
                break
            if C2[Y[q][0]]==10000:
                C2[Y[q][0]]=Y[q][1]
                if Y[q][1]<(K-1)//2:
                    for k in L[Y[q][0]]:
                        if C2[k]==10000:
                            Y.append([k,Y[q][1]+1])
        cnt=0
        for x in range(1,N+1):
            if C1[x]<=(K-1)//2 or C2[x]<=(K-1)//2:
                cnt+=1
        #print(a,b,cnt,C1,C2)
        if N-cnt<ans:
            ans=N-cnt
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=[[]for i in range(N+1)]
l=[]
for i in range(N-1):
    a,b=map(int,input().split())
    L[a].append(b)
    L[b].append(a)
    l.append([a,b])
ans=N
if K%2==0:
    for i in range(1,N+1):
        C=[-1 for i in range(N+1)]
        Q=[[i,0]]
        cnt=1
        for q in range(10000):
            if q==len(Q):
                break
            if C[Q[q][0]]==-1:
                C[Q[q][0]]=Q[q][1]
                cnt+=1
                if Q[q][1]<K//2:
                    for k in range(len(L[Q[q][0]])):
                        if C[k]==-1:
                            Q.append([k,Q[q][1]+1])
        if N-cnt<ans:
            ans=N-cnt
    print(ans)
else:
    for a,b in l:
        C1=[10000 for i in range(N+1)]
        C2=[10000 for i in range(N+1)]
        X=[[a,0]]
        for q in range(10000):
            if q==len(X):
                break
            if C1[X[q][0]]==10000:
                C1[X[q][0]]=X[q][1]
                if X[q][1]<(K-1)//2:
                    for k in L[X[q][0]]:
                        if C1[k]==10000:
                            X.append([k,X[q][1]+1])
        Y=[[b,0]]
        for q in range(10000):
            if q==len(Y):
                break
            if C2[Y[q][0]]==10000:
                C2[Y[q][0]]=Y[q][1]
                if Y[q][1]<(K-1)//2:
                    for k in L[Y[q][0]]:
                        if C2[k]==10000:
                            Y.append([k,Y[q][1]+1])
        cnt=0
        for x in range(1,N+1):
            if C1[x]<=(K-1)//2 or C2[x]<=(K-1)//2:
                cnt+=1
        #print(a,b,cnt,C1,C2)
        if N-cnt<ans:
            ans=N-cnt
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n,k = [int(i) for i in input().split()]
edges = [[int(i)-1 for i in input().split()] for j in range(n-1)]

node = [[] for i in range(n)]
for edge in edges:
    node[edge[0]].append(edge[1])
    node[edge[1]].append(edge[0])

M = 0
for i in range(n):
    count = 1
    que = deque()
    newque = deque()
    check = [False for i in range(n)]
    if k%2==1:
        check[i] = True
        que.append(i)
    elif i!=n-1:
        check[edges[i][0]]=True
        check[edges[i][1]]=True
        que.append(edges[i][0])
        que.append(edges[i][1])
        count = 2
    for j in range((k-1)//2):
        while len(que)!=0:
            q = que.pop()
            for l in node[q]:
                if check[l]==True:
                    continue
                newque.append(l)
                check[l] = True
                count += 1
        que = newque
        newque = deque()
    M = max(M,count)
print(n-M)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k = [int(i) for i in input().split()]
edges = [[] for _ in range(n)]

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

insides = 0
if k%2==0:     
    #各頂点始まり、で調べる
    for i in range(n):
        queue = deque([i])
        dist = [-1]*n
        dist[i] = 0
        
        #幅優先探索
        while queue:
            u = queue.popleft()
            d = dist[u]
            #幅優先探索ゆえ、k/2になったら終えてしまえばよい。
            if d==k//2:
                break

            nodes = edges[u]
            for v in nodes:
                if dist[v]==-1: #未踏.
                    dist[v] = d + 1
                    queue.append(v)

        cnt = 0
        for v in dist:
            if v != -1:
                cnt += 1
        insides = max(cnt, insides)
        
else:
    #各枝始まり、で調べる
    for i in range(n):
        for v in edges[i]: 
            queue = deque([i,v])
            dist = [-1]*n
            dist[i] = 0
            dist[v] = 0

        #幅優先探索
        while queue:
            u = queue.popleft()
            d = dist[u]
            #幅優先探索ゆえ、k/2になったら終えてしまえばよい。
            if d==k//2:
                break

            nodes = edges[u]
            for v in nodes:
                if dist[v]==-1: #未踏.
                    dist[v] = d + 1
                    queue.append(v)

        cnt = 0
        for v in dist:
            if v != -1:
                cnt += 1
        insides = max(cnt, insides)
        
    

print(n-insides)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k = [int(i) for i in input().split()]
edges = [[] for _ in range(n)]

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

insides = 0
if k%2==0:     
    #各頂点始まり、で調べる
    for i in range(n):
        queue = deque([i])
        dist = [-1]*n
        dist[i] = 0
        
        #幅優先探索
        while queue:
            u = queue.popleft()
            d = dist[u]
            #幅優先探索ゆえ、k/2になったら終えてしまえばよい。
            if d==k//2:
                break

            nodes = edges[u]
            for v in nodes:
                if dist[v]==-1: #未踏.
                    dist[v] = d + 1
                    queue.append(v)

        cnt = 0
        for v in dist:
            if v != -1:
                cnt += 1
        insides = max(cnt, insides)

print(n-insides)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
N,K=map(int,input().split())

L=[[]for i in range(N+1)]
for i in range(N-1):
    a,b=map(int,input().split())
    L[a].append((b,a))
    L[b].append((a,b))
    
#print(L)
def dfs(S):
    C=[10**5 for i in range(N+1)]
    C[S]=0
    A=L[S]
    for i in range(10**5):
        #print(A)
        #print(C)
        if len(A)<(i+1):
            break
        if C[A[i][0]]!=10**5:
            continue
        else:
            C[A[i][0]]=C[A[i][1]]+1
            for j in L[A[i][0]]:
                if C[j[0]]==10**5:
                    A.append(j)

    return C
array=[[]]
for i in range(1,N+1):
    array.append(dfs(i))
#print(array)
#直径
M=0
P=0
for i in range(1,N+1):
    if array[1][i]>M:
        P=i
        M=array[1][i]
#print(P)
M=max(array[P][1:])
#print(M)

if M%2==0:
    MAX=0
    for i in range(1,N+1):
        cnt=0
        for j in range(1,N+1):
            if array[i][j]<=(K//2):
                cnt+=1
        if MAX<cnt:
            MAX=cnt
    if K==1:
        print(N-2)
    else:
        print(N-MAX)
else:
    MAX=0
    for i in range(1,N+1):
        for j in L[i]:
            if j[0]<j[1]:
                cnt=0
                for k in range(1,N+1):
                    if min(array[j[0]][k],array[j[1]][k])<=(K//2):
                        cnt+=1
                if MAX<cnt:
                    MAX=cnt
    if K==1:
        print(N-2)
    else:
        print(N-MAX)

Submission status: Wrong Answer

Solution code:
n, k = [ int(v) for v in input().split() ]
node_list = [ [ int(v)-1 for v in input().split() ] for i in range(n-1) ]
node_type = [ 0 for i in range(n) ]
connect_list = [ [] for i in range(n) ]

for i in node_list:
    a, b = i
    connect_list[a].append(b)
    connect_list[b].append(a)
    node_type[a] += 1
    node_type[b] += 1

leaf_set = [ i for i in range(n) if node_type[i] == 1 ]


def bfs(p):
    search_list = [p]
    color_list = [ "W" for i in range(n) ]
    color_list[p] = "B"
    for _ in range(k):
        new_search_list = []
        for i in search_list:
            for j in connect_list[i]:
                if color_list[j] == "W":
                    color_list[j] = "B"
                    new_search_list.append(j)
        search_list = new_search_list
    return n - color_list.count("B")

ans_list = [ bfs(i) for i in leaf_set ]       
print(min(ans_list))

Submission status: Wrong Answer

Solution code:
from collections import deque

n, k = map(int, input().split())
graph = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

ans = 2000
max_distance = k // 2

if k % 2 == 0:
    for i in range(1, n+1):
        extra_node_cnt = 0
        q = deque()
        q.append((i, 0))
        seen = [0]*(n+1)
        seen[i] = 1
        while q:
            node, distance = q.popleft()
            if distance > max_distance:
                extra_node_cnt += 1
            for neighor_node in graph[node]:
                if seen[neighor_node] == 0:
                    seen[neighor_node] = 1
                    q.append((neighor_node, distance + 1))
        ans = min(ans, extra_node_cnt)
    print(ans)

else:
    for now_node in range(1, n+1):
        for neighor_now in graph[now_node]:
            extra_node_cnt = 0
            q = deque()
            q.append((now_node, 0))
            seen = [0]*(n+1)
            seen[now_node] = 1
            seen[neighor_now] = 1
            while q:
                node, distance = q.popleft()
                if distance > max_distance:
                    extra_node_cnt += 1
                for neighor_node in graph[node]:
                    if seen[neighor_node] == 0:
                        seen[neighor_node] = 1
                        q.append((neighor_node, distance + 1))
            q = deque()
            q.append((neighor_now, 0))
            seen = [0]*(n+1)
            seen[now_node] = 1
            seen[neighor_now] = 1
            while q:
                node, distance = q.popleft()
                if distance > max_distance+1:
                    extra_node_cnt += 1
                for neighor_node in graph[node]:
                    if seen[neighor_node] == 0:
                        seen[neighor_node] = 1
                        q.append((neighor_node, distance + 1))
            if extra_node_cnt != 0:
                ans = min(ans, extra_node_cnt)
    if ans == 2000:
        ans = 0
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, k = map(int, input().split())
graph = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

ans = float("inf")
max_distance = k // 2 if k % 2 == 0 else (k - 1) // 2
for i in range(1, n+1):
    extra_node_cnt = 0
    q = deque()
    q.append((i, 0))
    seen = [0]*(n+1)
    seen[i] = 1
    while q:
        node, distance = q.popleft()
        if distance > max_distance:
            extra_node_cnt += 1
        for neighor_node in graph[node]:
            if seen[neighor_node] == 0:
                seen[neighor_node] = 1
                q.append((neighor_node, distance + 1))
    ans = min(ans, extra_node_cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def computeDist(tree, nk, i):
    visited = [0] * (nk[0] + 1)
    queue = []
    visited[i] = 1
    queue.append(i)
    dist = [0] * (nk[0] + 1)
    dist[i] = 0
    while queue:
        s = queue.pop(0)
        for u in tree[s]:
            if not visited[u]:
                queue.append(u)
                visited[u] = 1
                dist[u] = dist[s] + 1
    return len([i for i in dist if i > nk[1] // 2])

def minimumNodesToBeRemoved(tree, nk):
    noOfNodesAtDistKBy2 = [0] * (nk[0] + 1)
    for i in range(1, nk[0] + 1):
        noOfNodesAtDistKBy2[i] = computeDist(tree, nk, i)
    return min(noOfNodesAtDistKBy2[1:])


def start():
    nk = [int(i) for i in input().split()]
    tree = defaultdict(list)
    for i in range(nk[0] - 1):
        uv = [int(i) for i in input().split()]
        tree[uv[0]].append(uv[1])
        tree[uv[1]].append(uv[0])
    return minimumNodesToBeRemoved(tree, nk)

print(start())

Submission status: Wrong Answer

Solution code:
import collections

def dfs(v,d):
  if len(g[v])==0:
    return 0
  for u in g[v]:
    if checked[u]==0:
      checked[u]=1
      dfs(u,d+1)
  print(v,d)

n,k=map(int,input().split())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
ans=0
for i in range(1,n+1):
  q=collections.deque()
  q.append((i,0))
  tmp=1
  checked=[0]*(n+1)
  checked[i]=1
  while len(q)!=0:
    v,d=q.popleft()
    if d>k:
      break
    l=len(g[v])
    for u in g[v]:
      if checked[u]==0:
        checked[u]=1
        if l!=1:
          q.append((u,d+2))
          if d+2<=k:
            tmp+=1
        else:
          q.append((u,d+1))
          if d+1<=k:
            tmp+=1
  ans=max(ans,tmp)
print(n-ans)

Submission status: Wrong Answer

Solution code:
import sys
n, k = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)

def bfs(start, nxt):
	vis = [-1 for _ in range(n)]
	vis[start] = 0
	vis[nxt] = 1
	s = [nxt]
	cnt = 1
	while s:
		l = s.pop()
		cnt += 1
		if vis[l] < k:
			for c in adj[l]:
				if vis[c] < 0:
					s.append(c)
					vis[c] = vis[l] + 1
	return n - cnt

ans = n-2
for i in range(n):
	for j in adj[i]:
		ans = min(ans, bfs(i, j))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]
n, k = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)

def bfs(start, nxt):
	vis = [-1 for _ in range(n)]
	vis[start] = 0
	vis[nxt] = 1
	s = [nxt]
	cnt = 1
	while s:
		l = s.pop()
		cnt += 1
		if vis[l] < k:
			for c in adj[l]:
				if vis[c] < 0:
					s.append(c)
					vis[c] = vis[l] + 1
	return n - cnt

ans = n-2
for i in range(n):
	for j in adj[i]:
		ans = min(ans, bfs(i, j))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
import numpy as np
edges = [[] for i in range(n)]

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



def dfs(v):
    visited  = []
    INF = float("inf")
    d=[INF for i in range(n)]
    q=[]
    d[v]=0
    q.append(v)
    while len(q):
        v=q.pop()
        visited.append(v)
        for i in edges[v]:
            if i not in visited:
                d[i] = d[v] + 1
                q.append(i)
            else:
                continue

    return d
INF = float("inf")
la = np.array(dfs(0))
a = la.argmax()
temp = np.array(dfs(a))
b = temp.argmax()
distfrom_a = dfs(a)
distfrom_b = dfs(b)
d = distfrom_a[b]

if k%2 == 0:
    mi = INF
    for i in range(n):
        center = i
        disi = dfs(i)
        c = 0
        for m in range(n):
            if disi[m] > k/2:
                c += 1
        if mi>c:
            mi = c
    print(mi)

else:
    mi = INF
    for i in range(n):
        center = i
        disi = dfs(i)
        c = 0
        for m in range(n):
            if disi[m] > (k-1/2):
                c += 1
        if mi>c:
            mi = c
    print(mi)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    alledge.append((a-1,b-1))
if n==2:
    print(0)
    exit()


def f(x,y,c):
    c += 1
    for e in edge[x]:
        if e == y:
            continue
        if c > d:
            tmp[0] +=1
        f(e,x,c)

sys.setrecursionlimit(4100000)

if k % 2 == 0:
    d = k//2
    ass = 10**9
    tmp = [0]
    for i in range(n):
        tmp[0] = 0
        f(i,-1,0)
        ass = min(ass,tmp[0])
    print(ass)
else:
    d = (k-1)//2
    ass = 10**9
    tmp = [0]
    for e1 in alledge:
        tmp[0] = 0
        f(e1[0],e1[1],0)
        f(e1[1],e1[0],0)
        ass = min(ass,tmp[0])
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    alledge.append((a-1,b-1))
if n==2:
    print(0)
    exit()


def f(x,y,c):
    c += 1
    for e in edge[x]:
        if e != y:
            if c > d:
                tmp[0] +=1
            f(e,x,c)

sys.setrecursionlimit(4100000)

if k % 2 == 0:
    d = k//2
    ass = 10**9
    tmp = [0]
    for i in range(n):
        tmp[0] = 0
        f(i,-1,0)
        ass = min(ass,tmp[0])
    print(ass)
else:
    d = (k-1)//2
    ass = 10**9
    tmp = [0]
    for e1 in alledge:
        tmp[0] = 0
        f(e1[0],e1[1],0)
        f(e1[1],e1[0],0)
        ass = min(ass,tmp[0])
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    alledge.append([a-1,b-1])
if n==2:
    print(0)
    exit()


def f(x,y,c):
    c += 1
    for e in edge[x]:
        if e != y:
            if c > d:
                used[e] = 1
            #f(e,x,c)


if k % 2 == 0:
    d = k//2
    res = []
    for i in range(n):
        used = [0]*n
        f(i,-1,0)
        res.append(sum(used))
    print(min(res))
else:
    d = (k-1)//2
    res = []
    for e1 in alledge:
        used = [0]*n
        f(e1[0],e1[1],0)
        f(e1[1],e1[0],0)
        res.append(sum(used))
    print(min(res))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    alledge.append([a-1,b-1])
if n==2:
    print(0)
    exit()


def f(x,y,c):
    c += 1
    for e in edge[x]:
        if e == y:
            continue
        if c > d:
            used[e] = 1
        f(e,x,c)



if k % 2 == 0:
    d = k//2
    res = []
    for i in range(n):
        used = [0]*n
        f(i,-1,0)
        res.append(sum(used))
    print(min(res))
else:
    d = (k-1)//2
    res = []
    for e1 in alledge:
        used = [0]*n
        #f(e1[0],e1[1],0)
        #f(e1[1],e1[0],0)
        res.append(sum(used))
    print(min(res))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    alledge.append((a-1,b-1))
if n==2:
    print(0)
    exit()


def f(x,y,c):
    c += 1
    for e in edge[x]:
        if e == y:
            continue
        if c > d:
            used[e] = 1
        f(e,x,c)
print(0)
exit()


if k % 2 == 0:
    d = k//2
    res = []
    for i in range(n):
        used = [0]*n
        f(i,-1,0)
        res.append(sum(used))
    print(min(res))
else:
    d = (k-1)//2
    res = []
    for e1 in alledge:
        used = [0]*n
        f(e1[0],e1[1],0)
        f(e1[1],e1[0],0)
        res.append(sum(used))
    print(min(res))

Submission status: Wrong Answer

Solution code:
import copy

N, K = (int(i) for i in input().split())

G = [[] for i in range(N)]
E = []
for i in range(N-1):
    A, B = (int(i) for i in input().split())
    E.append((A-1, B-1))
    G[A-1].append(B-1)
    G[B-1].append(A-1)

def DFS(u, n, G):
    q = [u]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] = 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2]:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)

def DFS_E(u, v, n, G):
    q = [u, v]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] = 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2] and u2 != v:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)


if K % 2 == 0:
    ans = 0
    for v in range(N):
        ans = max(ans, DFS(v, K//2, G))
    print(N-ans)
else:
    ans = 0
    for u, v in E:
        ans = max(ans, DFS_E(u,v,(K-1)//2,G))
    print(N-ans)

Submission status: Wrong Answer

Solution code:
import copy

N, K = (int(i) for i in input().split())

G = [[] for i in range(N)]
E = []
for i in range(N-1):
    A, B = (int(i) for i in input().split())
    E.append((A-1, B-1))
    G[A-1].append(B-1)
    G[B-1].append(A-1)

def DFS(u, n, G):
    q = [u]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] += 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2]:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)

def DFS_E(u, v, n, G):
    q = [u, v]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] += 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2] and u2 != v:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)


if K % 2 == 0:
    ans = 0
    for v in range(N):
        ans = max(ans, DFS(v, K//2, G))
    print(N-ans)
else:
    ans = 0
    for u, v in E:
        ans = max(ans, DFS_E(u,v,K//2,G))
    print(N-ans)

Submission status: Wrong Answer

Solution code:
import copy

N, K = (int(i) for i in input().split())

G = [[] for i in range(N)]
E = []
for i in range(N-1):
    A, B = (int(i) for i in input().split())
    E.append((A-1, B-1))
    G[A-1].append(B-1)
    G[B-1].append(A-1)

def DFS(u, n, G):
    q = [u]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] += 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2]:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)

def DFS_E(u, v, n, G):
    return 0

if K % 2 == 0:
    ans = 0
    for v in range(N):
        ans = max(ans, DFS(v, K//2, G))
    print(N-ans)
else:
    ans = 0
    for u, v in E:
        ans = max(ans, DFS_E(u,v,K//2,G))
    print(N-ans)

Submission status: Wrong Answer

Solution code:
import copy

N, K = (int(i) for i in input().split())

G = [[] for i in range(N)]
E = []
for i in range(N-1):
    A, B = (int(i) for i in input().split())
    E.append((A-1, B-1))
    G[A-1].append(B-1)
    G[B-1].append(A-1)

def DFS(u, n, G):
    q = [u]
    v = [0]*N
    d = [0]*N
    while q:
        u1 = q.pop()
        v[u1] += 1
        if d[u1] < n:
            for u2 in G[u1]:
                if not v[u2]:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)

def DFS_E(u, v, n, G):
    G_copy = copy.deepcopy(G)
    G_copy[u].append(N)
    G_copy[v].append(N)
    G_copy.append([u, v])
    q = [N]
    v = [0]*(N+1)
    d = [0]*(N+1)
    while q:
        u1 = q.pop()
        v[u1] += 1
        if d[u1] < n:
            for u2 in G_copy[u1]:
                if not v[u2]:
                    d[u2] = d[u1] + 1
                    q.append(u2)
    return sum(v)

if K % 2 == 0:
    ans = 0
    for v in range(N):
        ans = max(ans, DFS(v, K//2, G))
    print(N-ans)
else:
    ans = 0
    for u, v in E:
        ans = max(ans, DFS_E(u,v,K//2,G))
    print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)


t=K//2
s=K%2
def dfs(i):
    visit=[-1]*N
    h=deque()
    h.append(i)
    visit[i]=0
    num=0
    a=0
    while h:
        x=h.popleft()
        for y in table[x]:
            if visit[y]==-1:
                visit[y]=visit[x]+1
                h.append(y)
    for k in range(N):
        if 0<=visit[k]<=t:
            num+=1
        if visit[k]==t and s==1:
            b=0
            for z in table[k]:
                if visit[z]==t+1:
                    b+=1
            a=max(a,b)
    #print(a,num,visit)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)


t=K//2
s=K%2
def dfs(i):
    visit=[-1]*N
    h=deque()
    h.append(i)
    visit[i]=0
    num=0
    a=0
    while h:
        x=h.popleft()
        for y in table[x]:
            if visit[y]==-1:
                visit[y]=visit[x]+1
                h.append(y)
    for k in range(N):
        if 0<=visit[k]<=t:
            num+=1
        if visit[k]==t and s==1:
            b=0
            for z in table[k]:
                if visit[z]==k+1:
                    b+=1
            a=max(a,b)
    #print(a,num,visit)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)


t=K//2
s=K%2
def dfs(i):
    visit=[-1]*N
    h=deque()
    h.append(i)
    visit[i]=0
    num=0
    a=0
    while h:
        x=h.popleft()
        for y in table[x]:
            if visit[y]==-1:
                visit[y]=visit[x]+1
                h.append(y)
    for k in range(N):
        if 0<=visit[k]<=t:
            num+=1
        if visit[k]==t and s==1:
            a=max(a,len(table[k])-1)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)


t=K//2
s=K%2
def dfs(i):
    visit=[-1]*N
    h=deque()
    h.append(i)
    visit[i]=0
    num=0
    a=0
    while h:
        x=h.popleft()
        #if visit[x]>t:
            #continue
        for y in table[x]:
            if visit[y]==-1:
                visit[y]=visit[x]+1
                h.append(y)
    for k in range(N):
        if 0<=visit[k]<=t:
            num+=1
        if visit[k]==t and s==1:
            for z in table[k]:
                a=max(a,len(table[k])-1)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)


t=K//2
s=K%2
def dfs(i):
    visit=[-1]*N
    h=deque()
    h.append(i)
    visit[i]=0
    num=0
    a=0
    while h:
        x=h.popleft()
        if visit[x]>t:
            continue
        for y in table[x]:
            if visit[y]==-1:
                visit[y]=visit[x]+1
                h.append(y)
    for k in range(N):
        if 0<=visit[k]<=t:
            num+=1
        if visit[k]==t and s==1:
            for z in table[k]:
                a=max(a,len(table[k])-1)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)

t=K//2
s=K%2
def dfs(i):
    visit=[False]*N
    h=deque()
    h.append((i,0))
    visit[i]=True
    num=0
    a=0
    while h:
        x,q=h.popleft()
        if q>t:
            continue
        num+=1
        if s==1 and q==t:
            b=0
            for z in table[x]:
                if not visit[z]:
                    b+=1
            a=max(a,b)
        for y in table[x]:
            if not visit[y]:
                visit[y]=True
                h.append((y,q+1))
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import math
N,K=map(int,input().split())
table=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    a,b=a-1,b-1
    table[a].append(b)
    table[b].append(a)

t=K//2
s=K%2
def dfs(i):
    visit=[False]*N
    h=deque()
    h.append((i,0))
    visit[i]=True
    num=0
    a=0
    while h:
        x,q=h.popleft()
        if q>t:
            break
        num+=1
        for y in table[x]:
            if not visit[y]:
                visit[y]=True
                h.append((y,q+1))
        if s==1 and q==t:
            b=0
            for z in table[x]:
                if not visit[z]:
                    b+=1
            a=max(a,b)
    return a+num

ans=0
for i in range(N):
    ans=max(ans,dfs(i))
print(N-ans)