问题

### Instructions: You are an expert Python programmer. You will be given a question (problem specification) and will generate a correct Python program that matches the specification and passes all tests. You will NOT return anything except for the program.
### Question:
You are given a tree with N vertices: vertex 1, vertex 2, \ldots, vertex N.
The i-th edge (1\leq i\lt N) connects vertex u _ i and vertex v _ i.
Consider repeating the following operation some number of times:

- Choose one leaf vertex v and delete it along with all incident edges.

Find the minimum number of operations required to delete vertex 1.
What is a tree?
A tree is an undirected graph that is connected and has no cycles.
For more details, see: Wikipedia "Tree (graph theory)".

What is a leaf?
A leaf in a tree is a vertex with a degree of at most 1.

Input

The input is given from Standard Input in the following format:
N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}

Output

Print the answer in a single line.

Constraints


- 2\leq N\leq3\times10^5
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\lt N)
- The given graph is a tree.
- All input values are integers.

Sample Input 1

9
1 2
2 3
2 4
2 5
1 6
6 7
7 8
7 9

Sample Output 1

5

The given graph looks like this:

For example, you can choose vertices 9,8,7,6,1 in this order to delete vertex 1 in five operations.

Vertex 1 cannot be deleted in four or fewer operations, so print 5.

Sample Input 2

6
1 2
2 3
2 4
3 5
3 6

Sample Output 2

1

In the given graph, vertex 1 is a leaf.
Hence, you can choose and delete vertex 1 in the first operation.

Sample Input 3

24
3 6
7 17
7 20
7 11
14 18
17 21
6 19
5 22
9 24
11 14
6 23
8 17
9 12
4 17
2 15
1 17
3 9
10 16
7 13
2 16
1 16
5 7
1 3

Sample Output 3

12

### Format: Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within delimiters as follows.
```python
# YOUR CODE HERE
```

### Answer: (use the provided format with backticks)

答案

模型 模型答案 正确答案
金声玉亮2.0 ```python
import sys
from collections import deque

def main():
input = sys.stdin.read
data = input().split()

N = int(data[0])
edges = [(int(data[2 * i + 1]), int(data[2 * i + 2])) for i in range(N - 1)]

tree = [[] for _ in range(N + 1)]
for u, v in edges:
tree[u].append(v)
tree[v].append(u)

leaves = deque()
for i in range(1, N + 1):
if len(tree[i]) == 1:
leaves.append(i)

operations = 0
while leaves:
operations += 1
for _ in range(len(leaves)):
leaf = leaves.popleft()
if leaf == 1:
print(operations)
return
parent = tree[leaf][0]
tree[parent].remove(leaf)
if len(tree[parent]) == 1 and parent != 1:
leaves.append(parent)

if __name__ == "__main__":
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
deepseek-r1

```python
import sys
from sys import stdin

def main():
sys.setrecursionlimit(1 << 25)
N = int(stdin.readline())
if N == 1:
print(1)
return
adj = [[] for _ in range(N+1)]
for _ in range(N-1):
u, v = map(int, stdin.readline().split())
adj[u].append(v)
adj[v].append(u)
if len(adj[1]) == 1:
print(1)
return

children = [c for c in adj[1] if c != 0]

total = 0

def compute_max_depth(c):
max_depth = 0
stack = [(c, -1, 0)] # (node, parent, current_depth)
while stack:
node, parent, depth = stack.pop()
nonlocal max_depth
if depth > max_depth:
max_depth = depth
for neighbor in adj[node]:
if neighbor != parent and neighbor != 1:
stack.append((neighbor, node, depth + 1))
return max_depth

for c in children:
if c == 1:
continue
max_depth = compute_max_depth(c)
total += max_depth + 1

print(total)

if __name__ == "__main__":
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
gpto1 ```python
# YOUR CODE HERE
import sys
import threading
def main():
import sys
sys.setrecursionlimit(1 << 20)
N = int(sys.stdin.readline())
adj = [[] for _ in range(N + 1)]
degrees = [0] * (N + 1)
for _ in range(N -1):
u, v = map(int, sys.stdin.readline().split())
adj[u].append(v)
adj[v].append(u)
degrees[u] +=1
degrees[v] +=1
if degrees[1] ==1:
print(1)
else:
print((N +1)//2)
threading.Thread(target=main).start()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
gpt-4o-2024-08-06 ```python
# YOUR CODE HERE
import sys
from collections import defaultdict, deque

def min_operations_to_delete_vertex_1(n, edges):
# Create an adjacency list for the tree
tree = defaultdict(list)
for u, v in edges:
tree[u].append(v)
tree[v].append(u)

# If vertex 1 is already a leaf, we can delete it in one operation
if len(tree[1]) == 1:
return 1

# BFS to find the farthest leaf from vertex 1
def bfs(start):
visited = [False] * (n + 1)
queue = deque([(start, 0)]) # (current_node, distance)
visited[start] = True
farthest_node = start
max_distance = 0

while queue:
current, distance = queue.popleft()
if distance > max_distance:
max_distance = distance
farthest_node = current

for neighbor in tree[current]:
if not visited[neighbor]:
visited[neighbor] = True
queue.append((neighbor, distance + 1))

return farthest_node, max_distance

# Find the farthest leaf from vertex 1
farthest_leaf, _ = bfs(1)

# Find the farthest leaf from the previously found farthest leaf
_, max_distance = bfs(farthest_leaf)

# The minimum number of operations required to delete vertex 1 is half the diameter
return (max_distance + 1) // 2

def main():
input = sys.stdin.read
data = input().split()

n = int(data[0])
edges = []
index = 1
for _ in range(n - 1):
u = int(data[index])
v = int(data[index + 1])
edges.append((u, v))
index += 2

result = min_operations_to_delete_vertex_1(n, edges)
print(result)

if __name__ == "__main__":
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
DeepSeek-Coder-V2-Lite-Instruct ```python
# YOUR CODE HERE
import sys
from collections import defaultdict, deque

def read_ints():
return list(map(int, sys.stdin.readline().strip().split()))

N = int(sys.stdin.readline().strip())
edges = [read_ints() for _ in range(N - 1)]

# Create adjacency list
adj_list = defaultdict(list)
for u, v in edges:
adj_list[u].append(v)
adj_list[v].append(u)

# Find leaf nodes
leaf_nodes = [node for node, neighbors in adj_list.items() if len(neighbors) == 1]

# Perform BFS to find the minimum number of operations to delete vertex 1
def bfs(start):
visited = set([start])
queue = deque([start])
operations = 0

while queue:
for _ in range(len(queue)):
node = queue.popleft()
neighbors = adj_list[node]
for neighbor in neighbors:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
operations += 1

return operations - 1 # Subtract 1 because the last operation does not count

# Start BFS from any leaf node
min_operations = bfs(leaf_nodes[0])

# Output the result
print(min_operations)
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
Meta-Llama-3.1-405B-Instruct-Turbo ```python
import sys
from collections import deque

def solve():
n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n - 1):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)

def bfs(start):
queue = deque([(start, 0)])
visited = [False] * (n + 1)
visited[start] = True
max_distance = 0
while queue:
node, distance = queue.popleft()
max_distance = max(max_distance, distance)
for neighbor in graph[node]:
if not visited[neighbor]:
visited[neighbor] = True
queue.append((neighbor, distance + 1))
return max_distance

print(bfs(1))

if __name__ == "__main__":
solve()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
Meta-Llama-3.1-70B-Instruct-Turbo ```python
import sys
from collections import deque

def input():
return sys.stdin.readline().strip()

def solve():
n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n - 1):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)

q = deque()
q.append(1)
parent = [-1] * (n + 1)
while q:
u = q.popleft()
for v in graph[u]:
if v != parent[u]:
parent[v] = u
q.append(v)

dp = [0] * (n + 1)
for i in range(n, 0, -1):
if len(graph[i]) == 1:
dp[i] = 1
else:
for v in graph[i]:
if v != parent[i]:
dp[i] += dp[v]

print(dp[1])

if __name__ == "__main__":
solve()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
Qwen2-72B-Instruct ```python
from collections import defaultdict, deque

def main():
N = int(input())
edges = [list(map(int, input().split())) for _ in range(N-1)]

graph = defaultdict(set)
for u, v in edges:
graph[u].add(v)
graph[v].add(u)

queue = deque()
leaf_count = 0
for node in graph:
if len(graph[node]) == 1:
queue.append(node)
leaf_count += 1

while queue:
node = queue.popleft()
for neighbor in graph[node]:
graph[neighbor].remove(node)
if len(graph[neighbor]) == 1 and neighbor != 1:
queue.append(neighbor)
leaf_count += 1
break

print(N - 1 - leaf_count)

if __name__ == "__main__":
main()
```
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
baichuan4 ```python
from collections import defaultdict, deque

def find_min_operations(N, edges):
# Create adjacency list
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)

# Find the depth of each node from vertex 1
depth = [0] * (N + 1)
depth[1] = 0
queue = deque([1])
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if depth[neighbor] == 0: # Not visited yet
depth[neighbor] = depth[node] + 1
queue.append(neighbor)

# Find the maximum depth of any leaf node
max_depth = 0
for i in range(2, N + 1):
if len(graph[i]) == 1: # Leaf node
max_depth = max(max_depth, depth[i])

# The minimum number of operations is the maximum depth of any leaf node
return max_depth

# Read input from stdin
N = int(input())
edges = [tuple(map(int, input().split())) for _ in range(N - 1)]

# Solve the problem
result = find_min_operations(N, edges)

# Write the answer to stdout
print(result)
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
doubao ```python
import collections

def min_operations(n, edges):
graph = collections.defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)

def dfs(node, parent):
if len(graph[node]) == 1 and graph[node][0] == parent:
return 1
count = 0
for child in graph[node]:
if child!= parent:
count += dfs(child, node)
return count + 1

return dfs(1, -1) - 1

n = int(input())
edges = [tuple(map(int, input().split())) for _ in range(n - 1)]

print(min_operations(n, edges))
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
gpto1 ```python
# YOUR CODE HERE
import sys
import threading
def main():
import sys
sys.setrecursionlimit(1 << 20)
N = int(sys.stdin.readline())
adj = [[] for _ in range(N + 1)]
degrees = [0] * (N + 1)
for _ in range(N -1):
u, v = map(int, sys.stdin.readline().split())
adj[u].append(v)
adj[v].append(u)
degrees[u] +=1
degrees[v] +=1
if degrees[1] ==1:
print(1)
else:
print((N +1)//2)
threading.Thread(target=main).start()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
kimi ```python
import sys
sys.setrecursionlimit(500000)

def dfs(v, p):
ret = 1
cnt = 0
for nv in G[v]:
if nv == p:
continue
cnt += 1
ret += dfs(nv, v)
if v != 0 and cnt == 1:
ret = 0
return ret

def main():
global G
n = int(input())
G = [[] for _ in range(n)]
for _ in range(n - 1):
(u, v) = map(int, input().split())
u -= 1
v -= 1
G[u].append(v)
G[v].append(u)
print(dfs(0, -1))
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
minimax ```python
from collections import defaultdict

def min_operations_to_delete_vertex_1(N, edges):
# Create a graph from the edges
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)

# Function to find the number of operations required to delete a leaf vertex
def dfs(node, parent):
if len(graph[node]) == 1 and node != 1: # If it's a leaf and not vertex 1
return 1
count = 0
for neighbor in graph[node]:
if neighbor != parent:
count += dfs(neighbor, node)
return count

# Start DFS from vertex 1
return dfs(1, -1)

# Read the inputs from stdin
N = int(input())
edges = [tuple(map(int, input().split())) for _ in range(N - 1)]

# Solve the problem and write the answer to stdout
print(min_operations_to_delete_vertex_1(N, edges))
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
wenxinyiyan ```python
import sys
from collections import defaultdict, deque

def main():
N = int(input())
tree = defaultdict(list)
in_degree = [0] * (N + 1)

for _ in range(N - 1):
u, v = map(int, input().split())
tree[u].append(v)
tree[v].append(u)
in_degree[u] += 1
in_degree[v] += 1

# Perform a topological sort (BFS) from leaves to root
queue = deque()
for i in range(1, N + 1):
if in_degree[i] == 1 and i != 1: # Leaf nodes (excluding vertex 1)
queue.append(i)

operations = 0
while queue:
size = len(queue)
for _ in range(size):
leaf = queue.popleft()
operations += 1
for neighbor in tree[leaf]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 1 and neighbor != 1: # New leaf node
queue.append(neighbor)

# If vertex 1 becomes a leaf, it will be deleted in the next iteration
if in_degree[1] == 1:
break

# The final operation will delete vertex 1
print(operations + 1 if in_degree[1] == 0 else operations)

if __name__ == "__main__":
main()
```
This program reads the input from standard input, constructs an adjacency list representation of the tree, and initializes an in-degree array to keep track of the number of incident edges for each vertex. It then performs a topological sort (using BFS) from the leaves to the root, deleting leaf nodes and updating the in-degrees accordingly. The program counts the number of operations required to delete all leaf nodes until vertex 1 becomes a leaf or is isolated. Finally, it prints the minimum number of operations required to delete vertex 1, considering whether it was already deleted as a leaf or needs an additional operation.
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
zhipu ```python
import sys
input = sys.stdin.read
sys.setrecursionlimit(10**6)

def main():
data = input().split()
N = int(data[0])
edges = [[] for _ in range(N)]

index = 1
for _ in range(N - 1):
u = int(data[index]) - 1
v = int(data[index + 1]) - 1
edges[u].append(v)
edges[v].append(u)
index += 2

# DFS to find the minimum number of operations
def dfs(node, parent):
# If the node is a leaf, it can be deleted in one operation
if len(edges[node]) == (1 if parent != -1 else 0):
return 1

# Otherwise, sum up the operations required for all children
total_operations = 0
for neighbor in edges[node]:
if neighbor != parent:
total_operations += dfs(neighbor, node)

return total_operations

# Start DFS from node 0 (vertex 1 in the problem, 0-indexed here)
result = dfs(0, -1)
print(result)

if __name__ == "__main__":
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]
deepseek-v3 ```python
# YOUR CODE HERE
import sys
from collections import deque

def main():
N = int(sys.stdin.readline())
edges = [[] for _ in range(N+1)]
for _ in range(N-1):
u, v = map(int, sys.stdin.readline().split())
edges[u].append(v)
edges[v].append(u)

if len(edges[1]) == 1:
print(1)
return

depth = [0] * (N+1)
visited = [False] * (N+1)
q = deque()
q.append(1)
visited[1] = True

while q:
u = q.popleft()
for v in edges[u]:
if not visited[v]:
depth[v] = depth[u] + 1
visited[v] = True
q.append(v)

operations = 0
for i in range(2, N+1):
if len(edges[i]) == 1:
operations += depth[i]

print(operations)

if __name__ == "__main__":
main()
```
[{"input": "9\n1 2\n2 3\n2 4\n2 5\n1 6\n6 7\n7 8\n7 9\n", "output": "5\n", "testtype": "stdin"}, {"input": "6\n1 2\n2 3\n2 4\n3 5\n3 6\n", "output": "1\n", "testtype": "stdin"}, {"input": "24\n3 6\n7 17\n7 20\n7 11\n14 18\n17 21\n6 19\n5 22\n9 24\n11 14\n6 23\n8 17\n9 12\n4 17\n2 15\n1 17\n3 9\n10 16\n7 13\n2 16\n1 16\n5 7\n1 3\n", "output": "12\n", "testtype": "stdin"}]