湖北省大学程序设计竞赛(网络同步赛) D. Who killed Cock Robin

本文介绍了一种通过深度优先搜索(DFS)算法计算给定无根树中不同连通子树数量的方法。将无根树转化为有根树,并利用动态规划思想,通过递归方式计算每个节点作为根节点时的不同连通子树的数量。

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

 
 

链接:https://www.nowcoder.com/acm/contest/104/C来源:牛客网

题目描述

Who killed Cock Robin?

I, said the Sparrow, With my bow and arrow,I killed Cock Robin.

Who saw him die?

I, said the Fly.With my little eye,I saw him die.

Who caught his blood?

I, said the Fish,With my little dish,I caught his blood.

Who'll make his shroud?

I, said the Beetle,With my thread and needle,I'll make the shroud.    

.........

All the birds of the air

Fell a-sighing and a-sobbing.

When they heard the bell toll.

For poor Cock Robin.

March 26, 2018

Sparrows are a kind of gregarious animals,sometimes the relationship between them can be represented by a tree.

The Sparrow is for trial, at next bird assizes,we should select a connected subgraph from the whole tree of sparrows as trial objects.

Because the relationship between sparrows is too complex, so we want to leave this problem to you. And your task is to calculate how many different ways can we select a connected subgraph from the whole tree.

输入描述:

 
  

The first line has a number n to indicate the number of sparrows.

The next n-1 row has two numbers x and y per row, which means there is an undirected edge between x and y.

输出描述:

 
  

The output is only one integer, the answer module 10000007 (107+7) in a line

题目意思

给你一个无根树,问你有多少个不同的连通子树。

个人思路

当然是D了个P啦,把无根树变有根树,找个度为1的点开始DFS。

然后,每次返回当前节点子树能组成多少个不同的子树。

ans+=每次DFS的返回值。

关键在于求当前节点能组成多少个不同的子树!

这时候就要感谢我的队友,感谢他证明了愚蠢的我,不然会被卡很久滴。

比如上图(忽略劣质画工),用W数组表示带上这个节点会产生多少个子树,对于节点5,它是个叶子,只会产生一个子树,w[5]=1,同理,w[6]=1,w[4]=1,w[7]=1。现在到了节点3了,节点3有两个儿子5和6,怎么求呢,队友提醒加个空集,即对于子节点的取法可以不取它,用空表示,所以,w[i]=w[i]*(w[i的儿子]+1),这个DP就D出来啦~

注意一下,虽然最后要对1e7+7取模,但是,中间1e5*1e5会爆了int,所以,以后不要在乎那些小的空间啦。

最后,附上美丽的代码。

#include<bits/stdc++.h>
using namespace std;
const int ha = 10000007;
vector<int> g[200010];
bool vis[200010];
long long w[200010];
long long ans=0;
int du[200010];
int n;
void read(){
    int j,k;
    vis[1]=true;    w[1]=1;
    memset(du,0,sizeof(du));
    for (int i=1;i<n;++i){
        vis[i+1]=true;  w[i+1]=1;
        scanf("%d%d",&j,&k);
        g[j].push_back(k);
        g[k].push_back(j);
        du[j]++;    du[k]++;
    }
}
void dfs(int u){
    vis[u]=false;
    w[u]=1;
    vector<int>::iterator it;
    int a=1;
    for (it=g[u].begin();it!=g[u].end();++it){
        int k=*it;
        if (vis[k]){
            dfs(k);
            w[u]=(w[u]*(w[k]+1))%ha;
        }
    }
    ans+=w[u];
    ans%=ha;
    return ;
}
void solve(){
    for (int i=1;i<=n;++i){
        if (du[i]==1&&vis[i]){
            dfs(i);
        }
    }
    printf("%d\n",ans);
    return ;
}
int main(){
    scanf("%d",&n);
    read();
    solve();
    return 0;
}


构建 `mmcv-full` 轮子时遇到终止错误可能由多种原因引起,包括但不限于硬件资源不足、依赖项缺失或配置不当等问题。以下是针对该问题的分析和解决方案: ### 可能的原因及解决方法 #### 1. **内存不足** 如果系统可用内存较低,在编译过程中可能会因内存耗尽而导致进程被杀死(killed)。 可以通过以下方式缓解此问题: - 增加交换空间(swap space),以便在物理内存不足时提供额外的空间支持[^3]。 - 减少并行编译线程数。通常,默认情况下会使用多核处理器的最大性能来加速编译过程,但这也会增加内存消耗。可以尝试通过设置环境变量 `MAKEFLAGS=-jN` 来控制并发数量,其中 N 是希望使用的 CPU 核心数。 ```bash export MAKEFLAGS="-j4" pip install mmcv-full ``` #### 2. **CUDA 或 PyTorch 配置不匹配** `mmcv-full` 的安装需要与当前系统的 CUDA 和 PyTorch 版本兼容。如果不一致,则可能导致编译失败或其他运行时错误。建议先确认已安装的 PyTorch 和 CUDA 版本,并选择合适的预编译包或者手动指定版本进行安装[^4]。 验证命令如下: ```python import torch print(torch.__version__) print(torch.cuda.is_available()) ``` 对于特定组合下的安装指导,请访问官方文档页面获取最新推荐链接[^5]。 #### 3. **缺少必要的开发工具链** 某些 Linux 发行版上默认未安装完整的 C++ 编译器及相关库文件,这同样会造成无法成功创建 whl 文件的情况发生。因此需确保 GCC/G++ 已经正确部署完毕并且其版本满足最低需求标准之上;另外还需要注意 Python 开发头文件的存在与否——这些都属于基础性的前提条件之一[^6]。 更新软件包列表以及安装所需组件的例子如下所示: ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config python-pip python-setuptools python-wheel python-numpy python-scipy cython ``` --- ### 提供一段简单的测试脚本来验证最终成果是否正常工作 一旦上述所有潜在障碍都被排除之后,就可以再次尝试重新执行 pip 安装流程看能否顺利完成整个操作链条直至结束为止。下面给出了一段用于初步检验功能的小程序代码片段作为参考依据之用: ```python from mmdet.apis import inference_detector, init_detector config_file = 'path/to/config/file.py' checkpoint_file = 'path/to/checkpoint/file.pth' model = init_detector(config_file, checkpoint_file, device='cuda:0') result = inference_detector(model, img_path) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值