codeforces 161D. Distance in Tree(树dp)

本文介绍了一种使用树形动态规划(树DP)的方法来解决在一个给定树中寻找具有特定距离k的顶点对数量的问题。通过定义状态dp[u][j]为以u为根的子树中到u的距离为j的节点数量,并利用递归思想更新这些值,最终能够高效地计算出所需答案。
D. Distance in Tree
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

tree is a connected graph that doesn't contain any cycles.

The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.

You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (vu) and (uv) are considered to be the same pair.

Input

The first line contains two integers n and k (1 ≤ n ≤ 500001 ≤ k ≤ 500) — the number of vertices and the required distance between the vertices.

Next n - 1 lines describe the edges as "ai bi" (without the quotes) (1 ≤ ai, bi ≤ nai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.

Output

Print a single integer — the number of distinct pairs of the tree's vertices which have a distance of exactly k between them.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Sample test(s)
input
5 2
1 2
2 3
3 4
2 5
output
4
input
5 3
1 2
2 3
3 4
4 5
output
2
Note

In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).

题意:给出一棵树,每条边的长度都为1。现在给出一个k,问树中距离为k的两个点共有多少对。

思路:树dp。设dp[u][j]为以u为根的子树中到u的距离为j的结点个数,运用乘法原理更新dp值。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;

const int maxn = 50005;
const int maxm = 1000005;
const int INF = 1 << 20;
int n, k, ans;
int dp[maxn][505];
vector<int> G[maxn];
void dfs(int u, int pre){
    memset(dp[u], 0, sizeof(dp[u]));
    dp[u][0] = 1;
    for(int i = 0; i < (int)G[u].size(); i++)
    {
        int v = G[u][i];
        if(v == pre) continue;
        dfs(v, u);
        for(int j = 1; j <= k; j++)
        ans += dp[u][j - 1] * dp[v][k - j];
        for(int j = 1; j <= k; j++)
        dp[u][j] += dp[v][j - 1];
    }
}
int main()
{
    int a, b;
    while(~scanf("%d%d", &n, &k))
    {
        for(int i = 0; i <= n; i++) G[i].clear();
        for(int i = 0; i < n - 1; i++)
        {
            scanf("%d%d", &a, &b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        ans = 0;
        dfs(1, -1);
        printf("%d\n", ans);
    }
    return 0;
}


C:\Users\16079>wsl.exe -d Ubuntu-24.04 hkw@localhost:/mnt/c/Users/16079$ ollama run deepseek-r1:1.5b Error: llama runner process has terminated: exit status 127 hkw@localhost:/mnt/c/Users/16079$ ls /usr/local/cuda-12.x ls: cannot access '/usr/local/cuda-12.x': No such file or directory hkw@localhost:/mnt/c/Users/16079$ sudo apt install --reinstall cuda-toolkit-12-0 # 根据实际版本调整 [sudo] password for hkw: Reading package lists... Done Building dependency tree... Done Reading state information... Done N: Ignoring file 'ubuntu.sources.save.1' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension N: Ignoring file 'ubuntu.sources.save.2' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension E: Unable to locate package cuda-toolkit-12-0 hkw@localhost:/mnt/c/Users/16079$ ./ollama_llama_server -bash: ./ollama_llama_server: No such file or directory hkw@localhost:/mnt/c/Users/16079$ nvidia-smi # 查看驱动版本和GPU状态 Thu Mar 13 20:40:10 2025 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 565.75 Driver Version: 566.26 CUDA Version: 12.7 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA GeForce RTX 4050 ... On | 00000000:01:00.0 Off | N/A | | N/A 42C P8 1W / 78W | 0MiB / 6141MiB | 0% Default | | | | N/A | +-----------------------------------------+------------------------+---------
最新发布
03-14
<think>嗯,用户遇到了在WSL Ubuntu上安装CUDA工具包时出现错误,退出状态127,同时运行ollama时提示CUDA版本不匹配和NVIDIA驱动兼容性问题。我需要一步步帮他分析可能的原因和解决方案。 首先,退出状态127通常表示命令未找到。这可能是因为CUDA工具包没有正确安装,或者环境变量没配置好。用户提到使用的是WSL,所以得确认WSL是否支持CUDA,并且已经安装了正确的驱动。 接着,CUDA版本不匹配的问题。用户可能在WSL里安装了不兼容的CUDA版本,或者宿主机(Windows)的NVIDIA驱动版本太旧,不支持WSL所需的CUDA版本。需要检查宿主机驱动版本是否符合CUDA的要求,并确保WSL内的CUDA工具包版本与驱动兼容。 然后是ollama运行时的错误。这可能是因为ollama依赖的CUDA版本与系统安装的不一致。需要确认ollama要求的CUDA版本,并安装对应的工具包。 接下来,我需要考虑具体的解决步骤: 1. 检查宿主机的NVIDIA驱动版本。用户可能需要升级到支持WSL CUDA的最新驱动,比如>=515.43.04。用户可以通过Windows的设备管理器或NVIDIA官网下载最新驱动。 2. 确认WSL内安装了正确的CUDA工具包。根据NVIDIA的文档,WSL应该使用cuda-wsl-ubuntu的仓库,而不是常规的CUDA Ubuntu仓库。用户可能添加了错误的源,导致安装失败。 3. 验证环境变量是否正确设置。安装CUDA后,需要将CUDA路径添加到PATH,并设置LD_LIBRARY_PATH。用户提到的source ~/.bashrc可能没有正确配置这些变量,或者安装过程中出现错误。 4. 检查CUDA版本与NVIDIA驱动的兼容性。使用nvidia-smi查看驱动支持的CUDA版本,再使用nvcc -V查看实际安装的CUDA版本。两者如果不一致,可能导致不兼容。 5. 对于ollama的问题,可能需要指定使用特定CUDA版本,或者重新安装与系统CUDA版本匹配的ollama版本。 可能的错误点: - 用户可能没有按照WSL专用的CUDA安装指南操作,导致工具包安装不正确。 - 环境变量未正确配置,导致nvcc命令找不到,出现127错误。 - 宿主机驱动过旧,无法支持所需的CUDA版本,导致版本不匹配。 需要指导用户逐步检查这些方面,并提供具体的命令和步骤,比如如何添加正确的CUDA仓库、安装工具包、配置环境变量,以及如何检查驱动和CUDA版本的兼容性。</think>### WSL Ubuntu CUDA工具包安装与兼容性问题的逐步解决方案 --- #### **一、验证宿主机NVIDIA驱动兼容性** 1. **Windows宿主机驱动要求** WSL CUDA需要Windows 11 + NVIDIA驱动≥515.43.04(推荐更新至最新驱动)[^1]。 - 右键点击Windows开始菜单 → 选择"设备管理器" → 展开"显示适配器" → 检查驱动版本 - 访问[NVIDIA驱动下载页](https://www.nvidia.com/Download/index.aspx)手动更新 2. **验证WSL内GPU可见性** 在WSL Ubuntu终端输入: ```bash $ nvidia-smi ``` - 成功输出GPU信息表示驱动已正确加载 - 若报错`command not found`,说明驱动未正确安装或WSL集成未启用 --- #### **二、修复CUDA工具包安装错误(退出状态127)** 1. **清理旧安装残留** ```bash $ sudo apt --purge remove "cuda*" $ sudo apt autoremove $ sudo rm -rf /usr/local/cuda* ``` 2. **添加WSL专用CUDA仓库** ```bash $ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin $ sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600 $ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub $ sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/ /" ``` 3. **安装CUDA工具包** ```bash $ sudo apt update $ sudo apt install cuda-toolkit-12-3 # 根据nvidia-smi显示的兼容版本选择 ``` 4. **验证安装** ```bash $ nvcc -V # 应显示CUDA版本 $ echo $PATH | grep cuda # 检查路径是否包含/usr/local/cuda/bin ``` --- #### **三、解决CUDA版本不匹配问题** 1. **版本对照表** | NVIDIA驱动版本 | 支持的最高CUDA版本 | |----------------|--------------------| | 535.86.05 | 12.2 | | 545.23.08 | 12.3 | | 555.42.02 | 12.4 | 2. **强制指定CUDA版本(临时方案)** 在运行ollama前设置: ```bash $ export LD_LIBRARY_PATH=/usr/local/cuda-12.3/lib64:$LD_LIBRARY_PATH $ ollama run deepseek-r1 --cuda-version=12.3 ``` --- #### **四、Ollama运行环境配置** 1. **检查Ollama CUDA绑定** ```bash $ ollama list runners | grep cuda ``` - 若显示`cuda: not available`,需重新编译安装: ```bash $ OLLAMA_CUSTOM_CPU=1 OLLAMA_CUDA_VERSION=12.3 make install ``` 2. **创建兼容性符号链接(当版本号有微小差异时)** ```bash $ sudo ln -s /usr/local/cuda-12.3 /usr/local/cuda-12.2 ``` --- #### **五、验证完整环境** 执行验证脚本: ```bash $ git clone https://github.com/wilicc/gpu-burn $ cd gpu-burn $ make $ ./gpu_burn 60 # 运行60秒压力测试 ``` - 正常输出应显示持续计算的GPU利用率 - 若出现`CUDA error: invalid device ordinal`,检查WSLg配置: ```bash $ echo $DISPLAY | grep :0 # 应返回:0 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值