【每日一题】8.Shortest Path (树上DFS)
题目链接:Here
题意总结:给定的是无向图(树),要求把分成 \(n/2\) 对 让权值最小
思路:
看一下范围 在加上是一棵树 所以做法应该是dfs 复杂度为 \(\mathcal{O}(n)\)
直接去考虑贡献
设当前父节点为x 如果x的子树(包括x自己)的大小是个奇数 意味着什么呢
因为要两两配对,那么意味着这奇数个数中,一定有一个数要有外界配对
那么他就一定会经过x到x的父节点的那条边
所以 dfs 计算子树的大小 如果子树大小是个奇数 ,那么对答案有贡献 就要加上父节点往上连接的那一条边
AC 代码:246ms,1964kb
using ll = long long;
const int N = 1e4 + 10;
ll ans;
vector<ll> cnt(N);
vector<pair<int, ll>> e[N];
void dfs(int u, int fa, ll len) {
for (auto it : e[u]) {
int v = it.first;
if (v == fa) continue;
dfs(v, u, it.second);
cnt[u] += cnt[v];
}
if (cnt[u] & 1) ans += len;
}
void solve() {
int n;
cin >> n;
ans = 0;
for (int i = 0; i <= n; ++i) e[i].clear();
for (int i = 0; i <= n; ++i) cnt[i] = 1;
for (int i = 1, u, v; i < n; ++i) {
ll len;
cin >> u >> v >> len;
e[u].push_back(make_pair(v, len));
e[v].push_back(make_pair(u, len));
}
dfs(1, 0, 0);
cout << ans << "\n";
}
另外附上学姐讲解:Here
【每日一题】8.Shortest Path (树上DFS)的更多相关文章
- 【每日一题】【树的dfs递归,返回多次,注意都遍历完后才最终返回】2022年1月6日-112. 路径总和
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum .判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum .如果存在,返回 tr ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Codeforces Beta Round #3 A. Shortest path of the king 水题
A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...
- [每日一题2020.06.08]洛谷P1605 DFS
今天cf又杯具的只写出2题, 虽然AB题20分钟左右就搞定了, 但是CD写了2个小时也没写出来 D题我用到了DFS, 虽然必不正确, 但是我至少发现了一个问题, 那就是我连DFS都忘了, 于是怒找DF ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- Kuro and Walking Route CodeForces - 979C (树上DFS)
Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
- CF938G Shortest Path Queries 和 CF576E Painting Edges
这两道都用到了线段树分治和按秩合并可撤销并查集. Shortest Path Queries 给出一个连通带权无向图,边有边权,要求支持 q 个操作: x y d 在原图中加入一条 x 到 y 权值为 ...
- leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]
题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...
随机推荐
- 1. Shell 基本用法
重点: 条件测试. read. Shell 环境配置. case. for. find. xargs. gzip,bzip2,xz. tar. sed. 1)编程基础 Linus 说:Talk is ...
- 深入了解UUID:生成、应用与优势
一.引言 在当今数字化时代,唯一标识一个对象的能力变得越来越重要.UUID(Universally Unique Identifier,通用唯一标识符)应运而生,作为一种保证全球唯一性的标识方法,广泛 ...
- SnagIt 9-12 注册码
SnagIt 9 注册码: AM5SC-8LWML-MVMWU-DTLGE-ERMBE SnagIt 10 注册码: 5HCAK-DEGMZ-EYABA-M4LCC-ACBE2DFKDA-JZ5FC- ...
- Linux下^m符号删除
Linux下^m符号删除 从Windows上复制的代码到Linux尾会有M字符,通过下命令可以删除. :%s/\r//
- 踩坑:nacos启动报错提示需要设置JDK环境 ,报错:ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! !!
换了个Windows11的新电脑,因为个人工作.学习需要,就重新下载了Nacos并解压使用,结果就踩了个坑,使用下面命令启动Nacos服务端时: startup.cmd -m standalone 直 ...
- 前端传递Base64字符串,后端转流存入OSS
工具类 public static BufferedInputStream base64Convert(String base64) { // 解码 base64 = base64.split(&qu ...
- Python subprocess 使用(一)
Python subprocess 使用(一) 本文主要讲下 subprocess 的简单使用. 1: 通过subprocess 获取设备信息 import subprocess def get_an ...
- Python 潮流周刊第 33 期(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- 微信现金红包开发 PHP
第一次在cnblogs发文章 微信商家后台-现金红包开发 sdk <?php class wxPay { //配置参数信息 const SHANGHUHAO = "1430998xxx ...
- Windows Server2016 默认使用英文输入法或默认使用中文输入法
1.确认是Server2016操作系统及以后版本 2.打开开始菜单"设置"--"时间和语言" 3.添加英文输入法(已存在可以跳过) 找到"区域与语言& ...