submission_id string | problem_id string | status string | code string | input string | output string | problem_description string |
|---|---|---|---|---|---|---|
s308118789 | p04049 | Time Limit Exceeded | import sys
input = sys.stdin.readline
def main():
n, k = map(int, input().split())
T = [[] for _ in range(n)]
E = []
for _ in range(n-1):
a, b = map(int, input().split())
a -= 1
b -= 1
T[a].append(b)
T[b].append(a)
E.append((a, b))
d = k//2
def dfs(vs):
dist = [-1]*n
stack = []
cnt = 0
for v in vs:
stack.append(v)
dist[v] = 0
while stack:
v = stack.pop()
for nv in T[v]:
if dist[nv] == -1:
dist[nv] = dist[v] + 1
if dist[nv] > d:
cnt += 1
stack.append(nv)
return cnt
ans = n
if k%2 == 0:
for i in range(n):
ans = min(ans, dfs([i]))
else:
for l in E:
ans = min(ans, dfs(l))
print(ans)
if __name__ == "__main__":
main() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s808680052 | p04049 | Time Limit Exceeded | n, k = map(int, input().split())
T = [[] for _ in range(n)]
E = []
for _ in range(n-1):
a, b = map(int, input().split())
a -= 1
b -= 1
T[a].append(b)
T[b].append(a)
E.append((a, b))
d = k//2
def dfs(vs):
dist = [-1]*n
stack = []
cnt = 0
for v in vs:
stack.append(v)
dist[v] = 0
while stack:
v = stack.pop()
for nv in T[v]:
if dist[nv] == -1:
dist[nv] = dist[v] + 1
if dist[nv] > d:
cnt += 1
stack.append(nv)
return cnt
ans = n
if k%2 == 0:
for i in range(n):
ans = min(ans, dfs([i]))
else:
for l in E:
ans = min(ans, dfs(l))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s644859872 | p04049 | Time Limit Exceeded | #!/usr/bin/env python3
import sys
input = sys.stdin.readline
from collections import deque
n, k = map(int, input().split())
radius = k // 2
edge = [[] for _ in range(n)]
uv = []
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1; b -= 1
edge[a].append(b)
edge[b].append(a)
uv.append((a, b))
def dfs(p, v, d):
ret = 0
st = []
st.append((p, v, d))
while st:
p, v, d = st.pop()
if d > radius:
ret += 1
for nv in edge[v]:
if nv == p:
continue
st.append((v, nv, d+1))
return ret
ans = n
if k % 2 == 0:
for i in range(n):
ret = dfs(-1, i, 0)
ans = min(ans, ret)
else:
for u, v in uv:
ret = dfs(u, v, 0) + dfs(v, u, 0)
ans = min(ans, ret)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s954425719 | p04049 | Time Limit Exceeded | import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
e = dd(list)
for _ in range(N - 1):
a, b = map(int, input().split())
e[a].append(b)
e[b].append(a)
d = [[N for _ in range(N + 1)] for _ in range(N + 1)]
for s in range(1, N + 1):
Q = dq([s])
d[s][s] = 0
while len(Q):
p = Q.popleft()
for q in e[p]:
if d[s][q] > d[s][p] + 1:
d[s][q] = d[s][p] + 1
Q.append(q)
vis = set()
table = [[0] * (N + 1) for _ in range(N + 1)]
lntable = [0] * (N + 1)
for i in range(1, N + 1):
for j in range(1, N + 1):
if d[i][j] > K:
table[i][j] = 1
lntable[i] += 1
while True:
t = 0
for i in range(1, N + 1):
if i in vis: continue
if lntable[i] > lntable[t]: t = i
if t == 0: break
for i in range(1, N + 1):
if table[i][t] == 1:
table[i][t] = 0
lntable[i] -= 1
vis.add(t)
print(len(vis)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s423436289 | p04049 | Time Limit Exceeded | import sys
from collections import defaultdict as dd
from collections import deque as dq
input = sys.stdin.readline
N, K = map(int, input().split())
e = dd(list)
for _ in range(N - 1):
a, b = map(int, input().split())
e[a].append(b)
e[b].append(a)
d = [[N for _ in range(N + 1)] for _ in range(N + 1)]
for s in range(1, N + 1):
Q = dq([s])
d[s][s] = 0
while len(Q):
p = Q.popleft()
for q in e[p]:
if d[s][q] > d[s][p] + 1:
d[s][q] = d[s][p] + 1
Q.append(q)
vis = set()
table = [set() for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(1, N + 1):
if d[i][j] > K: table[i].add(j)
while True:
t = 0
for i in range(1, N + 1):
if i in vis: continue
if len(table[i]) > len(table[t]): t = i
if t == 0: break
for i in range(1, N + 1):
table[i].discard(t)
vis.add(t)
print(len(vis)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s590765428 | p04049 | Time Limit Exceeded | 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 _ 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 = []
check = [False]*n
if k%2==0:
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//2):
newque = []
while len(que)!=0:
q = que.pop()
for l in node[q]:
if check[l]:
continue
newque.append(l)
check[l] = True
count += 1
que = newque
M = max(M,count)
print(n-M) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s729496429 | p04049 | Time Limit Exceeded | import sys
input = sys.stdin.readline
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 = []
check = [False for i in range(n)]
if k%2==0:
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//2):
newque = []
while len(que)!=0:
q = que.pop()
for l in node[q]:
if check[l]:
continue
newque.append(l)
check[l] = True
count += 1
que = newque
M = max(M,count)
print(n-M) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s243620914 | p04049 | Time Limit Exceeded | 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 = []
newque = []
check = [False for i in range(n)]
if k%2==0:
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//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 = []
M = max(M,count)
print(n-M) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s253510799 | p04049 | Time Limit Exceeded | 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==0:
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//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) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s440258571 | p04049 | Time Limit Exceeded | import collections
n,k=map(int,input().split())
g=[[] for _ in range(n+1)]
e=[]
for _ in range(n-1):
a,b=map(int,input().split())
g[a].append(b)
g[b].append(a)
e.append((a,b))
ans=0
if k%2==0:
for i in range(1,n+1):
tmp=0
checked=[0]*(n+1)
checked[i]=1
q=collections.deque()
q.append((i,0))
while len(q)!=0:
v,d=q.popleft()
if d<=k//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
ans=max(ans,tmp)
print(n-ans)
if k%2==1:
for v1,v2 in e:
tmp=0
checked=[0]*(n+1)
checked[v1]=1
checked[v2]=1
q=collections.deque()
q.append((v1,0))
while len(q)!=0:
v,d=q.popleft()
if d<=k//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
checked=[0]*(n+1)
checked[v1]=1
checked[v2]=1
q=collections.deque()
q.append((v2,0))
while len(q)!=0:
v,d=q.popleft()
if d<=(k-1)//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
ans=max(ans,tmp)
print(n-ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s754287392 | p04049 | Time Limit Exceeded | N, K = map(int, input().split()) # 横に2個
graph = [[] for _ in range(N)]
for _ in range(N-1):
a, b = map(int, input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
#print(graph)
dis = [[] for _ in range(N)]
for i in range(N):
d = 0
checked = [False for _ in range(N)]
checked[i] = True
qs = [i]
#print(i)
while qs:
qqs = []
for p in qs:
for k in graph[p]:
if not checked[k]:
checked[k] = True
qqs.append(k)
d += 1
if d > K and qqs:
#print(d, qqs)
for l in qqs:
dis[i].append(l)
qs = qqs
ans = 0
while True:
end = True
max = 0
for i, d in enumerate(dis):
if len(d) > 0:
end = False
if len(d) > max:
rem = i
max = len(d)
if end:
break
for p in dis[rem]:
dis[p].remove(rem)
dis[rem] = []
#print(rem)
ans += 1
#print(dis)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s582994160 | p04049 | Time Limit Exceeded | # coding:utf-8
import sys
from collections import defaultdict, deque
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
def main():
n, k = LI()
G = defaultdict(list)
AB = [LI_() for _ in range(n - 1)]
for a, b in AB:
G[a].append(b)
G[b].append(a)
def DFS(ss: list, t: int):
# print([s + 1 for s in ss])
q = deque()
for s in ss:
q.append((s, 0))
visited = set(ss)
cnt = 0
while q:
v, d = q.pop()
visited.add(v)
# print(v + 1, d)
for to in G[v]:
if to in visited:
continue
if d + 1 > t:
cnt += 1
q.append((to, d + 1))
# print(cnt)
return cnt
# 木の性質
# 直径をdとした時
# dが偶数ならば,ある頂点vから他の頂点への距離がD/2となる頂点vが存在する
# dが奇数ならば,ある辺eから他の頂点への距離が(D-1)/2となる辺eが存在する
res = INF
if k % 2:
for a, b in AB:
res = min(res, DFS([a, b], (k - 1) // 2))
else:
for i in range(n):
res = min(res, DFS([i], k // 2))
return res
print(main())
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s351572521 | p04049 | Time Limit Exceeded | 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))
def f(x,y,c):
global tmp
c += 1
for e in edge[x]:
if e == y:
continue
if c > d:
tmp +=1
f(e,x,c)
sys.setrecursionlimit(4100000)
if k % 2 == 0:
d = k//2
ass = 2001
for i in range(n):
tmp = 0
f(i,-1,0)
ass = min(ass,tmp)
print(ass)
else:
d = (k-1)//2
ass = 2001
for e1 in alledge:
tmp = 0
f(e1[0],e1[1],0)
f(e1[1],e1[0],0)
ass = min(ass,tmp)
print(ass)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s515788976 | p04049 | Time Limit Exceeded | 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(ass)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s334561614 | p04049 | Time Limit Exceeded | 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(5000000)
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(ass) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s008057845 | p04049 | Time Limit Exceeded | import sys
sys.setrecursionlimit(5000000)
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 and e >= 0 and e<= n-1:
used[e] = 1
if len(edge[e])>=2:
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))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s029596429 | p04049 | Time Limit Exceeded | from collections import deque
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans,c = [[] for i in range(n)],[[] for i in range(n)],n,0
for i,j in b:
x[i].append(j)
x[j].append(i)
for i in x: c = max(len(i),c)
if n-c+1<=k: ans = 0
elif k==1: ans = n-2
else:
for i in range(n):
q,v = deque(),[1]*n
v[i] = 0
for j in x[i]:
q.append((j,1,j))
d[i].append(j)
v[j] = 0
while q:
p = q.pop()
if p[1]<k//2:
for j in x[p[0]]:
if v[j]:
q.append((j,p[1]+1,p[2]))
d[i].append(p[2])
v[j] = 0
if k%2:
for i,j in b: ans = min(ans,n-len(d[i])-len(d[j])+d[i].count(j)+d[j].count(i)-2)
else:
for i in range(n): ans = min(ans,n-len(d[i])-1)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s991677900 | p04049 | Time Limit Exceeded | from collections import deque
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans = [[] for i in range(n)],[[] for i in range(n)],n
for i,j in b:
x[i].append(j)
x[j].append(i)
def f(s):
for i in range(n):
q,v = deque(),[1]*n
v[i] = 0
for j in x[i]:
q.append((j,1,j))
d[i].append(j)
v[j] = 0
while q:
p = q.pop()
if p[1]<s:
for j in x[p[0]]:
if v[j]:
q.append((j,p[1]+1,p[2]))
d[i].append(p[2])
v[j] = 0
if k==1: ans = n-2
elif k%2:
f((k-1)//2)
for i in range(n-1):
d1,d2 = d[b[i][0]],d[b[i][1]]
ans = min(ans,n-len(d1)-len(d2)+d1.count(b[i][1])+d2.count(b[i][0])-2)
else:
f(k//2)
for i in range(n): ans = min(ans,n-len(d[i])-1)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s027490480 | p04049 | Time Limit Exceeded | n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans = [[] for i in range(n)],[[None]*n for i in range(n)],n
for i,j in b:
x[i].append(j)
x[j].append(i)
for i in range(n):
q,v,d[i][i] = [],[1]*n,(0,-1)
v[i] = 0
for j in x[i]:
q.append((j,1,j))
d[i][j],v[j] = (1,j),0
while q:
p = q.pop()
for j in x[p[0]]:
if v[j]:
q.append((j,p[1]+1,p[2]))
d[i][j],v[j] = (p[1]+1,p[2]),0
if k%2:
for i in range(n-1):
a = n
for j,j2 in d[b[i][0]]:
if b[i][1]!=j2 and (k-1)//2>=j: a-=1
for j,j2 in d[b[i][1]]:
if b[i][0]!=j2 and (k-1)//2>=j: a-=1
ans = min(a,ans)
else:
for i in range(n):
a = n
for j,j2 in d[i]:
if j<=k//2: a-=1
ans = min(a,ans)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s696276840 | p04049 | Time Limit Exceeded | from bisect import bisect_left
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans = [[] for i in range(n)],[[None]*n for i in range(n)],n
for i,j in b:
x[i].append(j)
x[j].append(i)
for i in range(n):
q,v,d[i][i] = [],[1]*n,(0,-1)
v[i] = 0
for j in x[i]:
q.append((j,1,j))
d[i][j],v[j] = (1,j),0
while q:
p = q.pop()
for j in x[p[0]]:
if v[j]:
q.append((j,p[1]+1,p[2]))
d[i][j],v[j] = (p[1]+1,p[2]),0
if k%2:
for i in range(n-1):
a = n
for j,j2 in d[b[i][0]]:
if b[i][1]!=j2 and (k-1)//2>=j: a-=1
for j,j2 in d[b[i][1]]:
if b[i][0]!=j2 and (k-1)//2>=j: a-=1
ans = min(a,ans)
else:
for i in range(n): ans = min(ans,n-bisect_left(sorted(d[i]),(k//2+1,0)))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s289601341 | p04049 | Time Limit Exceeded | import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
from collections import deque
def bfs(graph:list, start:int) -> list:
# 未探索のノードは距離null
node = len(graph)
dist = [None]*node
# 始点ノードの距離を0とし、bfsのためのqueueを作成
dist[start] = 0
que = deque([(0,start)])
# 未探索のノードをqueueに入れる
# kより大きいの距離のものを数える
while que:
cost, cur_node = que.popleft()
for nex_cost, nex_node in graph[cur_node]:
if dist[nex_node] is not None:
continue
else:
dist[nex_node] = dist[cur_node] + nex_cost
que.append((dist[nex_node], nex_node))
return dist
# 入力, グラフ作成
n,k = li()
adj_list = [[] for _ in range(n)]
edges = []
for _ in range(n-1):
a,b = li_()
adj_list[a].append((1,b))
adj_list[b].append((1,a))
edges.append((a,b))
ans = n
# kが奇数の時
if k%2:
for a,b in edges:
dist1 = bfs(adj_list, a)
dist2 = bfs(adj_list, b)
ans = min(ans, sum([min(d1,d2) > (k-1)//2 for d1,d2 in zip(dist1, dist2)]))
# kが偶数の時
else:
for st in range(n):
dist = bfs(adj_list, st)
ans = min(ans, sum([d > k//2 for d in dist]))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s691147826 | p04049 | Time Limit Exceeded | import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
from collections import deque
def bfs(graph:list, node:int, start:int, k:int) -> list:
# 未探索のノードは距離INF
INF = float("inf")
dist = [INF]*node
# 始点ノードの距離を0とし、dfsのためのstackを作成
dist[start] = 0
que = deque([(0,start)])
# 未探索のノードをqueueに入れる
# kより大きいの距離のものを数える
cnt = 0
while que:
cost, cur_node = que.popleft()
if cost > k:
cnt += 1
for nex_cost, nex_node in graph[cur_node]:
if dist[nex_node] != INF:
continue
else:
dist[nex_node] = dist[cur_node] + nex_cost
que.append((dist[nex_node], nex_node))
return dist,cnt
# 入力, グラフ作成
n,k = li()
adj_list = [[] for _ in range(n)]
for _ in range(n-1):
a,b = li_()
adj_list[a].append((1,b))
adj_list[b].append((1,a))
# bfs
ans = n
if k%2 == 0:
for st in range(n):
_, cnt = bfs(adj_list, n, st, k//2)
ans = min(ans, cnt)
else:
for st in range(n):
for cost, to in adj_list[st]:
if to <= st:
continue
dist1, _ = bfs(adj_list, n, st, (k-1)//2)
dist2, _ = bfs(adj_list, n, to, (k-1)//2)
cnt = 0
for d1, d2 in zip(dist1, dist2):
if min(d1,d2) > (k-1)//2:
cnt += 1
ans = min(ans, cnt)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s265269213 | p04049 | Time Limit Exceeded | N,K=map(int,input().split())
G=[[] for i in range(N)]
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
for i in range(N-1):
a[i],b[i]=map(int,input().split())
a[i]-=1;b[i]-=1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
P=[-1 for i in range(N)]
rnk=[-1 for i in range(N)]
q=[0]
rnk[0]=0
while(len(q)>0):
r=q[-1];q.pop()
for p in G[r]:
if rnk[p]!=-1:
continue
rnk[p]=rnk[r]+1
P[p]=r
q.append(p)
S=[[] for i in range(N)]
for i in range(N):
S[i].append(P[i])
while(True):
tmp=[0 for i in range(N)]
flag=0
for i in range(N):
if S[i][-1]==-1:
tmp[i]=-1
else:
flag=1
tmp[i]=S[S[i][-1]][-1]
if flag==0:
break
for i in range(N):
S[i].append(tmp[i])
#pのk個上(存在しない場合は-1)
def bef(p,k):
if rnk[p]<k:
return -1
else:
m=str(bin(k))[2:][::-1]
n=len(m)
res=p
for i in range(n):
if m[i]=="1":
res=S[res][i]
return res
M=len(S[0])
def dist(x,y):
if rnk[x]>rnk[y]:
return (rnk[x]-rnk[y])+dist(bef(x,rnk[x]-rnk[y]),y)
elif rnk[x]<rnk[y]:
return (rnk[y]-rnk[x])+dist(x,bef(y,rnk[y]-rnk[x]))
else:
if x==y:
return 0
res=0
tmpx=x
tmpy=y
while(True):
j=0
if tmpx==tmpy:
return res
for i in range(M-1):
if S[tmpx][i]!=S[tmpy][i] and S[tmpx][i+1]==S[tmpy][i+1]:
j=i
break
res+=2**(j+1)
tmpx=S[tmpx][i]
tmpy=S[tmpy][i]
'''
X=max(rnk)
leaf=[]
for i in range(N):
if rnk[i]==X:
leaf.append(i)
'''
d=[[dist(i,j) for i in range(N)] for j in range(N)]
#dia=max([max([d[i][j] for i in range(N)]) for j in range(N)])
if K%2==0:
t=[[d[i][j] for i in range(N)] for j in range(N)]
D=K//2
ans=[0 for i in range(N)]
for i in range(N):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans))
else:
t=[[min([d[a[i]][j],d[b[i]][j]]) for j in range(N)] for i in range(N-1)]
ans=[0 for i in range(N-1)]
D=(K-1)//2
for i in range(N-1):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s501876519 | p04049 | Time Limit Exceeded | N,K=map(int,input().split())
G=[[] for i in range(N)]
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
for i in range(N-1):
a[i],b[i]=map(int,input().split())
a[i]-=1;b[i]-=1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
P=[-1 for i in range(N)]
rnk=[-1 for i in range(N)]
q=[0]
rnk[0]=0
while(len(q)>0):
r=q[-1];q.pop()
for p in G[r]:
if rnk[p]!=-1:
continue
rnk[p]=rnk[r]+1
P[p]=r
q.append(p)
S=[[] for i in range(N)]
for i in range(N):
S[i].append(P[i])
while(True):
tmp=[0 for i in range(N)]
flag=0
for i in range(N):
if S[i][-1]==-1:
tmp[i]=-1
else:
flag=1
tmp[i]=S[S[i][-1]][-1]
if flag==0:
break
for i in range(N):
S[i].append(tmp[i])
#pのk個上(存在しない場合は-1)
def bef(p,k):
if rnk[p]<k:
return -1
else:
m=str(bin(k))[2:][::-1]
n=len(m)
res=p
for i in range(n):
if m[i]=="1":
res=S[res][i]
return res
M=len(S[0])
def dist(x,y):
if rnk[x]>rnk[y]:
return (rnk[x]-rnk[y])+dist(bef(x,rnk[x]-rnk[y]),y)
elif rnk[x]<rnk[y]:
return (rnk[y]-rnk[x])+dist(x,bef(y,rnk[y]-rnk[x]))
else:
if x==y:
return 0
res=0
tmpx=x
tmpy=y
while(True):
j=0
if tmpx==tmpy:
return res
for i in range(M-1):
if S[tmpx][i]!=S[tmpy][i] and S[tmpx][i+1]==S[tmpy][i+1]:
j=i
break
res+=2**(j+1)
tmpx=S[tmpx][i]
tmpy=S[tmpy][i]
'''
X=max(rnk)
leaf=[]
for i in range(N):
if rnk[i]==X:
leaf.append(i)
'''
d=[[dist(i,j) for i in range(N)] for j in range(N)]
#dia=max([max([d[i][j] for i in range(N)]) for j in range(N)])
if K%2==0:
t=[[d[i][j] for i in range(N)] for j in range(N)]
D=K//2
ans=[0 for i in range(N)]
for i in range(N):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans))
else:
t=[[min([d[a[i]][j],d[b[i]][j]]) for j in range(N)] for i in range(N-1)]
ans=[0 for i in range(N-1)]
D=(K-1)//2
for i in range(N-1):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s860403732 | p04049 | Time Limit Exceeded | from collections import deque
N, K = map(int, input().split())
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append([a, b])
def bfs(n):
visited = [False] * N
dist = [0] * N
queue = deque([n])
while queue:
node = queue.pop()
if visited[node]:
continue
visited[node] = True
for n in T[node]:
if not visited[n]:
dist[n] = dist[node] + 1
queue.appendleft(n)
return dist
dist = []
for i in range(N):
dist.append(bfs(i))
ans = float('inf')
if K % 2 == 0:
for i in range(N):
ans = min(ans, len([d for d in dist[i] if d > K / 2]))
else:
for a, b in E:
ans = min(ans, len([d for d in [min(d1, d2) for d1, d2 in zip(dist[a], dist[b])] if d> (K-1) / 2]))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s380088946 | p04049 | Time Limit Exceeded | from collections import deque
N, K = map(int, input().split())
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def bfs(n):
visited = [False] * N
dist = [0] * N
queue = deque([n])
while queue:
node = queue.pop()
if visited[node]:
continue
visited[node] = True
for n in T[node]:
if not visited[n]:
dist[n] = dist[node] + 1
queue.appendleft(n)
return dist
dist = []
for i in range(N):
dist.append(bfs(i))
ans = float('inf')
if K % 2 == 0:
# 全ての頂点について全探索
for i in range(N):
ans = min(ans, len([d for d in dist[i] if d > K / 2]))
else:
# 全ての辺について全探索
for a, b in E:
adist = [(1 if min(d1, d2) > (K-1) / 2 else 0) for d1, d2 in zip(dist[a], dist[b])]
ans = min(ans, sum(adist))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s723801958 | p04049 | Time Limit Exceeded | N, K = map(int, input().split())
E = [set() for i in range(N+1)]
Er = []
for i in range(N-1):
a, b = map(int, input().split())
E[a].add(b)
E[b].add(a)
Er.append({a, b})
ma = 0
Start = []
if K&1 == 1:
Start = Er
else:
Start = [{i} for i in range(1, N+1)]
for st in Start:
L = list(st)
Closed = st
for j in range(K//2):
S = []
for l in L:
S += E[l]
L = []
for s in S:
if s not in Closed:
L.append(s)
Closed.add(s)
ma = max(ma, len(Closed))
print(N-ma)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s894233622 | p04049 | Time Limit Exceeded | import sys
sys.setrecursionlimit(5000)
def dfs(v, parent, rest, edge):
ret = 0
for c in edge[v]:
if c != parent:
ret += dfs(c, v, rest - 1, edge)
return ret + (rest <= 0)
def main(N, K, edge):
ans = N
if K % 2 == 0:
for v in range(N):
ans = min(ans, sum(dfs(c, v, K // 2, edge) for c in edge[v]))
else:
for v in range(N):
for c in edge[v]:
ans = min(ans, dfs(v, c, K // 2 + 1, edge) + dfs(c, v, K // 2 + 1, edge))
print(ans)
if __name__ == '__main__':
N, K = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(N - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
edge[u].append(v)
edge[v].append(u)
main(N, K, edge)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s199073669 | p04049 | Time Limit Exceeded | o=lambda:map(int,raw_input().split())
T,F=True,False
n,k=o()
e=[[] for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
a=10**5
def dfs(x,f):
if f>=0:l[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=T
for w in e[v]:
if ~l[w]:q+=[(w,c+1)]
return n-l.count(T)
if k%2:
for i in range(n):
for j in e[i]:
if i<j:l=[F]*n;dfs(i,j);a=min(a,dfs(j,i))
else:
for i in range(n):l=[F]*n;a=min(a,dfs(i,-1))
print a
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s144068762 | p04049 | Time Limit Exceeded | o=lambda:map(int,raw_input().split())
T,F=True,False
n,k=o()
e=[[] for _ in [0]*n]
exec('a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1];'*(n-1))
a=10**5
def dfs(x,f):
if f>=0:l[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=T
for w in e[v]:
if ~l[w]:q+=[(w,c+1)]
return n-l.count(T)
if k%2:
for i in range(n):
for j in e[i]:
if i<j:l=[F]*n;dfs(i,j);a=min(a,dfs(j,i))
else:
for i in range(n):l=[F]*n;a=min(a,dfs(i,-1))
print a
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s888980912 | p04049 | Time Limit Exceeded | ii=lambda:map(int,raw_input().split())
n,k=ii()
e=[[] for _ in range(n)]
for _ in range(n-1):
a,b=ii()
e[a-1]+=[b-1];e[b-1]+=[a-1]
cost=[0]*n
ans=10**5
def dfs(x,f):
if f>=0:vis[f]=True
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
cost[v]=c;vis[v]=True
for w in e[v]:
if not vis[w]:q+=[(w,c+1)]
return n-vis.count(True)
if k%2:
for i in range(n):
for j in e[i]:
if i<j:vis=[False]*n;dfs(i,j);ans=min(ans,dfs(j,i))
print ans
else:
for i in range(n):vis=[False]*n;ans=min(ans,dfs(i,-1))
print ans
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s305401795 | p04049 | Time Limit Exceeded | ii=lambda:map(int,raw_input().split())
n,k=ii()
e=[[] for _ in range(n)]
for _ in range(n-1):
a,b=ii()
e[a-1]+=[b-1];e[b-1]+=[a-1]
cost=[0]*n
ans=10**5
visit=[0]
def dfs(x,f):
global visit
del visit[:]
visit+=[f]
q=[(x,0)]
while len(q):
v,c=q.pop(0);cost[v]=c;visit+=[v]
for w in e[v]:
if w not in visit:q+=[(w,c+1)]
return len([i for i in cost if i>k/2])
if k%2:
for i in range(n):
for j in e[i]:
if i<j:dfs(i,j);ans=min(ans,dfs(j,i))
print ans
else:
for i in range(n):
ans=min(ans,dfs(i,-1))
print ans
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s057601498 | p04049 | Time Limit Exceeded | from sys import setrecursionlimit
setrecursionlimit(100000)
def rad_reach(b,h,dist,E,rad):
cnt = 1
if dist < rad:
for nxt in E[h]:
if nxt == b:
continue
cnt += rad_reach(h,nxt,dist+1,E,rad)
return cnt
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [[] for i in xrange(N)]
for i in xrange(N-1):
a,b = map(lambda x:int(x)-1,raw_input().split())
E[a].append(b)
E[b].append(a)
pairs.append((a,b))
ans = N
rad = K/2
if K % 2 == 0:
for c in xrange(N):
ans = min(ans,N-rad_reach(-1,c,0,E,rad))
else:
for c1,c2 in pairs:
ans = min(ans,N-rad_reach(c2,c1,0,E,rad)-rad_reach(c1,c2,0,E,rad))
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s876381549 | p04049 | Time Limit Exceeded | import sys
sys.setrecursionlimit(1000000)
n, k = map(int,input().split())
d = [0] * n
e = [[-1 for i in range(n)] for j in range(n)]
def dfs(i):
for j in e[i]:
if j < 0:
continue
if d[j] < 0:
d[j] = d[i] + 1
dfs(j)
for i in range(n-1):
a, b = map(int,input().split())
a -= 1
b -= 1
e[a].append(b)
e[b].append(a)
ans = n
if k % 2 == 0:
for i in range(n):
for j in range(n):
d[j] = -1
d[i] = 0
dfs(i)
cnt = 0
for j in range(n):
if d[j] > k // 2:
cnt += 1
ans = min(ans, cnt)
else:
for i in range(n):
for j in e[i]:
if j < -1 or i > j:
continue
for l in range(n):
d[l] = -1
d[i] = 0
d[j] = 0
dfs(i)
dfs(j)
cnt = 0
for l in range(n):
if d[l] > (k - 1) // 2:
cnt += 1
ans = min(ans, cnt)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s193167998 | p04049 | Time Limit Exceeded | from collections import deque
import sys
sys.setrecursionlimit(10**6)
def bfs1(c):
visited[c]=1
q=deque([c])
while len(q)>0:
p=q.popleft()
for i in v[p]:
if visited[i]==0:
q.append(i)
visited[i]=1
d[c][i]=d[c][p]+1
def bfs2(c1,c2):
visited[c2]=1
bfs1(c1)
bfs1(c2)
n,k=map(int,raw_input().split())
d=[[0]*n for _ in xrange(n)]
v=[[] for _ in xrange(n)]
e=[]
for i in xrange(n-1):
a,b=map(int,raw_input().split())
a-=1
b-=1
v[a].append(b)
v[b].append(a)
e.append([a,b])
if k%2==0:
min_del=1000000
for i in xrange(n):
cnt=0
visited=[0]*n
bfs1(i)
for j in xrange(n):
if d[i][j]>k/2:
cnt+=1
min_del=min(min_del,cnt)
print(min_del)
else:
min_del=1000000
for i,j in e:
cnt=0
visited=[0]*n
d=[[0]*n for _ in xrange(n)]
bfs2(i,j)
for l in xrange(n):
if d[i][l]>(k-1)/2 or d[j][l]>(k-1)/2:
cnt+=1
min_del=min(min_del,cnt)
print(min_del) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s670043003 | p04049 | Time Limit Exceeded | import sys
sys.setrecursionlimit(10**6)
def dfs1(s,c):
visited[s]=1
for i in v[s]:
if visited[i]==0:
d[c][i]=d[c][s]+1
dfs1(i,c)
return
def dfs2(c1,c2):
visited[c2]=1
dfs1(c1,c1)
dfs1(c2,c2)
return
n,k=map(int,raw_input().split())
d=[[0]*n for _ in xrange(n)]
v=[[] for _ in xrange(n)]
e=[]
for i in xrange(n-1):
a,b=map(int,raw_input().split())
a-=1
b-=1
v[a].append(b)
v[b].append(a)
e.append([a,b])
if k%2==0:
min_del=1000000
for i in xrange(n):
cnt=0
visited=[0]*n
dfs1(i,i)
for j in xrange(n):
if d[i][j]>k/2:
cnt+=1
min_del=min(min_del,cnt)
print(min_del)
else:
min_del=1000000
for i,j in e:
cnt=0
visited=[0]*n
d=[[0]*n for _ in xrange(n)]
dfs2(i,j)
for l in xrange(n):
if d[i][l]>(k-1)/2 or d[j][l]>(k-1)/2:
cnt+=1
min_del=min(min_del,cnt)
print(min_del) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s219266930 | p04049 | Time Limit Exceeded | #!/usr/bin/env pypy3
# 参考:https://agc001.contest.atcoder.jp/data/agc/001/editorial.pdf
# 参考:http://yambe2002.hatenablog.com/entry/2016/07/17/050852
import sys
REC_LIMIT = 10000
UNDEF = -1
def min_vertices_to_remove(n, k, adj_edges):
def count_dfs(current, previous, distance):
result = int(distance > k // 2)
for next_vertex in adj_edges[current]:
if next_vertex == previous:
continue
else:
result += count_dfs(next_vertex, current, distance + 1)
return result
if k % 2 == 0:
ans = min(count_dfs(v, UNDEF, 0) for v in range(n))
else:
ans = min(min(count_dfs(v, w, 0) + count_dfs(w, v, 0)
for w in adj_edges[v]) for v in range(n))
return ans
def main():
sys.setrecursionlimit(REC_LIMIT)
n, k = map(int, input().split())
adj_edges = [set() for _ in range(n)]
for _ in range(n - 1):
a, b = map(lambda x: int(x) - 1, input().split())
adj_edges[a].add(b)
adj_edges[b].add(a)
print(min_vertices_to_remove(n, k, adj_edges))
if __name__ == '__main__':
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s823772579 | p04049 | Time Limit Exceeded | from sys import setrecursionlimit
setrecursionlimit(100000)
def rad_reach(bfr,h,dist,E,rad,ne):
cnt = 1
if dist < rad:
for nxt in E[h]:
if nxt == bfr: continue
cnt += rad_reach(h,nxt,dist+1,E,rad,ne)
return cnt
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [set() for i in xrange(N)]
for i in xrange(N-1):
a,b = map(lambda x:int(x)-1,raw_input().split())
E[a].add(b)
E[b].add(a)
pairs.append((a,b))
ans = N
rad = K/2
if K % 2 == 0:
for c in xrange(N):
ans = min(ans,N-rad_reach(-1,c,0,E,rad,-1))
else:
for c1,c2 in pairs:
E[c1].remove(c2); E[c2].remove(c1)
ans = min(ans,N-rad_reach(-1,c1,0,E,rad,c2)-rad_reach(-1,c2,0,E,rad,c1))
E[c1].add(c2); E[c2].add(c1)
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s194132137 | p04049 | Time Limit Exceeded | #!/usr/bin/env pypy3
# 参考:http://yambe2002.hatenablog.com/entry/2016/07/17/050852
import sys
REC_LIMIT = 10000
UNDEF = -1
def min_vertices_to_remove(n, k, adj_edges):
def count_dfs(current, previous, distance):
result = int(distance > k // 2)
for next_vertex in adj_edges[current]:
if next_vertex == previous:
continue
else:
result += count_dfs(next_vertex, current, distance + 1)
return result
if k % 2 == 0:
ans = min(count_dfs(v, UNDEF, 0) for v in range(n))
else:
ans = min(min(count_dfs(v, w, 0) + count_dfs(w, v, 0)
for w in adj_edges[v]) for v in range(n))
return ans
def main():
sys.setrecursionlimit(REC_LIMIT)
n, k = map(int, input().split())
adj_edges = [set() for _ in range(n)]
for _ in range(n - 1):
a, b = map(lambda x: int(x) - 1, input().split())
adj_edges[a].add(b)
adj_edges[b].add(a)
print(min_vertices_to_remove(n, k, adj_edges))
if __name__ == '__main__':
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s916871810 | p04049 | Time Limit Exceeded | from sys import setrecursionlimit
setrecursionlimit(100000)
def rad_reach(b,h,dist,E,rad,ne):
cnt = 1
if dist < rad:
for nxt in E[h]:
if nxt == b or nxt == ne:
continue
cnt += rad_reach(h,nxt,dist+1,E,rad,ne)
return cnt
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [[] for i in xrange(N)]
for i in xrange(N-1):
a,b = map(lambda x:int(x)-1,raw_input().split())
E[a].append(b)
E[b].append(a)
pairs.append((a,b))
ans = N
rad = K/2
if K % 2 == 0:
for c in xrange(N):
ans = min(ans,N-rad_reach(-1,c,0,E,rad,-1))
else:
for c1,c2 in pairs:
ans = min(ans,N-rad_reach(-1,c1,0,E,rad,c2)-rad_reach(-1,c2,0,E,rad,c1))
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s986115216 | p04049 | Time Limit Exceeded | def calc_dist(b,h,dist,E,D,ne):
D[dist] += 1
for nxt in E[h]:
if nxt == b or nxt == ne:
continue
calc_dist(h,nxt,dist+1,E,D,ne)
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [[] for i in xrange(N)]
for i in xrange(N-1):
a,b = map(lambda x:int(x)-1,raw_input().split())
E[a].append(b)
E[b].append(a)
pairs.append((a,b))
ans = N
rad = K/2
if K % 2 == 0:
for i in xrange(N):
D = [0]*N
calc_dist(-1,i,0,E,D,-1)
for j in xrange(rad):
D[rad-j-1] += D[rad-j]
ans = min(N-D[0],ans)
else:
for a,b in pairs:
D1 = [0]*N
D2 = [0]*N
calc_dist(-1,a,0,E,D1,b)
calc_dist(-1,b,0,E,D2,a)
for j in xrange(rad):
D1[rad-j-1] += D1[rad-j]
D2[rad-j-1] += D2[rad-j]
ans = min(N-D1[0]-D2[0],ans)
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s328238417 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[] for _ in range(N + 1)]
for n in range(1, N):
a, b = map(int, input().split())
tree[a].append(b)
tree[b].append(a)
def dfs(x, fa, d):
if d > K:
return 0
sz = 1
for v in tree[x]:
if v != fa:
sz += dfs(v, x, d + 1)
return sz
ans = N
if K % 2 == 0:
K //= 2
for i in range(N):
# print(i, N - dfs(i, 0, 0))
ans = min(ans, N - dfs(i, 0, 0))
print(ans)
else:
K //= 2
for x in range(N):
for v in tree[x]:
ans = min(ans, N - dfs(x, v, 0) - dfs(v, x, 0))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s514364551 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[] for _ in range(N)]
edges = []
for n in range(1, N):
a, b = map(int, input().split())
tree[a - 1].append(b - 1)
tree[b - 1].append(a - 1)
edges.append((a - 1, b - 1))
def dfs(i, now_v):
dias[i] = now_v
for next_node in tree[i]:
if dias[next_node] == -1:
dfs(next_node, now_v + 1)
ans = int(1e18)
dias_lst = []
for i in range(N):
dias = [-1 for _ in range(N)]
dfs(i, 0)
dias_lst.append(list(dias))
if K % 2 == 0:
for i in range(N):
over_count = sum([1 if x > K / 2 else 0 for x in dias_lst[i]])
ans = min(ans, over_count)
print(ans)
else:
for edge in edges:
x = sum([int(min(x, y) > (K - 1) / 2)
for x, y in zip(dias_lst[edge[0]], dias_lst[edge[1]])])
ans = min(ans, x)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s067873792 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[] for _ in range(N)]
edges = []
for n in range(1, N):
a, b = map(int, input().split())
tree[a - 1].append(b - 1)
tree[b - 1].append(a - 1)
edges.append((a - 1, b - 1))
def dfs(i, now_v):
dias[i] = now_v
for next_node in tree[i]:
if dias[next_node] == -1:
dfs(next_node, now_v + 1)
ans = int(1e18)
if K % 2 == 0:
for i in range(N):
dias = [-1 for _ in range(N)]
dfs(i, 0)
over_count = sum([1 if dias[j] > K / 2 else 0 for j in range(N)])
ans = min(ans, over_count)
print(ans)
else:
for edge in edges:
a, b = edge
dias = [-1 for _ in range(N)]
dfs(a, 0)
temp_dias = list(dias)
dias = [-1 for _ in range(N)]
dfs(b, 0)
# dists = [min(x, y) for x, y in zip(temp_dias, dias)]
# print(i, j, dists)
# x = sum([1 if dists[j] > (K - 1) / 2 else 0 for j in range(N)])
x = sum(int(min(x, y) > (K - 1) / 2) for x, y in zip(temp_dias, dias))
# print(x)
ans = min(ans, x)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s463515946 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[] for _ in range(N)]
edges = []
for n in range(1, N):
a, b = map(int, input().split())
tree[a - 1].append(b - 1)
tree[b - 1].append(a - 1)
edges.append((a - 1, b - 1))
def dfs(i, now_v):
dias[i] = now_v
for next_node in tree[i]:
if dias[next_node] == -1:
dfs(next_node, now_v + 1)
ans = int(1e18)
if K % 2 == 0:
for i in range(N):
dias = [-1 for _ in range(N)]
dfs(i, 0)
# print(dias)
x = sum([1 if dias[j] > K / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
else:
for edge in edges:
i, j = edge
dias = [-1 for _ in range(N)]
dfs(i, 0)
temp_dias = list(dias)
dias = [-1 for _ in range(N)]
dfs(j, 0)
dists = [min(x, y) for x, y in zip(temp_dias, dias)]
# print(i, j, dists)
x = sum([1 if dists[j] > (K - 1) / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s931686518 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[0 for n in range(N)] for _ in range(N)]
edges = []
for n in range(1, N):
a, b = map(int, input().split())
tree[a - 1][b - 1] = 1
tree[b - 1][a - 1] = 1
edges.append((a - 1, b - 1))
dias = [-1 for _ in range(N)]
def dfs(i, now_v):
dias[i] = now_v
for n in range(N):
if dias[n] == -1 and tree[i][n] == 1:
dfs(n, now_v + 1)
ans = int(1e18)
if K % 2 == 0:
for i in range(N):
dias = [-1 for _ in range(N)]
dfs(i, 0)
# print(dias)
x = sum([1 if dias[j] > K / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
else:
for edge in edges:
i, j = edge
if tree[i][j] == 1:
dias = [-1 for _ in range(N)]
dfs(i, 0)
temp_dias = list(dias)
dias = [-1 for _ in range(N)]
dfs(j, 0)
dists = [min(x, y) for x, y in zip(temp_dias, dias)]
# print(i, j, dists)
x = sum([1 if dists[j] > (K - 1) / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s448258594 | p04049 | Time Limit Exceeded | # -*- coding: utf-8 -*-
N, K = map(int, input().split())
tree = [[0 for n in range(N)] for _ in range(N)]
disj = [0 for n in range(N)]
for n in range(1, N):
a, b = map(int, input().split())
tree[a - 1][b - 1] = 1
tree[b - 1][a - 1] = 1
disj[a - 1] += 1
disj[b - 1] += 1
dias = [-1 for _ in range(N)]
def dfs(i, now_v):
dias[i] = now_v
for n in range(N):
if dias[n] == -1 and tree[i][n] == 1:
dfs(n, now_v + 1)
"""
dfs(0, 0)
max_node = dias.index(max(dias))
dias = [-1 for _ in range(N)]
dfs(max_node, 0)
print(dias)
D = max(dias)
"""
ans = int(1e18)
if K % 2 == 0:
for i in range(N):
dias = [-1 for _ in range(N)]
dfs(i, 0)
# print(dias)
x = sum([1 if dias[j] > K / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
else:
for i in range(N):
for j in range(i, N):
if tree[i][j] == 1:
dias = [-1 for _ in range(N)]
dfs(i, 0)
temp_dias = list(dias)
dias = [-1 for _ in range(N)]
dfs(j, 0)
dists = [min(x, y) for x, y in zip(temp_dias, dias)]
# print(i, j, dists)
x = sum([1 if dists[j] > (K - 1) / 2 else 0 for j in range(N)])
# print(x)
ans = min(ans, x)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s436598609 | p04049 | Time Limit Exceeded | def main():
n, k = map(int, raw_input().split())
a = []
g = [[] for _ in xrange(n)]
for _ in xrange(n - 1):
u, v = map(int, raw_input().split())
u -= 1
v -= 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append
po = st.pop
ans = 0
if k % 2:
k = k / 2 + 1
for u, v in a:
c = 0
pu((u, k, v))
pu((v, k, u))
while st:
u, d, p = po()
d -= 1
c += 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
if ans < c:
ans = c
else:
k = k / 2 + 1
for i in xrange(n):
c = 0
pu((i, k, -1))
while c < len(st):
u, d, p = st[c]
d -= 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
c += 1
if ans < c:
ans = c
del st[:]
print n - ans
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s191376932 | p04049 | Time Limit Exceeded | def main():
n, k = map(int, raw_input().split())
a = []
g = [[] for _ in xrange(n)]
for _ in xrange(n - 1):
u, v = map(int, raw_input().split())
u -= 1
v -= 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append
po = st.pop
ans = 0
if k % 2:
k = k / 2 + 1
for u, v in a:
c = 0
pu((u, k, v))
pu((v, k, u))
while st:
u, d, p = po()
d -= 1
c += 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
if ans < c:
ans = c
else:
k = k / 2 + 1
for i in xrange(n):
c = 0
pu((i, k, -1))
while st:
u, d, p = po()
d -= 1
c += 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
if ans < c:
ans = c
print n - ans
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s277558352 | p04049 | Time Limit Exceeded | import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
n = raw_input()
a = [int(i) for i in n.split()]
list1 = []
sum = 0
arr = np.zeros([a[0]-1,3])
print arr
for i in range(int(a[0]-1)):
x = raw_input()
list1.append(x)
print list1 | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s990130705 | p04049 | Accepted | import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
from collections import defaultdict
n,k = map(int, input().split())
ns = defaultdict(set)
for _ in range(n-1):
u,v = map(int, input().split())
u -= 1
v -= 1
ns[u].add(v)
ns[v].add(u)
def bfs(start):
from queue import deque
q = deque([start])
seen = [None] * n
seen[start] = 0
dist = defaultdict(list)
dist[0].append(start)
while q:
u = q.pop()
d = seen[u]
for v in ns[u]:
if seen[v] is None and v not in done:
seen[v] = d + 1
dist[d+1].append(v)
q.appendleft(v)
return seen, dist
ans = 0
done = set()
while True:
start = 0
while start in done:
start += 1
seen, dist1 = bfs(start)
m = max(item for item in seen if item is not None)
u = dist1[m][0]
seen, dist1 = bfs(u)
mm = max(item for item in seen if item is not None)
if mm<=k:
break
ans += 1
v = dist1[mm][0]
seen, dist2 = bfs(v)
v1 = sum(len(dist1[u]) for u in range(k+1, mm+1))
v2 = sum(len(dist2[u]) for u in range(k+1, mm+1))
if v1<v2:
done.add(v)
else:
done.add(u)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s600268083 | p04049 | Accepted | import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
m = map(int, read().split())
G = [[] for _ in range(N + 1)]
for a, b in zip(m, m):
G[a].append(b)
G[b].append(a)
def dfs_order(G, root=1):
parent = [0] * (N + 1)
order = []
stack = [root]
while stack:
x = stack.pop()
order.append(x)
for y in G[x]:
if y == parent[x]:
continue
parent[y] = x
stack.append(y)
return parent, order
parent, order = dfs_order(G)
dp1 = np.zeros((N + 1, N + 1), np.int32)
for v in order[::-1]:
p = parent[v]
dp1[v, 0] += 1
dp1[p, 1:] += dp1[v, :-1]
dp2 = np.zeros((N + 1, N + 1), np.int32)
dp2[1] = dp1[1]
for v in order[1:]:
p = parent[v]
x = dp2[p].copy()
x[1:] -= dp1[v, :-1]
dp2[v, 1:] += x[:-1]
dp2[v] += dp1[v]
dp3 = np.zeros((N + 1, N + 1), np.int32)
for v in order[1:]:
p = parent[v]
dp3[v] = dp1[v]
x = dp2[p].copy()
x[1:] -= dp1[v][:-1]
dp3[v] += x
# 距離 r まで生き残らせるとして、生き残る点の数
dp2.cumsum(axis=1, out=dp2)
dp3.cumsum(axis=1, out=dp3)
if K % 2 == 0:
x = dp2[:, K // 2].max()
else:
x = dp3[:, K // 2].max()
print(N - x) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s222618469 | p04049 | Accepted | import sys
input = sys.stdin.readline
n,k = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
for a,b in ab:
graph[a].append(b)
graph[b].append(a)
ans = n
rad = k//2
for center in range(1,n+1):
stack = [center]
dep = [[-1,-1] for _ in range(n+1)]
dep[center] = [0,0]
while stack:
x = stack.pop()
for i,y in enumerate(graph[x]):
if x == center:
dep[y] = [1,i+1]
stack.append(y)
else:
if dep[y][0] == -1:
stack.append(y)
dep[y][0] = dep[x][0]+1
dep[y][1] = dep[x][1]
anstmp = 0
bonus = [0]*(len(graph[center])+1)
for i in range(1,n+1):
if dep[i][0] <= rad:
anstmp += 1
if dep[i][0] == rad+1:
bonus[dep[i][1]] += 1
if k%2:
anstmp += max(bonus)
ans = min(n-anstmp,ans)
print(max(ans,0)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s159783548 | p04049 | Accepted | #参考コード
#https://atcoder.jp/contests/agc001/submissions/4594416
from collections import defaultdict,deque
N,K=map(int,input().split())
tree=[]
Graph=defaultdict(list)
for _ in range(N-1):
edge=list(map(int,input().split()))
Graph[edge[0]].append(edge[1])
Graph[edge[1]].append(edge[0])
tree.append(edge)
def DFSans(start,diam):
visited=[0]*N
q=deque()
for s in start:
q.append((s,0))
visited[s-1]=1
while q:
search,start_diam=q.pop()
if start_diam>=diam:
continue
for n in Graph[search]:
if visited[n-1]:
continue
q.append((n,start_diam+1))
visited[n-1]=1
#print(q,visited)
return N-sum(visited)
ans=99999999999999999999
if K%2==1:
for i in range(N-1):
ans=min(ans,DFSans(tree[i],(K-1)//2))
else:
for i in range(1,N+1):
ans=min(ans,DFSans([i],K//2))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s578647380 | p04049 | Accepted | import sys
from collections import deque
def bfs(links, s, limit, bits):
not_reachable = (1 << len(links)) - 1
q = deque([(0, s, -1)])
while q:
cost, v, p = q.popleft()
if cost > limit:
break
not_reachable ^= bits[v]
cost += 1
for u in links[v]:
if u != p:
q.append((cost, u, v))
return not_reachable
def solve(n, k, ab):
if k == 1:
return n - 2
links = [set() for _ in range(n)]
for a, b in ab:
a -= 1
b -= 1
links[a].add(b)
links[b].add(a)
limit = k // 2
bits = [1 << i for i in range(n)]
if k % 2 == 0:
ans = min(bin(bfs(links, v, limit, bits)).count('1') for v in range(n))
else:
dists = [bfs(links, v, limit, bits) for v in range(n)]
ans = min(bin(dists[a - 1] & dists[b - 1]).count('1') for a, b in ab)
return ans
n, k = map(int, sys.stdin.buffer.readline().split())
ab = map(int, sys.stdin.buffer.read().split())
ab = list(zip(ab, ab))
print(solve(n, k, ab))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s021652616 | p04049 | Accepted | n,k = map(int, input().split())
link = [[] for _ in range(n)]
ln = []
for i in range(n-1):
a,b = list(map(int,input().split()))
link[a-1].append(b-1)
link[b-1].append(a-1)
ln.append([a-1,b-1])
if k==1:
print(n-2)
exit()
from collections import deque
def get_v(node,dist):
Q = deque()
Q.append([node,0])
visited=[-1]*n
visited[node]=1
while Q:
now,cnt = Q.popleft()
if cnt > dist:
break
for nxt in link[now]:
if visited[nxt]!=-1:
continue
visited[nxt]=cnt+1
Q.append([nxt,cnt+1])
for i in range(n):
if visited[i] == -1:
ret = 0
elif visited[i] <= dist:
ret = 1
else:
ret = 0
visited[i] = ret
return visited
def borsm(l1,l2):
sm=0
for b1,b2 in zip(l1,l2):
sm+=(b1 | b2)
return sm
ans=float("inf")
if k%2==0:
for i in range(n):
ret = n - sum(get_v(i,k//2))
ans=min(ret,ans)
else:
for tmp in ln:
n1,n2=tmp
c1=get_v(n1,k//2)
c2=get_v(n2,k//2)
ret = n- borsm(c1,c2)
ans=min(ret,ans)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s004303402 | p04049 | Accepted | n, k = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
if k % 2 == 0:
ans = 100000
for i in range(n):
s = [i]
d = [-1] * n
d[i] = 0
while s:
p = s.pop()
for node in g[p]:
if d[node] == -1:
s.append(node)
d[node] = d[p] + 1
c = 0
for i in range(n):
if d[i] > k // 2:
c += 1
ans = min(ans, c)
else:
ans = 100000
for i in range(n):
for j in g[i]:
s = [i, j]
d = [-1] * n
d[i] = 0
d[j] = 0
while s:
p = s.pop()
for node in g[p]:
if d[node] == -1:
s.append(node)
d[node] = d[p] + 1
c = 0
for p in range(n):
if d[p] > k // 2:
c += 1
ans = min(ans, c)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s385319060 | p04049 | Accepted | # tree diameter if (currDia <= k) true else shorten() while (dia >= k) O(n) dia readjust
# multiple pairs . centroid of tree --> on largest path find centroid --> maintain a heap ?
from collections import deque
def main():
n, d = map(int, input().split())
adj = [[] for i in range(n)]
edges = []
for i in range(n-1):
a, b = map(int, input().split())
adj[a-1].append(b-1)
adj[b-1].append(a-1)
edges.append((a-1, b-1))
ans = 1000000
if (d % 2 == 0):
for i in range(n):
ans = min(ans, compute(adj, i, d))
else:
for i in range(n-1):
ans = min(ans, computeEdges(adj, edges[i], d-1))
print(ans)
def compute(adj, u, dia):
vis = [False]*len(adj)
dis = [0]*len(adj)
q = deque()
q.append(u)
vis[u] = True
while (len(q) > 0):
elem = q.popleft()
for v in adj[elem]:
if (vis[v] == False):
q.append(v)
vis[v] = True
dis[v] = dis[elem] + 1
count = 0
for a in dis:
if (a > dia/2):
count += 1
return count
def computeEdges(adj, edge, dia):
vis = [False]*len(adj)
dis = [0]*len(adj)
q = deque()
q.append(edge[0])
q.append(edge[1])
vis[edge[0]] = True
vis[edge[1]] = True
while (len(q) > 0):
elem = q.popleft()
for v in adj[elem]:
if (vis[v] == False):
q.append(v)
vis[v] = True
dis[v] = dis[elem] + 1
count = 0
for a in dis:
if (a > dia/2):
count += 1
return count
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s607655075 | p04049 | Accepted |
"""
Writer: SPD_9X2
https://atcoder.jp/contests/agc001/tasks/agc001_c
今度こそ通すぞ!!
制約が小さいので、各頂点に関して最短距離探索するぐらいはできる
直径の中央で全探索する?
→距離K//2以内の頂点の数を数え、残りを取り除けばよい
→その点を通らない長さK以上のパスは存在しえない
Kが偶数なら、1点を中心(深さ0)とし探索
Kが奇数なら、隣接する2点を深さ0とすればよい
計算量はO(N**2)
"""
from collections import deque
N,K = map(int,input().split())
AB = []
lis = [ [] for i in range(N) ]
for i in range(N-1):
A,B = map(int,input().split())
A -= 1
B -= 1
lis[A].append(B)
lis[B].append(A)
AB.append([A,B])
ans = float("inf")
if K % 2 == 0: #偶数の場合
for st in range(N):
q = deque([st])
dis = [float("inf")] * N
dis[st] = 0
nps = 0
while len(q) > 0:
now = q.popleft()
nps += 1
if dis[now] < K//2:
for nex in lis[now]:
if dis[nex] > dis[now] + 1:
dis[nex] = dis[now] + 1
q.append(nex)
ans = min(ans , N - nps)
else:
for i in range(N-1):
a,b = AB[i]
q = deque([a,b])
dis = [float("inf")] * N
dis[a] = 0
dis[b] = 0
nps = 0
while len(q) > 0:
now = q.popleft()
nps += 1
if dis[now] < K//2:
for nex in lis[now]:
if dis[nex] > dis[now] + 1:
dis[nex] = dis[now] + 1
q.append(nex)
ans = min(ans , N - nps)
print (ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s093002090 | p04049 | Accepted | #!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
from collections import deque
n, k = map(int, input().split())
radius = k // 2
edge = [[] for _ in range(n)]
uv = []
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1; b -= 1
edge[a].append(b)
edge[b].append(a)
uv.append((a, b))
def dfs(p, v, d):
ret = 0
for nv in edge[v]:
if nv == p:
continue
ret += dfs(v, nv, d+1)
if d > radius:
return ret + 1
else:
return ret
ans = n
if k % 2 == 0:
for i in range(n):
ret = dfs(-1, i, 0)
ans = min(ans, ret)
else:
for u, v in uv:
ret = dfs(u, v, 0) + dfs(v, u, 0)
ans = min(ans, ret)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s936861287 | p04049 | Accepted | #
# ⋀_⋀
# (・ω・)
# ./ U ∽ U\
# │* 合 *│
# │* 格 *│
# │* 祈 *│
# │* 願 *│
# │* *│
#  ̄
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A):
for a in A: print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
N,K=MI()
edge = [[]for _ in range(N)]
for _ in range(N-1):
a,b = MI_()
edge[a].append(b)
edge[b].append(a)
ans = inf
if K&1: #辺が中心となり、両端点からどのへんにもD/2イカ
for a,l in enumerate(edge):
for b in l:
res = 0
q = [a,b]
visited = set(q)
for step in range(N-1):
if step*2 > (K-1):
res += len(q)
tmp = []
while q:
v = q.pop()
visited.add(v)
for u in edge[v]:
if u in visited:
continue
visited.add(u)
tmp.append(u)
q = tmp
ans = min(res, ans)
else: #点が中心となり、どのへんにもD/2イカ
for center in range(N):
res = 0
q = [center]
visited = set()
for step in range(N-1):
tmp = []
if step*2 > K:
res += len(q)
while q:
v = q.pop()
if v in visited:
continue
visited.add(v)
for u in edge[v]:
if u in visited:
continue
tmp.append(u)
q = tmp
ans = min(res, ans)
print(ans)
if __name__ == '__main__':
main() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s491482207 | p04049 | Accepted | from collections import deque
N, K, *AB = map(int, open(0).read().split())
E = [[] for _ in range(N + 1)]
for a, b in zip(*[iter(AB)] * 2):
E[a].append(b)
E[b].append(a)
def solve(a, b):
D = [-1] * (N + 1)
if a == b:
Q = deque([a])
r = K // 2
D[a] = 0
else:
Q = deque([a, b])
r = (K - 1) // 2
D[a] = D[b] = 0
cnt = len(Q)
while Q:
v = Q.popleft()
d = D[v] + 1
if d > r:
break
for u in E[v]:
if D[u] == -1:
cnt += 1
D[u] = d
Q.append(u)
return N - cnt
if K % 2 == 1:
print(min(solve(a, b) for a, b in zip(*[iter(AB)] * 2)))
else:
print(min(solve(a, a) for a in range(1, N + 1))) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s320802157 | p04049 | Accepted | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
N,K = map(int,readline().split())
m = map(int,read().split())
AB = list(zip(m,m))
graph = [[] for _ in range(N+1)]
for a,b in AB:
graph[a].append(b)
graph[b].append(a)
def F(a,b,K):
INF = 10 ** 9
if a == b:
# 中心が1点
q = deque([a])
r = K // 2
else:
# 中心が辺
q = deque([a,b])
r = (K-1)//2
dist = [INF] * (N+1)
for x in q:
dist[x] = 0
pop = q.popleft; append = q.append
cnt = len(q)
while q:
v = pop()
dw = dist[v] + 1
if dw > r:
break
for w in graph[v]:
if dist[w] == INF:
cnt += 1
dist[w] = dw
append(w)
return N - cnt
if K & 1:
answer = min(F(a,b,K) for a,b in AB)
else:
answer = min(F(a,a,K) for a in range(1,N+1))
print(answer) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s879188984 | p04049 | Accepted | 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=0
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 L[Q[q][0]]:
if C[k]==10000:
Q.append([k,Q[q][1]+1])
if N-cnt<ans:
ans=N-cnt
#print(i,C)
print(max(0,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(max(0,ans)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s575011442 | p04049 | Accepted | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
sys.setrecursionlimit(10**9)
n,k = map(int, input().split())
G = [[] for i in range(n)]
edges = []
for i in range(n-1):
a,b = map(int, input().split())
a,b = a-1,b-1
G[a].append(b)
G[b].append(a)
edges.append((a,b))
cnt = [0]
def search(cur, prev, depth, k):
if depth > k:
cnt[0] += 1
for to in G[cur]:
if to != prev:
search(to, cur, depth+1, k)
ans = float("inf")
# 点中心
if k % 2 == 0:
for i in range(n):
cnt = [0]
search(i, -1, 0, k//2)
ans = min(ans, cnt[0])
# 辺中心
else:
for a, b in edges:
cnt = [0]
search(a, b, 0, k//2)
search(b, a, 0, k//2)
ans = min(ans, cnt[0])
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s988403881 | p04049 | Accepted | from collections import deque
n, k = map(int, input().split())
graph = [[] for _ in range(n+1)]
ab_list = []
for i in range(n-1):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
ab_list.append((a, b))
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)
else:
for now_node, now_neighbor in ab_list:
extra_node_cnt = 0
q = deque()
q.append((now_node, 0))
seen = [0]*(n+1)
seen[now_node] = 1
seen[now_neighbor] = 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((now_neighbor, 0))
seen = [0]*(n+1)
seen[now_node] = 1
seen[now_neighbor] = 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) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s576764163 | p04049 | Accepted | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
N,K = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
graph = [[] for _ in range(N+1)]
for a,b in AB:
graph[a].append(b)
graph[b].append(a)
def count_nbd(x,R,parent):
# parentを含まない部分木
# xを含む、xから半径R内の点の個数を数える
count = 0
visited = [False] * (N+1)
visited[x] = True
visited[parent] = True
count += 1
q = [x]
for _ in range(R):
qq = []
for y in q:
for z in graph[y]:
if visited[z]:
continue
visited[z] = True
qq.append(z)
count += 1
q = qq
return count
if K&1:
# 辺の中点に中心がある場合
R = (K-1)//2
x = max(count_nbd(x,R,y) + count_nbd(y,R,x) for x,y in AB)
else:
# 頂点が中心
R = K//2
x = max(count_nbd(x,R,0) for x in range(1,N+1))
answer = N-x
print(answer) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s718191855 | p04049 | Accepted | import collections
n,k=map(int,input().split())
g=[[] for _ in range(n+1)]
e=[]
for _ in range(n-1):
a,b=map(int,input().split())
g[a].append(b)
g[b].append(a)
e.append((a,b))
ans=0
if k%2==0:
for i in range(1,n+1):
tmp=0
checked=[0]*(n+1)
checked[i]=1
q=collections.deque()
q.append((i,0))
while len(q)!=0:
v,d=q.popleft()
if d<=k//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
ans=max(ans,tmp)
print(n-ans)
if k%2==1:
for v1,v2 in e:
tmp=0
checked=[0]*(n+1)
checked[v1]=1
checked[v2]=1
q=collections.deque()
q.append((v1,0))
while len(q)!=0:
v,d=q.popleft()
if d<=k//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
checked=[0]*(n+1)
checked[v1]=1
checked[v2]=1
q=collections.deque()
q.append((v2,0))
while len(q)!=0:
v,d=q.popleft()
if d<=(k-1)//2:
tmp+=1
else:
break
for u in g[v]:
if checked[u]==0:
checked[u]=1
q.append((u,d+1))
ans=max(ans,tmp)
print(n-ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s782776092 | p04049 | Accepted | 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, x):
vis = [-1 for _ in range(n)]
vis[start] = 0
s = [start]
cnt = 0
while s:
l = s.pop()
cnt += 1
if vis[l] < x:
for c in adj[l]:
if vis[c] < 0:
s.append(c)
vis[c] = vis[l] + 1
return n - cnt
def bfs2(start, nxt, x):
vis = [-1 for _ in range(n)]
vis[start] = 0
vis[nxt] = 0
s = [start, nxt]
cnt = 0
while s:
l = s.pop()
cnt += 1
if vis[l] < x:
for c in adj[l]:
if vis[c] < 0:
s.append(c)
vis[c] = vis[l] + 1
return n - cnt
ans = n-2
if k%2 == 0:
for i in range(n):
ans = min(ans, bfs(i, k//2))
else:
for i in range(n):
for j in adj[i]:
ans = min(ans, bfs2(i, j, k//2))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s951866574 | p04049 | Accepted | # coding:utf-8
import sys
from collections import defaultdict, deque
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
def main():
n, k = LI()
G = defaultdict(list)
AB = [LI_() for _ in range(n - 1)]
for a, b in AB:
G[a].append(b)
G[b].append(a)
def DFS(ss: list, t: int):
q = deque()
for s in ss:
q.append((s, 0))
visited = [0] * n
visited[ss[0]] = visited[ss[-1]] = 1
while q:
v, d = q.pop()
if d >= t:
continue
for to in G[v]:
if visited[to]:
continue
visited[to] = 1
q.append((to, d + 1))
return n - sum(visited)
# 木の性質
# 直径をdとした時
# dが偶数ならば,ある頂点vから他の頂点への距離がD/2となる頂点vが存在する
# dが奇数ならば,ある辺eから他の頂点への距離が(D-1)/2となる辺eが存在する
res = INF
if k % 2:
for a, b in AB:
res = min(res, DFS([a, b], (k - 1) // 2))
else:
for i in range(n):
res = min(res, DFS([i], k // 2))
return res
print(main())
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s377776494 | p04049 | Accepted | 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))
def f(x,y,c):
global tmp
c += 1
for e in edge[x]:
if e == y:
continue
if c > d:
tmp +=1
f(e,x,c)
sys.setrecursionlimit(4100000)
if k % 2 == 0:
d = k//2
ass = 2001
for i in range(n):
tmp = 0
f(i,-1,0)
ass = min(ass,tmp)
print(ass)
else:
d = (k-1)//2
ass = 2001
for e1 in alledge:
tmp = 0
f(e1[0],e1[1],0)
f(e1[1],e1[0],0)
ass = min(ass,tmp)
print(ass)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s269572606 | p04049 | Accepted | 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, uu, n, G):
q = [u, uu]
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 != u:
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)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s589681725 | p04049 | Accepted | from collections import deque
N,K=map(int,input().split())
node=[0]*N
edge={}
G=[[] for i in range(N)]
for i in range(N-1):
a,b=map(int,input().split())
edge[(a-1,b-1)]=0
G[a-1].append(b-1)
G[b-1].append(a-1)
if K%2:
for a,b in edge.keys():
visited=[False]*N
stack=deque([(a,0),(b,0)])
visited[a],visited[b]=True,True
while stack:
n,c=stack.popleft()
if c>K//2:
break
edge[(a,b)]+=1
for s in G[n]:
if not visited[s]:
stack.append((s,c+1))
visited[s]=True
print(N-max(edge.values()))
else:
for a in range(N):
visited=[False]*N
stack=deque([(a,0)])
visited[a]=True
while stack:
n,c=stack.popleft()
if c>K//2:
break
node[a]+=1
for s in G[n]:
if not visited[s]:
stack.append((s,c+1))
visited[s]=True
print(N-max(node))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s002964151 | p04049 | Accepted | 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
#print(a,num,visit)
return num
def dfs2(i,j):
visit=[-1]*N
h=deque()
h.append(i)
h.append(j)
visit[i]=0
visit[j]=0
num=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
#print(a,num,visit)
return num
if s==0:
ans=0
for i in range(N):
ans=max(ans,dfs(i))
print(N-ans)
elif s==1:
ans=0
for i in range(N):
for j in table[i]:
if j>i:
ans=max(ans,dfs2(i,j))
print(N-ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s397252240 | p04049 | Accepted | from collections import deque
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans,c = [[] for i in range(n)],[[] for i in range(n)],n,0
for i,j in b:
x[i].append(j)
x[j].append(i)
def f(s):
for i in range(n):
q,v = deque(),[1]*n
v[i] = 0
for j in x[i]:
q.append((j,1,j))
d[i].append(j)
v[j] = 0
while q:
p = q.pop()
if p[1]<s:
for j in x[p[0]]:
if v[j]:
q.append((j,p[1]+1,p[2]))
d[i].append(p[2])
v[j] = 0
if k>n//2:
for i in x: c = max(len(i),c)
if n-c+1<=k: ans = 0
elif k==1: ans = n-2
else:
f(k//2)
if k%2:
for i,j in b: ans = min(ans,n-len(d[i])-len(d[j])+d[i].count(j)+d[j].count(i)-2)
else:
for i in range(n): ans = min(ans,n-len(d[i])-1)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s200992865 | p04049 | Accepted | n, k = map(int, input().split())
import sys
sys.setrecursionlimit(2000)
l = [[] for i in range(n)]
e = []
for i in range(n-1):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
e.append([a-1, b-1])
def first_search(first, depth):
record = 0
for i in l[first]:
temp = search(first, i, depth-1)
if record < temp:
record = temp
return record + 1
def search(before, now, depth):
if depth <= 0:
return 1
else:
ans = 1
for i in l[now]:
if before != i:
ans += search(now, i, depth-1)
return ans
ret = 0
if k%2 == 0:
for i in range(n):
temp = search(-1, i, k//2)
if temp > ret:
ret = temp
else:
for i in e:
temp = search(i[0], i[1], k//2) + search(i[1], i[0], k//2)
if temp > ret:
ret = temp
print(n-ret) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s754814119 | p04049 | Accepted | import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
from collections import deque
from collections import defaultdict
def bfs(graph:list, start:int) -> list:
# 未探索のノードは距離null
node = len(graph)
dist = [None]*node
# 始点ノードの距離を0とし、bfsのためのqueueを作成
dist[start] = 0
que = deque([(0,start)])
# 未探索のノードをqueueに入れる
# kより大きいの距離のものを数える
while que:
cost, cur_node = que.popleft()
for nex_cost, nex_node in graph[cur_node]:
if dist[nex_node] is not None:
continue
else:
dist[nex_node] = dist[cur_node] + nex_cost
que.append((dist[nex_node], nex_node))
return dist
# 入力, グラフ作成
n,k = li()
adj_list = [[] for _ in range(n)]
edges = []
for _ in range(n-1):
a,b = li_()
adj_list[a].append((1,b))
adj_list[b].append((1,a))
edges.append((a,b))
ans = n
dists = []
for i in range(n):
dists.append(bfs(adj_list, i))
# kが奇数の時
if k%2:
for a,b in edges:
ans = min(ans, sum([min(d1,d2) > (k-1)//2 for d1,d2 in zip(dists[a], dists[b])]))
# kが偶数の時
else:
for st in range(n):
ans = min(ans, sum([d > k//2 for d in dists[st]]))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s308954100 | p04049 | Accepted | import sys
stdin = sys.stdin
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
from collections import deque
from collections import defaultdict
def bfs(graph:list, start:int) -> list:
# 未探索のノードは距離null
node = len(graph)
dist = [None]*node
# 始点ノードの距離を0とし、bfsのためのqueueを作成
dist[start] = 0
que = deque([(0,start)])
# 未探索のノードをqueueに入れる
# kより大きいの距離のものを数える
while que:
cost, cur_node = que.popleft()
for nex_cost, nex_node in graph[cur_node]:
if dist[nex_node] is not None:
continue
else:
dist[nex_node] = dist[cur_node] + nex_cost
que.append((dist[nex_node], nex_node))
return dist
# 入力, グラフ作成
n,k = li()
adj_list = [[] for _ in range(n)]
edges = []
for _ in range(n-1):
a,b = li_()
adj_list[a].append((1,b))
adj_list[b].append((1,a))
edges.append((a,b))
ans = n
# kが奇数の時
if k%2:
for a,b in edges:
dist1 = bfs(adj_list, a)
dist2 = bfs(adj_list, b)
ans = min(ans, sum([min(d1,d2) > (k-1)//2 for d1,d2 in zip(dist1, dist2)]))
# kが偶数の時
else:
for st in range(n):
dist = bfs(adj_list, st)
ans = min(ans, sum([d > k//2 for d in dist]))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s067221399 | p04049 | Accepted | from collections import deque
N,K = map(int,input().split())
es = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
tr = [[] for i in range(N)]
for a,b in es:
tr[a].append(b)
tr[b].append(a)
def reachable_points(root_v, cant_visit_v):
visited = [0] * N
visited[root_v] = visited[cant_visit_v] = 1
q = deque([(root_v,0)])
ret = 0
while q:
v,dep = q.popleft()
if dep == K//2: break
for to in tr[v]:
if visited[to]: continue
ret += 1
q.append((to,dep+1))
visited[to] = 1
return ret
def reachable_edges(root_e):
a,b = es[root_e]
return reachable_points(a,b) + reachable_points(b,a)
if K%2:
ans = N
for center_e in range(N-1):
ans = min(ans, N - 2 - reachable_edges(center_e))
print(ans)
else:
ans = N
for center_v in range(N):
ans = min(ans, N - 1 - reachable_points(center_v, center_v))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s023189898 | p04049 | Accepted | from collections import deque
N,K = map(int,input().split())
es = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
tr = [[] for i in range(N)]
for i,(a,b) in enumerate(es):
tr[a].append((b,i))
tr[b].append((a,i))
def reachable_points(root_v, cant_visit_v):
visited = [0] * N
visited[root_v] = visited[cant_visit_v] = 1
q = deque([(root_v,0)])
ret = 0
while q:
v,dep = q.popleft()
if dep == K//2: break
for to,_ in tr[v]:
if visited[to]: continue
ret += 1
q.append((to,dep+1))
visited[to] = 1
return ret
def reachable_edges(root_e):
a,b = es[root_e]
return reachable_points(a,b) + reachable_points(b,a)
if K%2:
ans = N
for center_e in range(N-1):
ans = min(ans, N - 2 - reachable_edges(center_e))
print(ans)
else:
ans = N
for center_v in range(N):
ans = min(ans, N - 1 - reachable_points(center_v, center_v))
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s508913083 | p04049 | Accepted | N,K=map(int,input().split())
G=[[] for i in range(N)]
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
for i in range(N-1):
a[i],b[i]=map(int,input().split())
a[i]-=1;b[i]-=1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
d=[[-1 for i in range(N)] for j in range(N)]
for i in range(N):
q=[i]
d[i][i]=0
while(len(q)>0):
r=q[-1];q.pop()
for p in G[r]:
if d[i][p]!=-1:
continue
d[i][p]=d[i][r]+1
q.append(p)
#dia=max([max([d[i][j] for i in range(N)]) for j in range(N)])
if K%2==0:
t=[[d[i][j] for i in range(N)] for j in range(N)]
D=K//2
ans=[0 for i in range(N)]
for i in range(N):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans))
else:
t=[[min([d[a[i]][j],d[b[i]][j]]) for j in range(N)] for i in range(N-1)]
ans=[0 for i in range(N-1)]
D=(K-1)//2
for i in range(N-1):
for j in range(N):
if t[i][j]>D:
ans[i]+=1
print(min(ans)) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s846800651 | p04049 | Accepted | from collections import deque
INF = float('inf')
N, K = map(int, input().split())
edges = [list(map(lambda x: int(x) - 1, input().split())) for e in range(N - 1)]
adjL = [[] for v in range(N)]
for a, b in edges:
adjL[a].append(b)
adjL[b].append(a)
# 頂点の集合VCenterから距離radius以内にある頂点の数を求める
def bfs(adjList, VCenter, radius):
numV = len(adjList)
dist = [INF] * numV
for v in VCenter:
dist[v] = 0
ans = 0
Q = deque(VCenter)
while Q:
vNow = Q.popleft()
distNow = dist[vNow]
if distNow > radius: break
ans += 1
for v2 in adjList[vNow]:
if dist[v2] != INF: continue
dist[v2] = distNow + 1
Q.append(v2)
return ans
# 削除後の「直径Kの木」の中心を全探索する
if K % 2:
# Kが奇数の場合、全ての辺を試す
VC = edges
r = (K - 1) // 2
else:
# Kが偶数の場合、全ての頂点を試す
VC = [[v] for v in range(N)]
r = K // 2
ans = N
for V in VC:
num = bfs(adjL, V, r)
ans = min(ans, N - num)
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s176796857 | p04049 | Accepted | from collections import deque
N, K = map(int, input().split())
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append([a, b])
def bfs(n):
visited = [False] * N
dist = [0] * N
queue = deque([n])
while queue:
node = queue.pop()
if visited[node]:
continue
visited[node] = True
for n in T[node]:
if not visited[n]:
dist[n] = dist[node] + 1
queue.appendleft(n)
return dist
dist = []
for i in range(N):
dist.append(bfs(i))
ans = float('inf')
if K % 2 == 0:
for i in range(N):
ans = min(ans, len([d for d in dist[i] if d > K / 2]))
else:
for a, b in E:
ans = min(ans, len([d for d in [min(d1, d2) for d1, d2 in zip(dist[a], dist[b])] if d > (K-1) / 2]))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s203445479 | p04049 | Accepted | from collections import deque
N, K = map(int, input().split())
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def bfs(n):
visited = [False] * N
dist = [0] * N
queue = deque([n])
while queue:
node = queue.pop()
if visited[node]:
continue
visited[node] = True
for n in T[node]:
if not visited[n]:
dist[n] = dist[node] + 1
queue.appendleft(n)
return dist
dist = []
for i in range(N):
dist.append(bfs(i))
ans = float('inf')
if K % 2 == 0:
# 全ての頂点について全探索
for i in range(N):
ans = min(ans, len(list(filter(lambda x: K / 2 < x, dist[i]))))
else:
# 全ての辺について全探索
for a, b in E:
adist = [min(d1, d2) for d1, d2 in zip(dist[a], dist[b])]
ans = min(ans, len(list(filter(lambda x: (K-1) / 2 < x, adist))))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s271128839 | p04049 | Accepted | from collections import deque
N, K = map(int, input().split())
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def dfs(n):
visited = [False] * N
stack = [[n, 0]]
longest = [-1, -1]
while stack:
node, weight = stack.pop()
if visited[node]:
continue
visited[node] = True
if longest[1] < weight:
longest = [node, weight]
for n in T[node]:
if not visited[n]:
stack.append([n, weight + 1])
return longest
def bfs(n):
visited = [False] * N
dist = [0] * N
queue = deque([n])
while queue:
node = queue.pop()
if visited[node]:
continue
visited[node] = True
for n in T[node]:
if not visited[n]:
dist[n] = dist[node] + 1
queue.appendleft(n)
return dist
xn, xw = dfs(0)
yn, yw = dfs(xn)
diameter = yw
d = bfs(0)
ans = float('inf')
if K % 2 == 0:
# 全ての頂点について全探索
for i in range(N):
dist = bfs(i)
ans = min(ans, len(list(filter(lambda x: K / 2 < x, dist))))
else:
# 全ての辺について全探索
for a, b in E:
dist1 = bfs(a)
dist2 = bfs(b)
dist = [min(d1, d2) for d1, d2 in zip(dist1, dist2)]
ans = min(ans, len(list(filter(lambda x: (K-1) / 2 < x, dist))))
print(ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s478477929 | p04049 | Accepted | # seishin.py
from collections import deque
N, K = map(int, input().split())
G = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = map(int, input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
E.append((a-1, b-1))
def bfs0(s):
que = deque()
prev = [-1]*N
prev[s] = s
que.append(s)
while que:
v = que.popleft()
for w in G[v]:
if prev[w] == -1:
prev[w] = v
que.append(w)
L = []
while v != s:
L.append(v)
v = prev[v]
L.append(s)
return L
D = bfs0(0)
D = bfs0(D[0])
C = {e for e in D}
l = len(D)
if l <= K+1:
print(0)
exit(0)
def bfs(que, U, K2):
if K2 == 0:
return 0
cnt = 0
while que:
v = que.popleft()
for w in G[v]:
if U[w] != -1:
continue
U[w] = r = U[v] + 1
cnt += 1
if r < K2:
que.append(w)
return cnt
ans = 0
que = deque()
if K % 2:
for a, b in E:
U = [-1]*N
U[a] = U[b] = 0
que.append(a)
que.append(b)
ans = max(ans, bfs(que, U, K // 2) + 2)
else:
for i in range(N):
U = [-1]*N
U[i] = 0
que.append(i)
ans = max(ans, bfs(que, U, K // 2) + 1)
print(N - ans)
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s557163790 | p04049 | Accepted | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 10**9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
n,k = LI()
e = collections.defaultdict(list)
t = []
for _ in range(n-1):
a,b = LI_()
e[a].append(b)
e[b].append(a)
t.append((a,b))
r = inf
q = [0] * n
k2 = k//2
if k%2 == 0:
for i in range(n):
v = [-1] * n
v[i] = 0
q[0] = i
qi = qe = 0
while qi <= qe:
c = q[qi]
for ni in e[c]:
if v[ni] >= 0:
continue
if v[c] == k2:
continue
v[ni] = v[c] + 1
qe += 1
q[qe] = ni
qi += 1
tr = n-qe-1
if r > tr:
r = tr
else:
for i in range(n-1):
a,b = t[i]
v = [-1] * n
v[a] = v[b] = 0
q[0] = a
q[1] = b
qi = 0
qe = 1
while qi <= qe:
c = q[qi]
for ni in e[c]:
if v[ni] >= 0:
continue
v[ni] = v[c] + 1
if v[ni] > k2:
continue
qe += 1
q[qe] = ni
qi += 1
tr = n-qe-1
if r > tr:
r = tr
return r
print(main())
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s841191279 | p04049 | Accepted | def dfs(now, d):
visited[now] = True
if d == k / 2 : return
for nx in g[now]:
if not visited[nx]:
dfs(nx, d + 1)
n, k = map(int, raw_input().split())
g = [[] for _ in xrange(n)]
for i in xrange(n - 1):
a, b = map(int, raw_input().split())
a -= 1; b -= 1;
g[a].append(b)
g[b].append(a)
ans = n
if k % 2 == 1:
for i in xrange(n):
for j in g[i]:
visited = [False] * n
visited[i] = True
visited[j] = True
dfs(i, 0)
dfs(j, 0)
ans = min(ans, n - sum(visited))
print ans
else:
for i in xrange(n):
visited = [False] * n
visited[i] = True
dfs(i, 0)
ans = min(ans, n - sum(visited))
print ans
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s529579515 | p04049 | Accepted | o=lambda:map(int,raw_input().split());s=9999
n,k=o();r=range(n)
e=[[]for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
def U(x,f):
if f<n:l[f]=0
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=0
for w in e[v]:
if l[w]:q+=[(w,c+1)]
return n-l.count(0)
if k%2:
for i in r:
for j in e[i]:
if i<j:l=[1]*n;U(i,j);s=min(s,U(j,i))
else:
for i in r:l=[1]*n;s=min(s,U(i,n))
print s
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s951208942 | p04049 | Accepted | o=lambda:map(int,raw_input().split());s=10**5
n,k=o();r=range(n)
e=[[]for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
def U(x,f):
if f>=0:l[f]=1
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=1
for w in e[v]:
if not l[w]:q+=[(w,c+1)]
return n-l.count(1)
if k%2:
for i in r:
for j in e[i]:
if i<j:l=[0]*n;U(i,j);s=min(s,U(j,i))
else:
for i in r:l=[0]*n;s=min(s,U(i,-1))
print s
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s429413576 | p04049 | Accepted | o=lambda:map(int,raw_input().split())
T,F=True,False
n,k=o()
e=[[] for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
a=10**5
def dfs(x,f):
if f>=0:l[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=T
for w in e[v]:
if not l[w]:q+=[(w,c+1)]
return n-l.count(T)
if k%2:
for i in range(n):
for j in e[i]:
if i<j:l=[F]*n;dfs(i,j);a=min(a,dfs(j,i))
else:
for i in range(n):l=[F]*n;a=min(a,dfs(i,-1))
print a
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s976551616 | p04049 | Accepted | ii=lambda:map(int,raw_input().split())
n,k=ii()
T,F=True,False
e=[[] for _ in range(n)]
for _ in range(n-1):
a,b=ii()
e[a-1]+=[b-1];e[b-1]+=[a-1]
cost=[0]*n
ans=10**5
def dfs(x,f):
if f>=0:vis[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
cost[v]=c;vis[v]=T
for w in e[v]:
if not vis[w]:q+=[(w,c+1)]
return n-vis.count(T)
if k%2:
for i in range(n):
for j in e[i]:
if i<j:vis=[F]*n;dfs(i,j);ans=min(ans,dfs(j,i))
else:
for i in range(n):vis=[F]*n;ans=min(ans,dfs(i,-1))
print ans
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s232989972 | p04049 | Accepted | from collections import deque
import sys
sys.setrecursionlimit(10**6)
def bfs1(c):
visited[c]=1
q=deque([c])
while len(q)>0:
p=q.popleft()
for i in v[p]:
if visited[i]==0:
q.append(i)
visited[i]=1
d[c][i]=d[c][p]+1
def bfs2(c1,c2):
visited[c2]=1
bfs1(c1)
bfs1(c2)
n,k=map(int,raw_input().split())
d=[[0]*n for _ in xrange(n)]
v=[[] for _ in xrange(n)]
e=[]
for i in xrange(n-1):
a,b=map(int,raw_input().split())
a-=1
b-=1
v[a].append(b)
v[b].append(a)
e.append([a,b])
if k%2==0:
min_del=2005
for i in xrange(n):
cnt=0
visited=[0]*n
bfs1(i)
for j in xrange(n):
if d[i][j]>k/2:
cnt+=1
min_del=min(min_del,cnt)
print(min_del)
else:
min_del=2005
for i,j in e:
cnt=0
visited=[0]*n
bfs2(i,j)
for l in xrange(n):
if d[i][l]>(k-1)/2 or d[j][l]>(k-1)/2:
cnt+=1
min_del=min(min_del,cnt)
d[i]=[0]*n
d[j]=[0]*n
print(min_del) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s769783107 | p04049 | Accepted | from collections import deque
def rad_reach(bfr,h,rest,E):
cnt = 0
Q = deque([(bfr,h,rest)])
while Q:
cnt += 1
bfr,h,rest = Q.popleft()
if rest > 0:
for nxt in E[h]:
if nxt == bfr:
continue
Q.append((h,nxt,rest-1))
return cnt
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [[] for i in xrange(N)]
for i in xrange(N-1):
p = map(lambda x:int(x)-1,raw_input().split())
pairs.append(p)
E[p[0]].append(p[1])
E[p[1]].append(p[0])
ans = N
rad = K/2
if K % 2 == 0:
for c in xrange(N):
ans = min(ans,N-rad_reach(-1,c,rad,E))
else:
for c1,c2 in pairs:
ans = min(ans,N-rad_reach(c2,c1,rad,E)-rad_reach(c1,c2,rad,E))
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s477256091 | p04049 | Accepted | from sys import setrecursionlimit
setrecursionlimit(1000000)
def calc_dist(b,h,dist,E,D,ne):
D[dist] += 1
for nxt in E[h]:
if nxt == b or nxt == ne:
continue
calc_dist(h,nxt,dist+1,E,D,ne)
def solve():
N,K = map(int,raw_input().split())
pairs = []
E = [[] for i in xrange(N)]
for i in xrange(N-1):
a,b = map(lambda x:int(x)-1,raw_input().split())
E[a].append(b)
E[b].append(a)
pairs.append((a,b))
ans = N
rad = K/2
if K % 2 == 0:
for i in xrange(N):
D = [0]*N
calc_dist(-1,i,0,E,D,-1)
for j in xrange(rad):
D[rad-j-1] += D[rad-j]
ans = min(N-D[0],ans)
else:
for a,b in pairs:
D1 = [0]*N
D2 = [0]*N
calc_dist(-1,a,0,E,D1,b)
calc_dist(-1,b,0,E,D2,a)
for j in xrange(rad):
D1[rad-j-1] += D1[rad-j]
D2[rad-j-1] += D2[rad-j]
ans = min(N-D1[0]-D2[0],ans)
print ans
solve() | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s751658323 | p04049 | Accepted | from sys import stdin
def main(le=len):
n, k = map(int, raw_input().split())
a = []
g = [[] for _ in xrange(n)]
dat = map(int, stdin.read().split(), [10] * 2 * (n - 1))
for i in xrange(n - 1):
u, v = dat[i*2] - 1, dat[i*2+1] - 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append
po = st.pop
ans = 0
if k % 2:
k = k / 2 + 1
for u, v in a:
c = 0
pu((u, k, v))
pu((v, k, u))
while c < le(st):
u, d, p = st[c]
d -= 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
c += 1
if ans < c:
ans = c
del st[:]
else:
k = k / 2 + 1
for i in xrange(n):
c = 0
pu((i, k, -1))
while c < le(st):
u, d, p = st[c]
d -= 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
c += 1
if ans < c:
ans = c
del st[:]
print n - ans
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s240968537 | p04049 | Accepted | def main(le=len):
n, k = map(int, raw_input().split())
a = []
g = [[] for _ in xrange(n)]
for _ in xrange(n - 1):
u, v = map(int, raw_input().split())
u -= 1
v -= 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append
po = st.pop
ans = 0
if k % 2:
k = k / 2 + 1
for u, v in a:
c = 0
pu((u, k, v))
pu((v, k, u))
while c < le(st):
u, d, p = st[c]
d -= 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
c += 1
if ans < c:
ans = c
del st[:]
else:
k = k / 2 + 1
for i in xrange(n):
c = 0
pu((i, k, -1))
while c < le(st):
u, d, p = st[c]
d -= 1
if d:
for v in g[u]:
if v != p:
pu((v, d, u))
c += 1
if ans < c:
ans = c
del st[:]
print n - ans
main()
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s315910061 | p04049 | Runtime Error | import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
from collections import defaultdict
n,k = map(int, input().split())
ns = defaultdict(set)
for _ in range(n-1):
u,v = map(int, input().split())
u -= 1
v -= 1
ns[u].add(v)
ns[v].add(u)
def bfs(start):
from queue import deque
q = deque([start])
seen = [None] * n
seen[start] = 0
dist = defaultdict(list)
dist[0].append(start)
while q:
u = q.pop()
d = seen[u]
for v in ns[u]:
if seen[v] is None and v not in done:
seen[v] = d + 1
dist[d+1].append(v)
q.appendleft(v)
return seen, dist
ans = 0
done = set()
while True:
start = 0
while start in done:
start += 1
seen, dist1 = bfs(start)
m = max(item for item in seen if item is not None)
u = dist1[m][0]
seen, dist1 = bfs(u)
mm = max(item for item in seen if item is not None)
if mm<=k:
break
ans += 1
v = dist[mm][0]
seen, dist2 = bfs(v)
if dist1[mm]<dist2[mm]:
done.add(v)
else:
done.add(u)
print(ans) | 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s994436547 | p04049 | Runtime Error | import sys
from collections import deque
def bfs(links, s, limit):
not_reachable = (1 << len(links)) - 1
q = deque([(0, s, -1)])
pop = q.popleft
extend = q.extend
while q:
cost, v, p = pop()
if cost > limit:
break
not_reachable ^= 1 << v
cost += 1
extend((cost, u, v) for u in links[v] if u != p)
return not_reachable
def solve(n, k, ab):
if k == 1:
return n - 2
links = [set() for _ in range(n)]
link_list = []
for a, b in ab:
a -= 1
b -= 1
links[a].add(b)
links[b].add(a)
limit = k // 2
if k % 2 == 0:
ans = min(bin(bfs(links, v, limit)).count('1') for v in range(n))
else:
dists = [bfs(links, v, limit) for v in range(n)]
ans = min(bin(dists[a] & dists[b]).count('1') for a, b in ab)
return ans
n, k = map(int, sys.stdin.buffer.readline().split())
ab = map(int, sys.stdin.buffer.read().split())
ab = list(zip(ab, ab))
print(solve(n, k, ab))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
s748681090 | p04049 | Runtime Error | import sys
from collections import deque
def bfs(links, s, limit):
not_reachable = (1 << len(links)) - 1
q = deque([(0, s, -1)])
pop = q.popleft
extend = q.extend
while q:
cost, v, p = pop()
if cost > limit:
break
not_reachable ^= 1 << v
cost += 1
extend((cost, u, v) for u in links[v] if u != p)
return not_reachable
def solve(n, k, ab):
if k == 1:
return n - 2
links = [set() for _ in range(n)]
link_list = []
for a, b in zip(ab[0::2], ab[1::2]):
a -= 1
b -= 1
links[a].add(b)
links[b].add(a)
link_list.append((a, b))
limit = k // 2
if k % 2 == 0:
ans = min(bin(bfs(links, v, limit)).count('1') for v in range(n))
else:
dists = [bfs(links, v, limit) for v in range(n)]
ans = min(bin(dists[a] & dists[b]).count('1') for a, b in link_list)
return ans
n, k = map(int, sys.stdin.buffer.readline().split())
ab = map(int, sys.stdin.buffer.read().split())
ab = list(zip(ab, ab))
print(solve(n, k, ab))
| 6 2
1 2
3 2
4 2
1 6
5 6
| 2
| <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>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.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.