submission_id
string
problem_id
string
status
string
code
string
input
string
output
string
problem_description
string
s472515885
p04048
Runtime Error
def f(x, y): if x == y: return x x, y = min(x, y), max(x, y) return 2 * x + f(x, y - x) N, X = list(map(int, input().split())) print(f(X, N-X) + N)
5 2
12
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
s034131434
p04048
Runtime Error
def f(a, b): if a == 1 and b == 1: return 1 r = min(a, b) q = max(a, b) return 2*r + f(r, q-r) N, X = map(int, raw_input().split()) print N + f(X, N-X)
5 2
12
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
s046359757
p04048
Runtime Error
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import functools def gcd(a, b): while b: a, b = b, a % b return a @functools.lru_cache(maxsize = None) def func(N, X): g = gcd(N, X) if g == 1: if X > N // 2: return func(N, N - X) if N == 2: return 3 if N == 3: return 6 return X * 3 + func(N-X, X) else: return g * func(N//g, X//g) N,X = map(int,input().split()) print(func(N,X))
5 2
12
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
s124680257
p04048
Runtime Error
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import functools def gcd(a, b): while b: a, b = b, a % b return a @functools.lru_cache(maxsize = None) def func(N, X): g = gcd(N, X) if g == 1: if X > N // 2: return func(N, N - X) if N == 2: return 3 if N == 3: return 6 return X * 3 + func(N-X, X) else: return g * func(N//g, X//g) N,X = map(int,input().split()) print(func(N,X))
5 2
12
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
s412168424
p04048
Runtime Error
n, x = map(int, raw_input().split()) g = gcd(n, x) n /= g x /= g print 3 * (n - 1) * g
5 2
12
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
s501653297
p04049
Wrong Answer
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, mm+1)) v2 = sum(len(dist2[u]) for u in range(k, 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>
s188309836
p04049
Wrong Answer
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) val = mm while val>=0 and len(dist1[val])==len(dist2[val]): val -= 1 if len(dist1[val])<len(dist2[val]): 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>
s335687967
p04049
Wrong Answer
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) if len(dist1[mm])<len(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>
s106835136
p04049
Wrong Answer
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, dist = bfs(start) m = max(item for item in seen if item is not None) u = dist[m][0] seen, dist = bfs(u) mm = max(item for item in seen if item is not None) if mm<=k: break ans += 1 if len(dist[mm])==1: v = dist[mm][0] 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>
s021807684
p04049
Wrong Answer
class tree_unit: def __init__(self, name): self.name = name self.conn = [] def add_conn(self, vectex): self.conn.append(vectex) def search(self, vectex): if vectex in self.conn: conn = self.conn[:].remove(vectex) return conn else: return [] def __repr__(self): return str(self.name) N, K = [int(x) for x in input().split()] units = [tree_unit(0)] for vec in range(1, N+1): units.append(tree_unit(vec)) for i in range(N-1): A, B = [int(x) for x in input().split()] units[A].add_conn(B) units[B].add_conn(A) start = 1 for i in range(1, N+1): if len(units[i].conn) == 1: start = i cur = units[start].conn[0] stack = [cur] visited = [False for i in range(N+1)] depth = [0 for i in range(N+1)] depth[start] = 1 depth[cur] = 2 depth_var = 3 visited[start] = True while stack: cur = stack.pop() visited[cur] = True exist = False for vec in units[cur].conn: if not visited[vec]: stack.append(vec) depth[vec] = depth_var exist = True if exist: depth_var += 1 max_depth = max(depth) if max_depth <= K: print(0) else: dif = max_depth - K count_up, count_down = 0, 0 for v in depth[1:]: if v > max_depth-dif: count_up += 1 elif v <= dif: count_down += 1 print(min(count_up, count_down))
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>
s634224044
p04049
Wrong Answer
class tree_unit: def __init__(self, name): self.name = name self.conn = [] def add_conn(self, vectex): self.conn.append(vectex) def search(self, vectex): if vectex in self.conn: conn = self.conn[:].remove(vectex) return conn else: return [] def __repr__(self): return str(self.name) N, K = [int(x) for x in input().split()] units = [[tree_unit(0), 0, False]] for vec in range(1, N+1): units.append([tree_unit(vec), 0, False]) for i in range(N-1): A, B = [int(x) for x in input().split()] units[A][0].add_conn(B) units[B][0].add_conn(A) ones = [] for i in range(1, N+1): units[i][1] = len(units[i][0].conn) if units[i][1] == 1: ones.append(i) start = ones[0] cur = units[start][0].conn[0] stack = [cur] depth = [0 for i in range(N+1)] depth[start] = 1 depth[cur] = 2 depth_var = 3 units[start][2] = True while stack: cur = stack.pop() units[cur][2] = True exist = False for vec in units[cur][0].conn: if not units[vec][2]: stack.append(vec) depth[vec] = depth_var exist = True if exist: depth_var += 1 max_depth = max(depth) if max_depth <= K: print(0) else: dif = max_depth - K count_up, count_down = 0, 0 for v in depth[1:]: if v > max_depth-dif: count_up += 1 elif v <= dif: count_down += 1 print(min(count_up, count_down))
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>
s904820298
p04049
Wrong Answer
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]*(n+1) dep[center] = 0 while stack: x = stack.pop() for y in graph[x]: if dep[y] == -1: stack.append(y) dep[y] = dep[x]+1 anstmp = 0 for i in range(1,n+1): if dep[i] <= rad: anstmp += 1 if k%2: anstmp += 1 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>
s662629062
p04049
Wrong Answer
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 i in range(n): if d[i] > 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>
s379139258
p04049
Wrong Answer
""" Writer: SPD_9X2 https://atcoder.jp/contests/agc001/tasks/agc001_c 削除後のグラフが連結だから、葉の方から消していく必要がある 直径の一端となる頂点と、そこから伸びている辺を全探索? 距離がK以下の頂点の数の最大値が答え 1辺が高々2回選択されるので O(N**2) """ import sys sys.setrecursionlimit(10 ** 5) N,K = map(int,input().split()) 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) ans = float("inf") def dfs(v,np,dep): ret = 1 if dep == K: return ret for nex in lis[v]: if nex != np: ret += dfs(nex,v,dep+1) return ret for st in range(N): for fp in lis[st]: now = 1 + dfs(st,fp,1) ans = min(ans , N-now) 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>
s667913919
p04049
Wrong Answer
from collections import deque n, k = map(int, input().split()) info = [list(map(int, input().split())) for i in range(n - 1)] tree = [[] for i in range(n)] for a, b in info: a -= 1 b -= 1 tree[a].append(b) tree[b].append(a) def solve(root): visited = [-1] * n visited[root] = 0 res = 0 for v in tree[root]: q = deque([v]) visited[v] = 1 cnt = 2 while q: v = q.pop() if visited[v] >= k: continue for nxt_v in tree[v]: if visited[nxt_v] != -1: continue visited[nxt_v] = visited[v] + 1 cnt+= 1 q.append(nxt_v) res = max(cnt, res) return res ans = 0 for i in range(n): ans = max(solve(i), ans) 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>
s338913473
p04049
Wrong Answer
#!/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)] for _ in range(n - 1): a, b = map(int, input().split()) a -= 1; b -= 1 edge[a].append(b) edge[b].append(a) 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 for i in range(n): ret = dfs(-1, i, 0) ans = min(ans, ret) if k % 2 == 1: ans = max(0, ans-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>
s180465134
p04049
Wrong Answer
from collections import deque n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for _ in range(n-1): a,b = [int(i) for i in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) insides = 0 if k%2==0: #各頂点始まり、で調べる for i in range(n): queue = deque([i]) dist = [-1]*n dist[i] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: continue nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) else: #各枝始まり、で調べる for i,e in enumerate(edges): for v in e: queue = deque([i,v]) dist = [-1]*n dist[i] = 0 dist[v] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: continue nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) print(n-insides)
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>
s153410330
p04049
Wrong Answer
from collections import deque n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for _ in range(n-1): a,b = [int(i) for i in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) insides = 0 if k%2==0: #各頂点始まり、で調べる for i in range(n): queue = deque([i]) dist = [-1]*n dist[i] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: break nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) else: #各枝始まり、で調べる for i,e in enumerate(edges): for v in e: queue = deque([i,v]) dist = [-1]*n dist[i] = 0 dist[v] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: break nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) print(n-insides)
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>
s793243789
p04049
Wrong Answer
# #    ⋀_⋀  #   (・ω・) # ./ 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() for step in range(N-1): if step*2 > (K-1): res += len(q) tmp = [] 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) 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>
s147361873
p04049
Wrong Answer
# #    ⋀_⋀  #   (・ω・) # ./ 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() for step in range(N-1): tmp = [] 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 if step*2 > (K-1): res += len(q) 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>
s039168990
p04049
Wrong Answer
import sys input = sys.stdin.readline from collections import deque from bisect import insort_left N,K = map(int,input().split()) G = [[] for _ in range(N)] for _ in range(N-1): A,B = map(int,input().split()) G[A-1].append(B-1) G[B-1].append(A-1) def MAXDIST(s): dist = [None] * N que = deque([s]) dist[s] = 0 while que: v = que.popleft() d = dist[v] for w in G[v]: if dist[w] is not None: continue dist[w] = d + 1 que.append(w) d = 0 for x in dist: if x is not None: d = max(d, x) return dist.index(d) def cut_tree(s): dist = [None] * N que = deque([s]) dist[s] = 0 while que: v = que.popleft() d = dist[v] if d == K: break for w in G[v]: if dist[w] is not None: continue dist[w] = d + 1 que.append(w) cut_list = tuple(i for i in range(N) if dist[i] is None) return cut_list def main(): ans = 0 tmp = G while True: for i,x in enumerate(G): if x: sta = i u = MAXDIST(sta) cut_list = cut_tree(u) ans += len(cut_list) if not cut_list: break for i in range(N): for E in cut_list: if i == E: G[i] = [] continue try: G[i].remove(E) except ValueError as e: pass if tmp == G: break else: tmp = G 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>
s616478388
p04049
Wrong Answer
import sys input = sys.stdin.readline from collections import deque N,K = map(int,input().split()) G = [[] for _ in range(N)] for _ in range(N-1): A,B = map(int,input().split()) G[A-1].append(B-1) G[B-1].append(A-1) def MAXDIST(s): dist = [None] * N que = deque([s]) dist[s] = 0 while que: v = que.popleft() d = dist[v] for w in G[v]: if dist[w] is not None: continue dist[w] = d + 1 que.append(w) d = max(dist) return dist.index(d), d def cut_cnt(s): dist = [None] * N que = deque([s]) dist[s] = 0 cnt = 1 while que: v = que.popleft() d = dist[v] if d == K: break for w in G[v]: if dist[w] is not None: continue dist[w] = d + 1 que.append(w) cnt += 1 return N - cnt def main(): # u: 頂点0との最長頂点間距離 u, _ = MAXDIST(0) print(cut_cnt(u)) 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>
s448409036
p04049
Wrong Answer
from collections import deque import sys input = sys.stdin.readline N, K = map(int, input().split()) edge = [[] for _ in range(N + 1)] for _ in range(N - 1): a, b = map(int, input().split()) edge[a].append(b) edge[b].append(a) ans = 10 ** 9 for i in range(1, N + 1): if len(edge[i]) > 1: continue cnt = 0 # 削除すべき節点 depth = [-1] * (N + 1) depth[i] = 0 node = deque([i]) while node: s = node.popleft() d = depth[s] if d > K: cnt += 1 for t in edge[s]: if depth[t] != -1: continue depth[t] = d + 1 node.append(t) 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>
s694614494
p04049
Wrong Answer
from collections import deque import sys input = sys.stdin.readline N, K = map(int, input().split()) edge = [[] for _ in range(N + 1)] for _ in range(N - 1): a, b = map(int, input().split()) edge[a].append(b) edge[b].append(a) ans = 10 ** 9 for i in range(1, N + 1): if len(edge[i]) > 1: continue cnt = 0 # 削除すべき節点 depth = [-1] * (N + 1) depth[i] = 0 node = deque([i]) while node: s = node.popleft() d = depth[s] for t in edge[s]: if depth[t] != -1: continue if d >= K: cnt += 1 depth[t] = d + 1 node.append(t) 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>
s843226192
p04049
Wrong Answer
n, k = map(int, input().split()) g = [[] for _ in range(n)] for _ in range(n - 1): a, b = map(int, input().split()) g[a - 1].append(b - 1) g[b - 1].append(a - 1) s = [0] d = [-1] * n d[0] = 0 while s: p = s.pop() for node in g[p]: if d[node] == -1: d[node] = d[p] + 1 s.append(node) m = max(d) ans = n for i in range(n): if d[i] != m: continue s = [i] d1 = [-1] * n d1[i] = 0 while s: p = s.pop() for node in g[p]: if d1[node] == -1: d1[node] = d1[p] + 1 s.append(node) d1.sort(reverse=True) for j in range(n): if d1[j] <= k: break ans = min(ans, j) 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>
s551253934
p04049
Wrong Answer
import sys 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()) 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点 stack = [a] r = K // 2 else: # 中心が辺 stack = [a,b] r = (K-1)//2 dist = [INF] * (N+1) for x in stack: dist[x] = 0 pop = stack.pop; append = stack.append cnt = len(stack) while stack: v = pop() dw = dist[v] + 1 if dw > r: break for w in graph[v]: if dist[w] == INF: cnt += 1 dist[w] = dw stack.append(w) return N - cnt x = min(F(a,b,K) for a,b in AB) y = min(F(a,a,K) for a in range(1,N+1)) answer = min(x,y) 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>
s679832296
p04049
Wrong Answer
import sys 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()) 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点 stack = [a] r = K // 2 else: # 中心が辺 stack = [a,b] r = (K-1)//2 dist = [INF] * (N+1) for x in stack: dist[x] = 0 pop = stack.pop; append = stack.append cnt = len(stack) while stack: v = pop() dw = dist[v] + 1 if dw > r: break for w in graph[v]: if dist[w] == INF: cnt += 1 dist[w] = dw stack.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>
s195321694
p04049
Wrong Answer
N,K=map(int,input().split()) L=[[]for i in range(N+1)] l=[] for i in range(N-1): a,b=map(int,input().split()) L[a].append(b) L[b].append(a) l.append([a,b]) ans=N if K%2==0: for i in range(1,N+1): C=[10000 for i in range(N+1)] Q=[[i,0]] cnt=1 for q in range(10000): if q==len(Q): break if C[Q[q][0]]==10000: C[Q[q][0]]=Q[q][1] cnt+=1 if Q[q][1]<K//2: for k in range(len(L[Q[q][0]])): if C[k]==10000: Q.append([k,Q[q][1]+1]) if N-cnt<ans: ans=N-cnt print(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>
s529364832
p04049
Wrong Answer
N,K=map(int,input().split()) L=[[]for i in range(N+1)] l=[] for i in range(N-1): a,b=map(int,input().split()) L[a].append(b) L[b].append(a) l.append([a,b]) ans=N if K%2==0: for i in range(1,N+1): C=[10000 for i in range(N+1)] Q=[[i,0]] cnt=1 for q in range(10000): if q==len(Q): break if C[Q[q][0]]==10000: C[Q[q][0]]=Q[q][1] cnt+=1 if Q[q][1]<K//2: for k in range(len(L[Q[q][0]])): if C[k]==10000: Q.append([k,Q[q][1]+1]) if N-cnt<ans: ans=N-cnt print(ans) else: if K==1: print(N-2) exit() for a,b in l: C1=[10000 for i in range(N+1)] C2=[10000 for i in range(N+1)] X=[[a,0]] for q in range(10000): if q==len(X): break if C1[X[q][0]]==10000: C1[X[q][0]]=X[q][1] if X[q][1]<(K-1)//2: for k in L[X[q][0]]: if C1[k]==10000: X.append([k,X[q][1]+1]) Y=[[b,0]] for q in range(10000): if q==len(Y): break if C2[Y[q][0]]==10000: C2[Y[q][0]]=Y[q][1] if Y[q][1]<(K-1)//2: for k in L[Y[q][0]]: if C2[k]==10000: Y.append([k,Y[q][1]+1]) cnt=0 for x in range(1,N+1): if C1[x]<=(K-1)//2 or C2[x]<=(K-1)//2: cnt+=1 #print(a,b,cnt,C1,C2) if N-cnt<ans: ans=N-cnt print(ans)
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>
s393365562
p04049
Wrong Answer
N,K=map(int,input().split()) L=[[]for i in range(N+1)] l=[] for i in range(N-1): a,b=map(int,input().split()) L[a].append(b) L[b].append(a) l.append([a,b]) ans=N if K%2==0: for i in range(1,N+1): C=[-1 for i in range(N+1)] Q=[[i,0]] cnt=1 for q in range(10000): if q==len(Q): break if C[Q[q][0]]==-1: C[Q[q][0]]=Q[q][1] cnt+=1 if Q[q][1]<K//2: for k in range(len(L[Q[q][0]])): if C[k]==-1: Q.append([k,Q[q][1]+1]) if N-cnt<ans: ans=N-cnt print(ans) else: for a,b in l: C1=[10000 for i in range(N+1)] C2=[10000 for i in range(N+1)] X=[[a,0]] for q in range(10000): if q==len(X): break if C1[X[q][0]]==10000: C1[X[q][0]]=X[q][1] if X[q][1]<(K-1)//2: for k in L[X[q][0]]: if C1[k]==10000: X.append([k,X[q][1]+1]) Y=[[b,0]] for q in range(10000): if q==len(Y): break if C2[Y[q][0]]==10000: C2[Y[q][0]]=Y[q][1] if Y[q][1]<(K-1)//2: for k in L[Y[q][0]]: if C2[k]==10000: Y.append([k,Y[q][1]+1]) cnt=0 for x in range(1,N+1): if C1[x]<=(K-1)//2 or C2[x]<=(K-1)//2: cnt+=1 #print(a,b,cnt,C1,C2) if N-cnt<ans: ans=N-cnt print(ans)
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>
s477994685
p04049
Wrong Answer
from collections import deque n,k = [int(i) for i in input().split()] edges = [[int(i)-1 for i in input().split()] for j in range(n-1)] node = [[] for i in range(n)] for edge in edges: node[edge[0]].append(edge[1]) node[edge[1]].append(edge[0]) M = 0 for i in range(n): count = 1 que = deque() newque = deque() check = [False for i in range(n)] if k%2==1: check[i] = True que.append(i) elif i!=n-1: check[edges[i][0]]=True check[edges[i][1]]=True que.append(edges[i][0]) que.append(edges[i][1]) count = 2 for j in range((k-1)//2): while len(que)!=0: q = que.pop() for l in node[q]: if check[l]==True: continue newque.append(l) check[l] = True count += 1 que = newque newque = deque() M = max(M,count) print(n-M)
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>
s887378058
p04049
Wrong Answer
from collections import deque n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for _ in range(n-1): a,b = [int(i) for i in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) insides = 0 if k%2==0: #各頂点始まり、で調べる for i in range(n): queue = deque([i]) dist = [-1]*n dist[i] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: break nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) else: #各枝始まり、で調べる for i in range(n): for v in edges[i]: queue = deque([i,v]) dist = [-1]*n dist[i] = 0 dist[v] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: break nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) print(n-insides)
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>
s393503281
p04049
Wrong Answer
from collections import deque n,k = [int(i) for i in input().split()] edges = [[] for _ in range(n)] for _ in range(n-1): a,b = [int(i) for i in input().split()] edges[a-1].append(b-1) edges[b-1].append(a-1) insides = 0 if k%2==0: #各頂点始まり、で調べる for i in range(n): queue = deque([i]) dist = [-1]*n dist[i] = 0 #幅優先探索 while queue: u = queue.popleft() d = dist[u] #幅優先探索ゆえ、k/2になったら終えてしまえばよい。 if d==k//2: break nodes = edges[u] for v in nodes: if dist[v]==-1: #未踏. dist[v] = d + 1 queue.append(v) cnt = 0 for v in dist: if v != -1: cnt += 1 insides = max(cnt, insides) print(n-insides)
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>
s190170676
p04049
Wrong Answer
import sys input=sys.stdin.readline N,K=map(int,input().split()) L=[[]for i in range(N+1)] for i in range(N-1): a,b=map(int,input().split()) L[a].append((b,a)) L[b].append((a,b)) #print(L) def dfs(S): C=[10**5 for i in range(N+1)] C[S]=0 A=L[S] for i in range(10**5): #print(A) #print(C) if len(A)<(i+1): break if C[A[i][0]]!=10**5: continue else: C[A[i][0]]=C[A[i][1]]+1 for j in L[A[i][0]]: if C[j[0]]==10**5: A.append(j) return C array=[[]] for i in range(1,N+1): array.append(dfs(i)) #print(array) #直径 M=0 P=0 for i in range(1,N+1): if array[1][i]>M: P=i M=array[1][i] #print(P) M=max(array[P][1:]) #print(M) if M%2==0: MAX=0 for i in range(1,N+1): cnt=0 for j in range(1,N+1): if array[i][j]<=(K//2): cnt+=1 if MAX<cnt: MAX=cnt if K==1: print(N-2) else: print(N-MAX) else: MAX=0 for i in range(1,N+1): for j in L[i]: if j[0]<j[1]: cnt=0 for k in range(1,N+1): if min(array[j[0]][k],array[j[1]][k])<=(K//2): cnt+=1 if MAX<cnt: MAX=cnt if K==1: print(N-2) else: print(N-MAX)
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>
s915240473
p04049
Wrong Answer
n, k = [ int(v) for v in input().split() ] node_list = [ [ int(v)-1 for v in input().split() ] for i in range(n-1) ] node_type = [ 0 for i in range(n) ] connect_list = [ [] for i in range(n) ] for i in node_list: a, b = i connect_list[a].append(b) connect_list[b].append(a) node_type[a] += 1 node_type[b] += 1 leaf_set = [ i for i in range(n) if node_type[i] == 1 ] def bfs(p): search_list = [p] color_list = [ "W" for i in range(n) ] color_list[p] = "B" for _ in range(k): new_search_list = [] for i in search_list: for j in connect_list[i]: if color_list[j] == "W": color_list[j] = "B" new_search_list.append(j) search_list = new_search_list return n - color_list.count("B") ans_list = [ bfs(i) for i in leaf_set ] print(min(ans_list))
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>
s837151529
p04049
Wrong Answer
from collections import deque n, k = map(int, input().split()) graph = [[] for _ in range(n+1)] for i in range(n-1): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a) ans = 2000 max_distance = k // 2 if k % 2 == 0: for i in range(1, n+1): extra_node_cnt = 0 q = deque() q.append((i, 0)) seen = [0]*(n+1) seen[i] = 1 while q: node, distance = q.popleft() if distance > max_distance: extra_node_cnt += 1 for neighor_node in graph[node]: if seen[neighor_node] == 0: seen[neighor_node] = 1 q.append((neighor_node, distance + 1)) ans = min(ans, extra_node_cnt) print(ans) else: for now_node in range(1, n+1): for neighor_now in graph[now_node]: extra_node_cnt = 0 q = deque() q.append((now_node, 0)) seen = [0]*(n+1) seen[now_node] = 1 seen[neighor_now] = 1 while q: node, distance = q.popleft() if distance > max_distance: extra_node_cnt += 1 for neighor_node in graph[node]: if seen[neighor_node] == 0: seen[neighor_node] = 1 q.append((neighor_node, distance + 1)) q = deque() q.append((neighor_now, 0)) seen = [0]*(n+1) seen[now_node] = 1 seen[neighor_now] = 1 while q: node, distance = q.popleft() if distance > max_distance+1: extra_node_cnt += 1 for neighor_node in graph[node]: if seen[neighor_node] == 0: seen[neighor_node] = 1 q.append((neighor_node, distance + 1)) if extra_node_cnt != 0: ans = min(ans, extra_node_cnt) if ans == 2000: ans = 0 print(ans)
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>
s990822550
p04049
Wrong Answer
from collections import deque n, k = map(int, input().split()) graph = [[] for _ in range(n+1)] for i in range(n-1): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a) ans = float("inf") max_distance = k // 2 if k % 2 == 0 else (k - 1) // 2 for i in range(1, n+1): extra_node_cnt = 0 q = deque() q.append((i, 0)) seen = [0]*(n+1) seen[i] = 1 while q: node, distance = q.popleft() if distance > max_distance: extra_node_cnt += 1 for neighor_node in graph[node]: if seen[neighor_node] == 0: seen[neighor_node] = 1 q.append((neighor_node, distance + 1)) ans = min(ans, extra_node_cnt) print(ans)
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>
s549402182
p04049
Wrong Answer
from collections import defaultdict def computeDist(tree, nk, i): visited = [0] * (nk[0] + 1) queue = [] visited[i] = 1 queue.append(i) dist = [0] * (nk[0] + 1) dist[i] = 0 while queue: s = queue.pop(0) for u in tree[s]: if not visited[u]: queue.append(u) visited[u] = 1 dist[u] = dist[s] + 1 return len([i for i in dist if i > nk[1] // 2]) def minimumNodesToBeRemoved(tree, nk): noOfNodesAtDistKBy2 = [0] * (nk[0] + 1) for i in range(1, nk[0] + 1): noOfNodesAtDistKBy2[i] = computeDist(tree, nk, i) return min(noOfNodesAtDistKBy2[1:]) def start(): nk = [int(i) for i in input().split()] tree = defaultdict(list) for i in range(nk[0] - 1): uv = [int(i) for i in input().split()] tree[uv[0]].append(uv[1]) tree[uv[1]].append(uv[0]) return minimumNodesToBeRemoved(tree, nk) print(start())
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>
s289434738
p04049
Wrong Answer
import collections def dfs(v,d): if len(g[v])==0: return 0 for u in g[v]: if checked[u]==0: checked[u]=1 dfs(u,d+1) print(v,d) n,k=map(int,input().split()) g=[[] for _ in range(n+1)] for _ in range(n-1): a,b=map(int,input().split()) g[a].append(b) g[b].append(a) ans=0 for i in range(1,n+1): q=collections.deque() q.append((i,0)) tmp=1 checked=[0]*(n+1) checked[i]=1 while len(q)!=0: v,d=q.popleft() if d>k: break l=len(g[v]) for u in g[v]: if checked[u]==0: checked[u]=1 if l!=1: q.append((u,d+2)) if d+2<=k: tmp+=1 else: q.append((u,d+1)) if d+1<=k: tmp+=1 ans=max(ans,tmp) print(n-ans)
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>
s519749834
p04049
Wrong Answer
import sys n, k = map(int, input().split()) adj = [[] for _ in range(n)] for _ in range(n-1): a, b = map(int, input().split()) adj[a-1].append(b-1) adj[b-1].append(a-1) def bfs(start, nxt): vis = [-1 for _ in range(n)] vis[start] = 0 vis[nxt] = 1 s = [nxt] cnt = 1 while s: l = s.pop() cnt += 1 if vis[l] < k: for c in adj[l]: if vis[c] < 0: s.append(c) vis[c] = vis[l] + 1 return n - cnt ans = n-2 for i in range(n): for j in adj[i]: ans = min(ans, bfs(i, j)) print(ans)
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>
s930363966
p04049
Wrong Answer
import sys def input(): return sys.stdin.readline()[:-1] n, k = map(int, input().split()) adj = [[] for _ in range(n)] for _ in range(n-1): a, b = map(int, input().split()) adj[a-1].append(b-1) adj[b-1].append(a-1) def bfs(start, nxt): vis = [-1 for _ in range(n)] vis[start] = 0 vis[nxt] = 1 s = [nxt] cnt = 1 while s: l = s.pop() cnt += 1 if vis[l] < k: for c in adj[l]: if vis[c] < 0: s.append(c) vis[c] = vis[l] + 1 return n - cnt ans = n-2 for i in range(n): for j in adj[i]: ans = min(ans, bfs(i, j)) print(ans)
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>
s421830919
p04049
Wrong Answer
n,k = map(int,input().split()) import numpy as np edges = [[] for i in range(n)] for i in range(n-1): a,b = map(int,input().split()) a = a-1 b = b-1 edges[a].append(b) edges[b].append(a) def dfs(v): visited = [] INF = float("inf") d=[INF for i in range(n)] q=[] d[v]=0 q.append(v) while len(q): v=q.pop() visited.append(v) for i in edges[v]: if i not in visited: d[i] = d[v] + 1 q.append(i) else: continue return d INF = float("inf") la = np.array(dfs(0)) a = la.argmax() temp = np.array(dfs(a)) b = temp.argmax() distfrom_a = dfs(a) distfrom_b = dfs(b) d = distfrom_a[b] if k%2 == 0: mi = INF for i in range(n): center = i disi = dfs(i) c = 0 for m in range(n): if disi[m] > k/2: c += 1 if mi>c: mi = c print(mi) else: mi = INF for i in range(n): center = i disi = dfs(i) c = 0 for m in range(n): if disi[m] > (k-1/2): c += 1 if mi>c: mi = c print(mi)
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>
s639190332
p04049
Wrong Answer
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: tmp[0] +=1 f(e,x,c) sys.setrecursionlimit(4100000) if k % 2 == 0: d = k//2 ass = 10**9 tmp = [0] for i in range(n): tmp[0] = 0 f(i,-1,0) ass = min(ass,tmp[0]) print(ass) else: d = (k-1)//2 ass = 10**9 tmp = [0] for e1 in alledge: tmp[0] = 0 f(e1[0],e1[1],0) f(e1[1],e1[0],0) ass = min(ass,tmp[0]) print(1)
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>
s868402522
p04049
Wrong Answer
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: if c > d: tmp[0] +=1 f(e,x,c) sys.setrecursionlimit(4100000) if k % 2 == 0: d = k//2 ass = 10**9 tmp = [0] for i in range(n): tmp[0] = 0 f(i,-1,0) ass = min(ass,tmp[0]) print(ass) else: d = (k-1)//2 ass = 10**9 tmp = [0] for e1 in alledge: tmp[0] = 0 f(e1[0],e1[1],0) f(e1[1],e1[0],0) ass = min(ass,tmp[0]) print(-1)
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>
s479691259
p04049
Wrong Answer
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e != y: if c > d: used[e] = 1 #f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
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>
s185345023
p04049
Wrong Answer
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append([a-1,b-1]) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n #f(e1[0],e1[1],0) #f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
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>
s054333374
p04049
Wrong Answer
import sys input = sys.stdin.readline n,k = map(int,input().split()) edge = [[] for i in range(n)] alledge = [] for i in range(n-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) alledge.append((a-1,b-1)) if n==2: print(0) exit() def f(x,y,c): c += 1 for e in edge[x]: if e == y: continue if c > d: used[e] = 1 f(e,x,c) print(0) exit() if k % 2 == 0: d = k//2 res = [] for i in range(n): used = [0]*n f(i,-1,0) res.append(sum(used)) print(min(res)) else: d = (k-1)//2 res = [] for e1 in alledge: used = [0]*n f(e1[0],e1[1],0) f(e1[1],e1[0],0) res.append(sum(used)) print(min(res))
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>
s010352281
p04049
Wrong Answer
import copy N, K = (int(i) for i in input().split()) G = [[] for i in range(N)] E = [] for i in range(N-1): A, B = (int(i) for i in input().split()) E.append((A-1, B-1)) G[A-1].append(B-1) G[B-1].append(A-1) def DFS(u, n, G): q = [u] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] = 1 if d[u1] < n: for u2 in G[u1]: if not v[u2]: d[u2] = d[u1] + 1 q.append(u2) return sum(v) def DFS_E(u, v, n, G): q = [u, v] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] = 1 if d[u1] < n: for u2 in G[u1]: if not v[u2] and u2 != v: d[u2] = d[u1] + 1 q.append(u2) return sum(v) if K % 2 == 0: ans = 0 for v in range(N): ans = max(ans, DFS(v, K//2, G)) print(N-ans) else: ans = 0 for u, v in E: ans = max(ans, DFS_E(u,v,(K-1)//2,G)) print(N-ans)
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>
s784697261
p04049
Wrong Answer
import copy N, K = (int(i) for i in input().split()) G = [[] for i in range(N)] E = [] for i in range(N-1): A, B = (int(i) for i in input().split()) E.append((A-1, B-1)) G[A-1].append(B-1) G[B-1].append(A-1) def DFS(u, n, G): q = [u] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] += 1 if d[u1] < n: for u2 in G[u1]: if not v[u2]: d[u2] = d[u1] + 1 q.append(u2) return sum(v) def DFS_E(u, v, n, G): q = [u, v] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] += 1 if d[u1] < n: for u2 in G[u1]: if not v[u2] and u2 != v: d[u2] = d[u1] + 1 q.append(u2) return sum(v) if K % 2 == 0: ans = 0 for v in range(N): ans = max(ans, DFS(v, K//2, G)) print(N-ans) else: ans = 0 for u, v in E: ans = max(ans, DFS_E(u,v,K//2,G)) print(N-ans)
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>
s490476127
p04049
Wrong Answer
import copy N, K = (int(i) for i in input().split()) G = [[] for i in range(N)] E = [] for i in range(N-1): A, B = (int(i) for i in input().split()) E.append((A-1, B-1)) G[A-1].append(B-1) G[B-1].append(A-1) def DFS(u, n, G): q = [u] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] += 1 if d[u1] < n: for u2 in G[u1]: if not v[u2]: d[u2] = d[u1] + 1 q.append(u2) return sum(v) def DFS_E(u, v, n, G): return 0 if K % 2 == 0: ans = 0 for v in range(N): ans = max(ans, DFS(v, K//2, G)) print(N-ans) else: ans = 0 for u, v in E: ans = max(ans, DFS_E(u,v,K//2,G)) print(N-ans)
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>
s556514942
p04049
Wrong Answer
import copy N, K = (int(i) for i in input().split()) G = [[] for i in range(N)] E = [] for i in range(N-1): A, B = (int(i) for i in input().split()) E.append((A-1, B-1)) G[A-1].append(B-1) G[B-1].append(A-1) def DFS(u, n, G): q = [u] v = [0]*N d = [0]*N while q: u1 = q.pop() v[u1] += 1 if d[u1] < n: for u2 in G[u1]: if not v[u2]: d[u2] = d[u1] + 1 q.append(u2) return sum(v) def DFS_E(u, v, n, G): G_copy = copy.deepcopy(G) G_copy[u].append(N) G_copy[v].append(N) G_copy.append([u, v]) q = [N] v = [0]*(N+1) d = [0]*(N+1) while q: u1 = q.pop() v[u1] += 1 if d[u1] < n: for u2 in G_copy[u1]: if not v[u2]: d[u2] = d[u1] + 1 q.append(u2) return sum(v) if K % 2 == 0: ans = 0 for v in range(N): ans = max(ans, DFS(v, K//2, G)) print(N-ans) else: ans = 0 for u, v in E: ans = max(ans, DFS_E(u,v,K//2,G)) print(N-ans)
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>
s564126239
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[-1]*N h=deque() h.append(i) visit[i]=0 num=0 a=0 while h: x=h.popleft() for y in table[x]: if visit[y]==-1: visit[y]=visit[x]+1 h.append(y) for k in range(N): if 0<=visit[k]<=t: num+=1 if visit[k]==t and s==1: b=0 for z in table[k]: if visit[z]==t+1: b+=1 a=max(a,b) #print(a,num,visit) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s134954120
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[-1]*N h=deque() h.append(i) visit[i]=0 num=0 a=0 while h: x=h.popleft() for y in table[x]: if visit[y]==-1: visit[y]=visit[x]+1 h.append(y) for k in range(N): if 0<=visit[k]<=t: num+=1 if visit[k]==t and s==1: b=0 for z in table[k]: if visit[z]==k+1: b+=1 a=max(a,b) #print(a,num,visit) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s258340653
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[-1]*N h=deque() h.append(i) visit[i]=0 num=0 a=0 while h: x=h.popleft() for y in table[x]: if visit[y]==-1: visit[y]=visit[x]+1 h.append(y) for k in range(N): if 0<=visit[k]<=t: num+=1 if visit[k]==t and s==1: a=max(a,len(table[k])-1) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s256824678
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[-1]*N h=deque() h.append(i) visit[i]=0 num=0 a=0 while h: x=h.popleft() #if visit[x]>t: #continue for y in table[x]: if visit[y]==-1: visit[y]=visit[x]+1 h.append(y) for k in range(N): if 0<=visit[k]<=t: num+=1 if visit[k]==t and s==1: for z in table[k]: a=max(a,len(table[k])-1) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s355481257
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[-1]*N h=deque() h.append(i) visit[i]=0 num=0 a=0 while h: x=h.popleft() if visit[x]>t: continue for y in table[x]: if visit[y]==-1: visit[y]=visit[x]+1 h.append(y) for k in range(N): if 0<=visit[k]<=t: num+=1 if visit[k]==t and s==1: for z in table[k]: a=max(a,len(table[k])-1) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s823410799
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[False]*N h=deque() h.append((i,0)) visit[i]=True num=0 a=0 while h: x,q=h.popleft() if q>t: continue num+=1 if s==1 and q==t: b=0 for z in table[x]: if not visit[z]: b+=1 a=max(a,b) for y in table[x]: if not visit[y]: visit[y]=True h.append((y,q+1)) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s465179850
p04049
Wrong Answer
from collections import deque import math N,K=map(int,input().split()) table=[[] for i in range(N)] for i in range(N-1): a,b=map(int,input().split()) a,b=a-1,b-1 table[a].append(b) table[b].append(a) t=K//2 s=K%2 def dfs(i): visit=[False]*N h=deque() h.append((i,0)) visit[i]=True num=0 a=0 while h: x,q=h.popleft() if q>t: break num+=1 for y in table[x]: if not visit[y]: visit[y]=True h.append((y,q+1)) if s==1 and q==t: b=0 for z in table[x]: if not visit[z]: b+=1 a=max(a,b) return a+num ans=0 for i in range(N): ans=max(ans,dfs(i)) print(N-ans)
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>
s994126800
p04049
Wrong Answer
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%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>
s824673259
p04049
Wrong Answer
import collections N,K=map(int,input().strip().split()) AB=[] for _ in range(N-1): t=list(map(int,input().strip().split())) AB.append(t) AB.sort() ten=[] kaku=AB[0][0] for i in range(N-1): for x in range(N-1): if i==x: continue if AB[i][0]==AB[x][0]: ten.append(AB[x][1]) tenz=[] tenzz={} for n in range(N-1): for y in ten: if y==AB[n][0] or AB[n][0]==kaku: continue if AB[n][1]==y: tenz.append(AB[n][1]) #tenz.append(AB[n][0]) tenzz[AB[n][0]]=AB[n][1] #print(AB,ten,tenz,tenzz) if len(tenz)+1<=K: print(0) else: l=collections.Counter(tenz) print(min(l.values())+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>
s369333350
p04049
Wrong Answer
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 = max(search(i[0], i[1], k//2) + search(i[1], i[0], k//2 + 1) , search(i[0], i[1], k//2 + 1) + 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>
s513940538
p04049
Wrong Answer
n, k = map(int, input().split()) import sys sys.setrecursionlimit(20000) l = [[] for i in range(n)] for i in range(n-1): a, b = map(int, input().split()) l[a-1].append(b-1) l[b-1].append(a-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 for i in range(n): temp = first_search(i, k) if temp > ret: ret = temp if ret > n: print(0) else: 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>
s332847110
p04049
Wrong Answer
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[i]])) 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>
s975693189
p04049
Wrong Answer
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]: print(st, to) 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>
s630090640
p04049
Wrong Answer
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 bisect import bisect_right 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)] cnt = [0]*n for _ in range(n-1): a,b = li_() cnt[a] += 1 cnt[b] += 1 adj_list[a].append((1,b)) adj_list[b].append((1,a)) # leafの数を数える leaves = [] for i in range(n): if cnt[i] == 1: leaves.append(i) # bfs ans = n for st in leaves: dist, cnt = bfs(adj_list, n, st, k) dist.sort() 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>
s002511452
p04049
Wrong Answer
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 bisect import bisect_right def bfs(graph:list, node:int, start:int) -> list: # 未探索のノードは距離INF INF = float("inf") dist = [INF]*node # 始点ノードの距離を0とし、dfsのためのstackを作成 dist[start] = 0 que = deque([(0,start)]) # 未探索のノードをqueueに入れる while que: cost, cur_node = que.popleft() 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 # 入力, グラフ作成 n,k = li() adj_list = [[] for _ in range(n)] cnt = [0]*n for _ in range(n-1): a,b = li_() cnt[a] += 1 cnt[b] += 1 adj_list[a].append((1,b)) adj_list[b].append((1,a)) # leafの数を数える leaves = [] for i in range(n): if cnt[i] == 1: leaves.append(i) # bfs ans = n for st in leaves: dist = bfs(adj_list, n, st) dist.sort() ans = min(ans, n - bisect_right(dist,k)) 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>
s859370317
p04049
Wrong Answer
import numpy as np N, K = map(int, input().split()) E = np.zeros((N, N), dtype=int) # Elist = [] for e in range(N-1): i, o = map(int, input().split()) # Elist.append((i-1, o-1)) E[i-1][o-1] = 1 E[o-1][i-1] = 1 # print("N, K=", N, K) # for e in Elist: # print(e[0], e[1]) I = np.eye(N, dtype=int) # print("I=", I) exit(0) ie = I for k in range(K): ie = ie + np.matmul(ie, E) # print("I + IE =", ie) # withinK = np.count_nonzero(ie, axis=0) withinK = np.zeros(N, dtype=int) for i in range(N): for j in range(N): if ie[i][j] > 0: withinK[i] += 1 # print("withinK=", withinK) maxBall = np.amax(withinK) # print("maxBall=", maxBall) solution = N - maxBall print(solution)
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>
s679372832
p04049
Wrong Answer
import numpy as np N, K = map(int, input().split()) E = np.zeros((N, N), dtype=int) # Elist = [] for e in range(N-1): i, o = map(int, input().split()) # Elist.append((i-1, o-1)) E[i-1][o-1] = 1 E[o-1][i-1] = 1 exit(0) # print("N, K=", N, K) # for e in Elist: # print(e[0], e[1]) I = np.eye(N, dtype=int) # print("I=", I) ie = I for k in range(K): ie = ie + np.matmul(I, E) # print("I + IE =", ie) # withinK = np.count_nonzero(ie, axis=0) withinK = np.zeros(N, dtype=int) for i in range(N): for j in range(N): if ie[i][j] > 0: withinK[i] += 1 # print("withinK=", withinK) maxBall = np.amax(withinK) # print("maxBall=", maxBall) solution = N - maxBall print(solution)
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>
s289833241
p04049
Wrong Answer
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 diameter % 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>
s417381187
p04049
Wrong Answer
N, K = map(int, input().split()) E = [set() for i in range(N+1)] for i in range(N-1): a, b = map(int, input().split()) E[a].add(b) E[b].add(a) ma = 0 for i in range(1, N+1): L = [i] Closed = {i} for j in range(K-1): 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>
s206569825
p04049
Wrong Answer
import sys sys.setrecursionlimit(5000) def get_diamiter(v, parent, dep, edge): max_dep, cnt, cnt_prev, ret_v = dep, 1, 0, v for e in edge[v]: if e != parent: cand_dep, cand_cnt, cand_prev, cand_v = get_diamiter(e, v, dep + 1, edge) if cand_dep > max_dep: max_dep, cnt, cnt_prev, ret_v = cand_dep, cand_cnt, cand_prev, cand_v elif cand_dep == max_dep: cnt += cand_cnt cnt_prev += cand_prev return max_dep, cnt, (1 if dep + 1 == max_dep else cnt_prev), ret_v def remove(v, edge): for row in edge: if v in row: row.remove(v) def main(N, K, edge): v = 0 ans = 0 while True: _, _, _, v = get_diamiter(v, -1, 0, edge) d, c1, p1, v1 = get_diamiter(v, -1, 0, edge) if d <= K: break _, c2, p2, v2 = get_diamiter(v1, -1, 0, edge) if c1 < c2 or (c1 == c2 and p1 < p2): remove(v1, edge) v = v2 else: remove(v2, edge) v = v1 ans += 1 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>
s228672511
p04049
Wrong Answer
import sys sys.setrecursionlimit(5000) def get_diamiter(v, parent, dep, edge): max_dep, cnt, cnt_prev, ret_v = dep, 1, 0, v for e in edge[v]: if e != parent: cand_dep, cand_cnt, cand_prev, cand_v = get_diamiter(e, v, dep + 1, edge) if cand_dep > max_dep: max_dep, cnt, cnt_prev, ret_v = cand_dep, cand_cnt, cand_prev, cand_v elif cand_dep == max_dep: cnt += cand_cnt cnt_prev += cand_prev return max_dep, cnt, (1 if dep + 1 == max_dep else cnt_prev), ret_v def remove(v, edge): for row in edge: if v in row: row.remove(v) def main(N, K, edge): ans = 0 _, _, _, v = get_diamiter(0, -1, 0, edge) while True: d, c1, p1, v1 = get_diamiter(v, -1, 0, edge) if d <= K: break _, c2, p2, v2 = get_diamiter(v1, -1, 0, edge) if c1 < c2 or (c1 == c2 and p1 < p2): remove(v1, edge) v = v2 else: remove(v2, edge) v = v1 ans += 1 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>
s739806406
p04049
Wrong Answer
import sys sys.setrecursionlimit(5000) def get_diamiter(v, parent, dep, edge): max_dep, cnt, cnt_prev, ret_v = dep, 1, 0, v for e in edge[v]: if e != parent: cand_dep, cand_cnt, cand_prev, cand_v = get_diamiter(e, v, dep + 1, edge) if cand_dep > max_dep: max_dep, cnt, cnt_prev, ret_v = cand_dep, cand_cnt, cnt_prev, cand_v elif cand_dep == max_dep: cnt += cand_cnt cnt_prev += cand_prev return max_dep, cnt, (1 if dep + 1 == max_dep else cnt_prev), ret_v def remove(v, edge): for row in edge: if v in row: row.remove(v) def main(N, K, edge): ans = 0 _, _, _, v = get_diamiter(0, -1, 0, edge) while True: d, c1, p1, v1 = get_diamiter(v, -1, 0, edge) if d <= K: break _, c2, p2, v2 = get_diamiter(v1, -1, 0, edge) if c1 < c2 or (c1 == c2 and p1 < p2): remove(v1, edge) v = v2 else: remove(v2, edge) v = v1 ans += 1 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>
s173862854
p04049
Wrong Answer
import sys sys.setrecursionlimit(5000) def get_diamiter(v, parent, dep, edge): max_dep, cnt, ret_v = dep, 1, v for e in edge[v]: if e != parent: cand_dep, cand_cnt, cand_v = get_diamiter(e, v, dep + 1, edge) if cand_dep > max_dep: max_dep, cnt, ret_v = cand_dep, cand_cnt, cand_v elif cand_dep == max_dep: cnt += cand_cnt return max_dep, cnt, ret_v def remove(v, edge): for row in edge: if v in row: row.remove(v) def main(N, K, edge): ans = 0 _, _, v = get_diamiter(0, -1, 0, edge) while True: d, c1, v1 = get_diamiter(v, -1, 0, edge) if d <= K: break _, c2, v2 = get_diamiter(v1, -1, 0, edge) if c1 < c2: remove(v1, edge) v = v2 else: remove(v2, edge) v = v1 ans += 1 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>
s952720489
p04049
Wrong Answer
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) + 1) 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>
s494568946
p04049
Wrong Answer
from collections import deque N, K = map(int, input().split()) G = [[] for i 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) 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(N-1) D = bfs0(D[0]) C = {e for e in D} l = len(D) if l <= K+1: print(0) exit(0) memo = {} def bfs(s, rest): if rest == 0: return 1 if (s, rest) in memo: return memo[s, rest] que = deque() dist = {s: rest} for v in G[s]: if v in C: continue dist[v] = rest-1 if rest > 1: que.append(v) while que: v = que.popleft() for w in G[v]: if w in dist: continue dist[w] = r = dist[v]-1 if r: que.append(w) memo[s, rest] = res = len(dist) return res ans = 0 for i in range(l-K): cnt = sum(bfs(D[i+j], min(j, K-j)) for j in range(K+1)) ans = max(ans, cnt) 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>
s900344007
p04049
Wrong Answer
from collections import deque N, K = map(int, input().split()) G = [[] for i 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) 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) memo = {} def bfs(s, rest): if rest == 0: return 1 if (s, rest) in memo: return memo[s, rest] que = deque() dist = {s: rest} for v in G[s]: if v in C: continue dist[v] = rest-1 if rest > 1: que.append(v) while que: v = que.popleft() for w in G[v]: if w in dist: continue dist[w] = r = dist[v]-1 if r: que.append(w) memo[s, rest] = res = len(dist) return res ans = 0 for i in range(l-K): cnt = sum(bfs(D[i+j], min(j, K-j)) for j in range(K+1)) ans = max(ans, cnt) 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>
s577183325
p04049
Wrong Answer
from collections import deque N, K = map(int, input().split()) G = [[] for i 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) 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: print(0) exit(0) memo = {} def bfs(s, rest): if rest == 0: return 1 if (s, rest) in memo: return memo[s, rest] que = deque() dist = {s: rest} for v in G[s]: if v in C: continue dist[v] = rest-1 if rest > 1: que.append(v) while que: v = que.popleft() for w in G[v]: if w in dist: continue dist[w] = r = dist[v]-1 if r: que.append(w) memo[s, rest] = res = len(dist) return res ans = 0 for i in range(l-K): cnt = sum(bfs(D[i+j], min(j, K-j)) for j in range(K+1)) ans = max(ans, cnt) 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>
s159843090
p04049
Wrong Answer
def i1(): return int(input()) def i2(): return [int(i) for i in input().split()] [n,k]=i2() e=[[] for i in range(n)] for i in range(n-1): [a,b]=i2() e[a-1].append(b-1) e[b-1].append(a-1) import sys sys.setrecursionlimit(10000) def dfs(v,d): global c for i in range(len(e[v])): if d[e[v][i]]==-1: d[e[v][i]]=d[v]+1 if d[e[v][i]]>=k:c+=1 dfs(e[v][i],d) cc=float("inf") for i in range(n): c=0 d=[-1 for j in range(n)] d[i]=0 dfs(i,d) cc=min(cc,c) print(cc)
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>
s383083592
p04049
Wrong Answer
n,k = map(int,input().split()) branch = dict() for i in range(1,n+1): branch[i] = [] a = [] b = [] for i in range(n-1): atemp,btemp = map(int,input().split()) branch[atemp].append(btemp) branch[btemp].append(atemp) a.append(atemp) b.append(btemp) if k%2 == 0: ans = [] for i in range(1,n+1): q = [i] arrival = [0 for i in range(n+1)] arrival[i] = 1 while len(q) >= 1: for e in range(len(branch[q[0]])): if arrival[branch[q[0]][e]] == 0: q.append(branch[q[0]][e]) arrival[branch[q[0]][e]] = arrival[q[0]]+1 q.pop(0) counter = 0 for e in range(1,n+1): if arrival[e] > k//2+1: counter += 1 ans.append(counter) print(min(ans)) else: ans1 = [] ans2 = [] for i in range(n-1): q1 = [a[i]] q2 = [b[i]] arrival1 = [0 for i in range(n+1)] arrival2 = [0 for i in range(n+1)] while len(q1) >= 1: for e in range(len(branch[q1[0]])): if arrival1[branch[q1[0]][e]] == 0: q1.append(branch[q1[0]][e]) arrival1[branch[q1[0]][e]] = arrival1[q1[0]]+1 q1.pop(0) counter = 0 for e in range(1,n+1): if arrival1[e] > k//2+1: counter += 1 ans1.append(counter) while len(q2) >= 1: for e in range(len(branch[q2[0]])): if arrival2[branch[q2[0]][e]] == 0: q2.append(branch[q2[0]][e]) arrival2[branch[q2[0]][e]] = arrival2[q2[0]]+1 q2.pop(0) counter = 0 for e in range(1,n+1): if arrival2[e] > k//2+1: counter += 1 ans2.append(counter) ans = [] for e in range(n-1): if ans1[e] <= ans2[e]: ans.append(ans1[e]) else: ans.append(ans2[e]) 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>
s223471727
p04049
Wrong Answer
o=lambda:map(int,raw_input().split());r=range;a=10**5 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] 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(n): for j in e[i]: if i<j:l=[0]*n;U(i,j);a=min(a,U(j,i)) else: for i in r(n):l=[0]*n;a=min(a,U(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>
s990482530
p04049
Wrong Answer
o=lambda:map(int,raw_input().split());r=range;a=10**5 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] def U(x,f): 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(n): for j in e[i]: if i<j:l=[0]*n;U(i,j);a=min(a,U(j,i)) else: for i in r(n):l=[0]*n;a=min(a,U(i,i)) 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>
s421202969
p04049
Wrong Answer
o=lambda:map(int,raw_input().split());T,F=True,False;r=range;a=10**5 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] def U(x,f): 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 r(n): for j in e[i]: if i<j:l=[F]*n;U(i,j);a=min(a,U(j,i)) else: for i in r(n):l=[F]*n;a=min(a,U(i,i)) 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>
s650676455
p04049
Wrong Answer
import sys sys.setrecursionlimit(10**6) def dfs1(s,c,cnt): visited[s]=1 for i in v[s]: if visited[i]==0: d[c][i]=d[c][s]+1 if k%2==0 and d[c][i]>k/2: cnt+=1 if k%2==1 and d[c][i]>(k-1)/2: cnt+=1 cnt=dfs1(i,c,cnt) return cnt def dfs2(c1,c2,cnt): visited[c2]=1 cnt+=dfs1(c1,c1,cnt) cnt+=dfs1(c2,c2,cnt) return cnt 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 cnt=dfs1(i,i,cnt) 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)] cnt=dfs2(i,j,cnt) 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>
s260898636
p04049
Wrong Answer
from sys import setrecursionlimit setrecursionlimit(100000) def rad_reach(b,h,dist,E,rad,ne): cnt = 1 for nxt in E[h]: if nxt == b or nxt == ne: continue if dist + 1 == rad: cnt += 1 else: 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>
s188344538
p04049
Wrong Answer
from sys import setrecursionlimit setrecursionlimit(100000) def rad_reach(b,h,dist,E,rad,ne): cnt = 1 for nxt in E[h]: if nxt == b or nxt == ne: continue if dist < rad: cnt += rad_reach(h,nxt,dist+1,E,rad,ne) else: cnt += 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): 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>
s291463450
p04049
Wrong Answer
# -*- 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) over_count = sum([1 if dias[j] > K / 2 else 0 for j in range(N)]) ans = min(ans, over_count) print(ans) else: dias_lst = [] for i in range(N): dias = [-1 for _ in range(N)] dfs(i, 0) dias_lst.append(list(dias)) # print(dias_lst) 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>
s315362321
p04049
Wrong Answer
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 = po() 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>
s255412320
p04049
Wrong Answer
# -*- 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 get_start(): for i in range(N): for j in range(N): if tree[i][j] == 1: return i 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) s = get_start() dfs(s, 0) max_node = dias.index(max(dias)) dias = [-1 for _ in range(N)] dfs(max_node, 0) ans = 0 max_dias = max(dias) while True: # print(disj) if max_dias <= K: print(ans) exit() lst = [i for i in range(N) if disj[i] == 1] # print("lst", lst) counts = [] for l in lst: count = 0 for n in range(N): if tree[l][n] == 1: count = disj[n] break counts.append(count) # print("counts", counts) delete_node = lst[counts.index(min(counts))] # print("delete", delete_node) for i in range(N): if tree[i][delete_node] == 1: disj[i] -= 1 tree[i][delete_node] = 0 tree[delete_node][i] = 0 if disj[i] == 1: max_dias -= 1 disj[delete_node] = 0 ans += 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>
s691034648
p04049
Wrong Answer
# -*- 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 get_start(): for i in range(N): for j in range(N): if tree[i][j] == 1: return i 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) s = get_start() dfs(s, 0) max_node = dias.index(max(dias)) dias = [-1 for _ in range(N)] dfs(max_node, 0) ans = 0 while True: max_dias = max(dias) if max_dias <= K: print(ans) exit() lst = [i for i in range(N) if disj[i] == 1] counts = [] for l in lst: count = 0 for n in range(N): if tree[l][n] == 1: count += 1 counts.append(count) delete_node = lst[counts.index(min(counts))] for i in range(N): tree[i][delete_node] = 0 tree[delete_node][i] = 0 disj[delete_node] = 0 s = get_start() dfs(s, 0) max_node = dias.index(max(dias)) dias = [-1 for _ in range(N)] dfs(max_node, 0) ans += 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>
s654851725
p04049
Time Limit Exceeded
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 q.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)] 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>
s920254795
p04049
Time Limit Exceeded
import sys from collections import deque def bfs(links, s, limit): not_reachable = (1 << len(links)) - 1 q = deque([(0, s, -1)]) while q: cost, v, p = q.popleft() if cost > limit: break not_reachable ^= 1 << v cost += 1 q.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, *ab = map(int, sys.stdin.buffer.read().split()) 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>
s829062897
p04049
Time Limit Exceeded
import sys from collections import deque def bfs(links, s, limit): not_reachable = [True] * len(links) q = deque([(0, s, -1)]) while q: cost, v, p = q.popleft() if cost > limit: break not_reachable[v] = False cost += 1 q.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)] for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 links[a].add(b) links[b].add(a) ans = n thr = k // 2 if k % 2 == 0: ans = min(sum(bfs(links, v, thr)) for v in range(n)) else: dists = [bfs(links, v, thr) for v in range(n)] for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 ans = min(ans, sum(d1 and d2 for d1, d2 in zip(dists[a], dists[b]))) return ans n, k, *ab = map(int, sys.stdin.buffer.read().split()) 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>
s464997249
p04049
Time Limit Exceeded
import sys from collections import deque, defaultdict def bfs(links, s=0): ans = [0] * len(links) q = deque([(0, s, -1)]) while q: cost, v, p = q.popleft() ans[v] = cost cost += 1 q.extend((cost, u, v) for u in links[v] if u != p) return ans def solve(n, k, ab): if k == 1: return n - 2 links = [set() for _ in range(n)] for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 links[a].add(b) links[b].add(a) ans = n thr = k // 2 if k % 2 == 0: for v in range(n): ans = min(ans, sum(d > thr for d in bfs(links, v))) else: cache = {} for a, b in zip(ab[0::2], ab[1::2]): a -= 1 b -= 1 if a in cache: da = cache[a] else: da = cache[a] = bfs(links, a) if b in cache: db = cache[b] else: db = cache[b] = bfs(links, b) ans = min(ans, sum(d1 > thr and d2 > thr for d1, d2 in zip(da, db))) return ans n, k, *ab = map(int, sys.stdin.buffer.read().split()) 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>
s355921971
p04049
Time Limit Exceeded
import sys import numpy as np from scipy.sparse import csr_matrix from scipy.sparse.csgraph import floyd_warshall n, k, *ab = map(int, sys.stdin.buffer.read().split()) aaa = np.array(ab[0::2], dtype=np.int64) - 1 bbb = np.array(ab[1::2], dtype=np.int64) - 1 ccc = np.ones(n - 1, dtype=np.int64) graph = csr_matrix((ccc, (aaa, bbb)), shape=(n, n)) dists = floyd_warshall(graph, directed=False) if k % 2 == 0: print((dists > k // 2).sum(axis=0).min()) else: ans = n over = dists > k // 2 for a, b in zip(ab[0::2], ab[1::2]): ans = min(ans, (over[a - 1] & over[b - 1]).sum()) 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>
s805598937
p04049
Time Limit Exceeded
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 import numpy as np 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=np.array(get_v(n1,k//2)) c2=np.array(get_v(n2,k//2)) ret = n- sum(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>
s516507320
p04049
Time Limit Exceeded
# 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 queue import Queue 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 = Queue(maxsize = len(adj)) q.put(u) vis[u] = True while (q.qsize() > 0): elem = q.get() for v in adj[elem]: if (vis[v] == False): q.put(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 = Queue(maxsize = len(adj)) q.put(edge[0]) q.put(edge[1]) vis[edge[0]] = True vis[edge[1]] = True while (q.qsize() > 0): elem = q.get() for v in adj[elem]: if (vis[v] == False): q.put(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>
s004564469
p04049
Time Limit Exceeded
# 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 queue import Queue def main(): n, d = [int(x) for x in input().split(" ")] adj = [] edges = [] for i in range(n): adj.append([]) for i in range(n-1): a, b = [int(x) for x in 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 = Queue(maxsize = len(adj)) q.put(u) vis[u] = True while (q.qsize() > 0): elem = q.get() for v in adj[elem]: if (vis[v] == False): q.put(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 = Queue(maxsize = len(adj)) q.put(edge[0]) q.put(edge[1]) vis[edge[0]] = True vis[edge[1]] = True while (q.qsize() > 0): elem = q.get() for v in adj[elem]: if (vis[v] == False): q.put(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>
s127743502
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: t = dist[v] + 1 dist[nv] = t if t > 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>
s241648386
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 if cnt >= ans: return cnt 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>