B1. Maximum Control (easy)

本文介绍了一个算法问题,即计算在一个形如树状的星系中,有多少个星球仅通过单一的超空间隧道与其它星球相连。文章提供了输入输出示例及完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 23 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.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a[10002];
    int n,i,x,y,ans=0;
    memset(a,0,sizeof(a));
    cin>>n;
    for(i=1;i<n;i++){
        cin>>x>>y;
        a[x]++,a[y]++;
    }
    for(i=1;i<=n;i++){
        if(a[i]==1)
            ans++;
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值