B1. Maximum Control (easy)

本文介绍了一个算法问题,即计算在一个形如树状的星系中,有多少个星球仅通过单一的超空间隧道与其它星球相连。文章提供了输入输出示例及完整的C++代码实现。
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;
}
<think>好的,我需要理解用户的问题:关于PyTorch中clamp函数的用法以及在交集计算中使用minimum和maximum操作的含义。用户提供了几个引用,我需要结合这些内容来解答。 首先,用户提到的引用[1]中提到了计算两个边界框的最小外接矩形,用到了maximum和minimum操作。例如,cw是b1_x2和b2_x2的最大值减去b1_x1和b2_x1的最小值。这可能涉及两个框的坐标比较,取最大或最小的坐标值来形成外接矩形的宽和高。 然后,关于clamp函数,用户提到了代码中的.clamp_(0)。我需要回忆PyTorch中clamp的作用。clamp通常用于将张量中的元素限制在某个范围内。例如,clamp(min=0)会将所有小于0的值置为0。这里的clamp_(0)可能是指将结果限制在0以上,避免负数出现,这在计算面积或IoU时很重要,因为负数没有意义。 接下来,需要结合这些操作在IoU计算中的具体应用。在目标检测中,计算两个框的交集通常需要确定交集的左上角和右下角坐标。左上角的x坐标是两个框左上x的最大值,y坐标同理。右下角的x坐标是两个框右下x的最小值,y同理。如果右下坐标小于左上坐标,说明没有交集,此时交集的宽或高会是负数,用clamp(min=0)可以将这些负值置零,表示交集面积为0。 例如,假设框A的x2小于框B的x1,那么交集的宽度会是负数,clamp之后变为0,正确表示没有交集。这在引用[1]中的GIoU、DIoU、CIoU计算中是基础步骤,确保计算的数值合理。 另外,用户提到的引用[2]中的代码使用了bbox_iou函数,其中可能包含clamp操作来保证IoU值在0到1之间。引用[3]中的Focus机制可能需要处理不同的IoU返回值,但clamp在这里的作用可能相似,保持数值稳定性。 总结起来,minimum和maximum用于确定交集或外接矩形的边界,而clamp函数确保计算结果不会出现负值,保持后续计算的正确性。需要将这些步骤详细解释,并结合具体例子说明,比如交集的宽高计算,以及clamp如何避免负数的情况。</think>### PyTorch中的`clamp`函数及`minimum/maximum`操作在交集计算中的应用 #### 1. `minimum`和`maximum`操作的作用 在目标检测的交集(IoU)计算中,`minimum`和`maximum`操作用于确定两个边界框的交集区域坐标: - **交集左上角坐标**:取两个框左上角坐标的较大值,即: $$x_{1}^{\text{inter}} = \max(x_{1}^{A}, x_{1}^{B}), \quad y_{1}^{\text{inter}} = \max(y_{1}^{A}, y_{1}^{B})$$ - **交集右下角坐标**:取两个框右下角坐标的较小值,即: $$x_{2}^{\text{inter}} = \min(x_{2}^{A}, x_{2}^{B}), \quad y_{2}^{\text{inter}} = \min(y_{2}^{A}, y_{2}^{B})$$ 如果$x_{2}^{\text{inter}} < x_{1}^{\text{inter}}$或$y_{2}^{\text{inter}} < y_{1}^{\text{inter}}$,说明两个框无交集[^1]。 #### 2. `clamp`函数的功能 `torch.clamp(input, min, max)`用于将张量元素限制在指定范围内: - 当输入值小于`min`时,替换为`min` - 当输入值大于`max`时,替换为`max` 在IoU计算中,`clamp(min=0)`的作用是: - **消除负面积**:若两个框无交集,计算出的宽度或高度为负数,`clamp(min=0)`会将其置零 - **保证数值合理性**:交集面积始终非负,公式为: $$ \text{inter\_area} = (x_{2}^{\text{inter}} - x_{1}^{\text{inter}}).clamp(0) \times (y_{2}^{\text{inter}} - y_{1}^{\text{inter}}).clamp(0) $$ #### 3. 代码示例解析 以YOLOv10的改进代码为例: ```python cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # 外接矩形宽度 ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # 外接矩形高度 iou = inter_area / union_area.clamp(min=eps) # 防止除以零 ``` - `b1_x2.maximum(b2_x2)`:取两个框右边界较大的值 - `b1_x1.minimum(b2_x1)`:取两个框左边界较小的值 - `clamp(min=eps)`:确保分母不小于极小值`eps`(如1e-7),避免数值不稳定[^2] #### 4. 应用场景对比 | 操作类型 | 作用场景 | 典型应用 | |----------------|------------------------------|---------------------------| | `maximum` | 确定外接矩形边界 | GIoU计算中的凸包生成 | | `minimum` | 确定交集区域边界 | 基础IoU计算 | | `clamp(min=0)` | 消除负面积/负距离 | DIoU中的中心点距离计算| | `clamp(max=1)` | 限制损失函数输出范围 | FocusIoU中的权重调节[^3] | #### 5. 实际计算示例 假设两个框坐标为: - 框A:`x1=10, y1=20, x2=30, y2=40` - 框B:`x1=25, y1=15, x2=35, y2=30` 则: ```python inter_x1 = max(10,25) = 25 inter_y1 = max(20,15) = 20 inter_x2 = min(30,35) = 30 inter_y2 = min(40,30) = 30 width = (30-25).clamp(0) = 5 height = (30-20).clamp(0) = 10 inter_area = 5*10 = 50 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值