【每日一题】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 ...
随机推荐
- 【外包杯】【报错】微信小程序 “[app.json 文件内容错误]app.json:未找到[“pages”][0]对应 pages/xx/xx.wxml(或其他)文件” 报错 的简单解决方案(已解决)
问题的解决方案 删除components目录下的uni-link文件夹
- 基于DotNetty实现一个接口自动发布工具 - 通信实现
基于 DotNetty 实现通信 DotNetty : 是微软的 Azure 团队,使用 C#实现的 Netty 的版本发布.是.NET 平台的优秀网络库. 项目介绍 OpenDeploy.Commu ...
- java当中组件和窗口的相容问题(里面包含了这些方法的作用)
窗口也称为容器,在书中大部分称为容器,这里主要讲的是底层容器. JFrame():创建一个无标签的窗口. JFrame(String s):创建标题为s的窗口 setBounds(int a,int ...
- springcloud+nacos项目启动后,登陆连接服务器时超时:Connection timed out no further information
问题现象:项目启动后,登陆连接服务器时超时:Connection timed out no further information 192.168.42.190:4004 原因:我的配置有问题,在na ...
- [ABC274G] Security Camera 3
Problem Statement There is a grid with $H$ rows from top to bottom and $W$ columns from left to righ ...
- 使用C++和QT实现Log自定义日志系统
MyLog 说明 使用QT的qInstallMessageHandler函数结合qDebug,qInfo实现自定义的日志系统 输出日志到文件和控制台 自动检测日志文件大小 自动更新日志文件修改日期 自 ...
- 理解 Paimon changelog producer
介绍 目的 Chaneglog producer 的主要目的是为了在 Paimon 表上产生流读的 changelog, 所以如果只是批读的表是可以不用设置 Chaneglog producer 的. ...
- C++中数字与字符串之间的转换(转载自http://www.cnblogs.com/luxiaoxun/)
C++中数字与字符串之间的转换 1.字符串数字之间的转换(1)string --> char * string str("OK"); char * p = str ...
- 开发AI量化策略所遇到的坑
AI只是工具,想要驾驭AI还得自身有点功底,不然反而会被工具所害,甚至从信仰AI变为抵制AI.本文简单介绍开发AI量化选股策略中所遇到的各种坑,希望大家有所收获,少走弯路. 本文为BigQuant用户 ...
- eclipse工具使用
eclipse下载 官网下载:https://www.eclipse.org/downloads/packages/ 打开后,找到Eclipse IDE for Java Developers点击进入 ...