「CF911F」Tree Destruction
传送门
Luogu
解题思路
显然的贪心策略,因为每次都要尽量使得删点后的收益最大。
我们可以求出树的直径(因为树上的任意一个节点与其距离最远的点一定是直径的端点)。
然后我们对于所有不是直径上的点,从叶子开始,从下往上删点,最后再由深而浅删掉直径。
最后输出答案即可。
细节注意事项
- 有些地方的计算不要写错式子之类的
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 200010;
const int __ = 400010;
int tot, head[_], nxt[__], ver[__];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int n, rt, lf, mark[_], fa[_], dep[_];
queue < pair < int, int > > Q;
inline void dfs1(int u, int f) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dep[v] = dep[u] + 1, dfs1(v, u);
}
}
inline void dfs2(int u, int f) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
fa[v] = u, dfs2(v, u), mark[u] |= mark[v];
}
}
LL ans = 0;
inline void dfs3(int u, int f, int lca) {
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dfs3(v, u, mark[v] ? v : lca);
}
if (!mark[u]) {
if (dep[u] > dep[u] + dep[lf] - 2 * dep[lca])
ans += dep[u], Q.push(make_pair(rt, u));
else
ans += dep[u] + dep[lf] - 2 * dep[lca], Q.push(make_pair(lf, u));
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dep[1] = 0, dfs1(1, 0);
rt = 1;
for (rg int i = 1; i <= n; ++i)
if (dep[i] > dep[rt]) rt = i;
dep[rt] = 0, dfs1(rt, 0);
lf = rt;
for (rg int i = 1; i <= n; ++i)
if (dep[i] > dep[lf]) lf = i;
mark[lf] = 1;
dfs2(rt, 0);
dfs3(rt, 0, rt);
for (rg int i = lf; i != rt; i = fa[i])
ans += dep[i], Q.push(make_pair(rt, i));
printf("%lld\n", ans);
while (!Q.empty()) {
pair < int, int > x = Q.front(); Q.pop();
printf("%d %d %d\n", x.first, x.second, x.second);
}
return 0;
}
完结撒花 \(qwq\)
「CF911F」Tree Destruction的更多相关文章
- 「BZOJ2654」tree
「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...
- 「AGC010F」 Tree Game
「AGC010F」 Tree Game 传送门 切了一个 AGC 的题,很有精神. 于是决定纪念一下. 首先如果任意一个人在点 \(u\),他肯定不会向点权大于等于 \(a_u\) 的点走的,因为此时 ...
- 「POJ3237」Tree(树链剖分)
题意 给棵n个点的树.边有边权然后有三种操作 1.CHANGE i v 将编号为i的边权变为v 2.NEGATE a b 将a到b的所有边权变为相反数. 3.QUERY a b 查询a b路径的最大边 ...
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- CF911F Tree Destruction 解题报告
CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- 「AGC035C」 Skolem XOR Tree
「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...
随机推荐
- 在虚拟机中使用Git
自己如何从安装虚拟机到使用git进行项目代码版本管理的部分教程因为是自学所以没有好的教程只能自己进行百度,网上的教程太多了但都是只是一个模块没有从头到尾详细的教程,我们如果有个详细的教程本来只需花很少 ...
- 配置数据库属性validationQuery
配置数据库时,属性validationQuery默认值为“select 1”,对于oracle值应为“select 1 from dual” validationQuery属性:用来验证数据库连接的语 ...
- Python入门2 —— 变量
一:问号三连 1.什么是变量? 变 指的是事物的状态是可以发生变化的 量 指的是记录事物的状态 2.为什么要有变量? 为了让计算机像人一样去记录事物的状态 3.怎么用变量? 先定义 后引用 二:变量的 ...
- codeforces Codeforces Round #597 (Div. 2) Constanze's Machine 斐波拉契数列的应用
#include<bits/stdc++.h> using namespace std; ]; ]; ; int main() { dp[] = ; scanf(); ); ; i< ...
- python 中if和elif的区别
如果程序中判断事件很多,全部用if的话,会遍历整个程序,用elif 程序运行时,只要if或后续某一个elif之一满足逻辑值为True,则程序执行完对应输出语句后自动结束该轮if-elif(即不会再去冗 ...
- 如何更改已经pushed的commit的注释信息(How to change the pushed commit message)
1, 修改最后一次注释(Modify the last comment message) git commit -amend 2,修改之前的注释 1)输入: git rebase -i HEAD~3 ...
- 16进制 32进制 base64之间的区别
Base64: 包含大写字母(A-Z),小写字母(a-z),数字(0-9)以及+/; Base32: 而Base32中只有大写字母(A-Z)和数字234567: Base16: 而Base16就是16 ...
- sqli-libs(29(jspstudy)-31关)
Less_29 Less-29: 需要用到jspstudy跟phpstudy 搭建jspstudy: sqli-labs-master文件夹下面还有tomcat文件,这才是真正的关卡,里面的jsp ...
- plsql 导出oracle数据库
plsql 导出数据库有两个问题,一个是只导出结构,一个是导出表结构加数据这样的,首先人家让我导成sql语句 这不是简单,首先打开PLSQL 一.导出结构 1.然后tools->Export U ...
- 10day rpm简单用法qa ql qf "`"用法
查看软件是否安装: [root@oldboyedu ~]# rpm -qa sl -q表示查询 -a表示所有 sl-5.02-1.el7.x86_64 查看软件包中有哪些信息 [root@oldboy ...