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

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

 

链接: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;
}


<think> 我们在处理adb install命令时遇到"Killed"错误。这个错误通常与内存不足、存储空间不足、APK文件损坏或设备兼容性问题有关。以下是根据常见原因和解决方案的逐步排查指南: ### 1. **检查设备存储空间** - 错误可能由设备存储空间不足引起。使用以下命令检查: ```bash adb shell df /data ``` 如果`/data`分区空间不足,需要清理设备存储(如删除无用应用、缓存等)。 ### 2. **检查APK文件完整性** - 文件损坏或不完整会导致安装失败。尝试重新下载或传输APK文件,并在其他设备上测试安装。 ### 3. **检查设备内存(RAM)** - 安装过程需要足够内存。如果设备内存不足,系统可能终止安装进程。重启设备释放内存,然后重试。 ### 4. **使用`-g`选项授予权限** - 部分APK需要特定权限。尝试安装时自动授予所有权限: ```bash adb install -g "D:\APK\Android4.4\UDP-sample-debug.apk" ``` ### 5. **调整安装位置** - 尝试将APK安装到外部存储(如SD卡),但需设备支持: ```bash adb install -s "D:\APK\Android4.4\UDP-sample-debug.apk" ``` ### 6. **分步安装(先推送再安装)** - 将APK推送到设备再安装: ```bash adb push "D:\APK\Android4.4\UDP-sample-debug.apk" /sdcard/ adb shell pm install /sdcard/UDP-sample-debug.apk ``` ### 7. **检查Android版本兼容性** - 确保APK支持设备系统版本。查看`AndroidManifest.xml`中的`minSdkVersion`和`targetSdkVersion`。 ### 8. **查看Logcat日志** - 获取详细错误信息: ```bash adb logcat | findstr "Killed" ``` 或完整日志: ```bash adb logcat -d > log.txt ``` ### 9. **尝试低内存模式安装** - 使用`-l`选项(针对低内存设备): ```bash adb install -l "D:\APK\Android4.4\UDP-sample-debug.apk" ``` ### 10. **重启ADB服务** - 重启ADB服务并重试: ```bash adb kill-server adb start-server ``` 如果以上方法无效,请提供设备型号和Android版本以便进一步分析。 §§相关问题§§ 1. 如何查看Android设备的存储空间使用情况? 2. adb install命令有哪些常用参数? 3. 如何解析APK文件以检查其兼容性? </think> ### ADB Install出现"Killed"错误的解决方案 当执行`adb install D:\APK\Android4.4\UDP-sample-debug.apk`命令时出现**Killed**错误,通常由以下原因引起。请按顺序排查: --- ### 1. **内存不足(最常见原因)** - **现象**:Android设备内存(RAM)不足时,系统会强制终止安装进程 - **解决方案**: 1. 重启设备:`adb reboot` 2. 关闭后台应用: ```bash adb shell am kill-all ``` 3. 检查内存状态: ```bash adb shell cat /proc/meminfo | grep MemFree ``` 4. 尝试低内存安装模式: ```bash adb install -l D:\APK\Android4.4\UDP-sample-debug.apk ``` --- ### 2. **存储空间不足** - **检查方法**: ```bash adb shell df /data ``` 若`Avail`值低于50MB需清理空间 - **清理建议**: - 卸载无用应用:`adb uninstall <包名>` - 清除缓存:`adb shell pm trim-caches 500000000`(清理500MB缓存) --- ### 3. **APK文件问题** - **验证步骤**: 1. 检查APK完整性(Windows): ```powershell certutil -hashfile "D:\APK\Android4.4\UDP-sample-debug.apk" SHA256 ``` 2. 重签名APK测试: ```bash adb install -t D:\APK\Android4.4\UDP-sample-debug.apk ``` 3. 尝试安装其他APK验证是否文件损坏 --- ### 4. **权限问题** - **解决方法**: ```bash # 1. 授予完整权限安装 adb install -g D:\APK\Android4.4\UDP-sample-debug.apk # 2. 关闭SELinux(临时) adb shell setenforce 0 ``` --- ### 5. **设备兼容性问题** - **排查步骤**: 1. 检查设备支持的ABI: ```bash adb shell getprop ro.product.cpu.abi ``` 2. 查看APK支持的架构: ```bash aapt dump badging D:\APK\Android4.4\UDP-sample-debug.apk | grep native-code ``` 3. 若架构不匹配,需重新编译APK --- ### 6. **ADB服务故障** - **重置ADB**: ```bash adb kill-server adb start-server adb devices # 确认设备重新连接 ``` --- ### 7. **系统限制(针对Android 4.4)** 旧版本Android的特殊处理: ```bash # 分割安装(避免内存峰值) adb push D:\APK\Android4.4\UDP-sample-debug.apk /sdcard/ adb shell pm install /sdcard/UDP-sample-debug.apk ``` > ⚠️ 如果所有方法均失败,查看内核日志定位原因: > ```bash > adb shell dmesg | grep -i "killed" > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值