Codeforce-958B1

B:
B1. Maximum Control (easy)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
Input
Copy
5
4 1
4 2
1 3
1 5
Output
Copy
3
Input
Copy
4
1 2
4 3
1 4
Output
Copy
2
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.

给出N个行星,接下来N-1行 :每行两个数字,意味着这两个行星有通道

求出有几个行星是只有一条通道的

扫描二维码关注公众号,回复: 2185856 查看本文章

思路:最多为1000个行星,那么开一个数组1001,(num[1001]),代表着第X 个行星有几个通道

再设置计数变量ans=0,当某行星只有一条通道时,ans++

最后输出ans

#include <iostream>
#include <cstring>
using namespace std;
int num[1001];
int main()
{
    memset(num,0,sizeof(num));
    int n,ans=0;
    cin>>n;
    int m=n-1;
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        num[a]++;
        num[b]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(num[i]==1)ans++;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/consine926/article/details/80961906