Maximum Control (easy) (Codeforces 958B1)
Tags: Implement, implementation

The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them.The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel.How much work is there left to be done: that is, how many remote planets are there?

Input: The first line of the input contains an integer N (2 ≤ N ≤ 1000) – the number of planets in the galaxy.The next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 ≤ u, v ≤ N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets.

Output: A single integer denoting the number of remote planets.

Note: In the first example, only planets 2, 3 and 5 are connected by a single tunnel.In the second example, the remote planets are 2 and 3.Note that this problem has only two versions – easy and medium.

Sample Inputs:
5
4 1
4 2
1 3
1 5

4
1 2
4 3
1 4

Sample Outputs:
3

2

Solution (Python):
n = int(input())
arr = {i: 0 for i in range(1, n + 1)}
for i in range(n - 1):
    (a, b) = map(int, input().split())
    arr[a] += 1
    arr[b] += 1
answer = 0
for i in arr:
    if arr[i] < 2:
        answer += 1
print(answer)

Solution (Python):
t = int(input())
res = [0 for i in range(t)]
for _ in range(t - 1):
    (a, b) = list(map(int, input().split()))
    res[a - 1] += 1
    res[b - 1] += 1
print(res.count(1))

Solution (Python):
N = int(input())
a = [0] * N
for _ in range(N - 1):
    (u, v) = list(map(int, input().split()))
    a[u - 1] += 1
    a[v - 1] += 1
print(a.count(1))