Codeforces 911F Tree Destruction(贪心 && 树的直径)
题目链接 Tree Destructi
题意 给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个。答案每次加上两个选定的叶子之间的距离。
求最后答案的最大值。
首先求出树的某一条直径,令其端点分别为L, R。
把L看成树的根,那么R一定是叶子结点。
对于那些非直径上的点,离他们最远的点肯定是L或R中的一个(可能也有其他的,但是L或R肯定已经最大了)
所以依次把这些非直径上的点删掉,删掉的时候在L和R中选择一个就行了。
最后把直径删掉即可。
时间复杂度$O(nlogn)$ (应该是可以做到$O(n)$的,但是我比较懒)
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 2e5 + 10; vector <int> v[N]; int n;
int cnt;
int x, y;
int L, R; int f[N][20];
int a[N];
int deep[N], father[N];
int Father[N];
int vis[N];
int c[N];
int isroot[N]; LL ans = 0; queue <int> q; void dfs(int x, int fa, int now){
deep[x] = now;
if (fa){
f[x][0] = fa;
for (int i = 0; f[f[x][i]][i]; ++i)
f[x][i + 1] = f[f[x][i]][i];
} if (now > cnt) cnt = now, L = x;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, now + 1);
}
} void dfs2(int x, int fa, int now){
father[x] = fa;
if (now > cnt) cnt = now, R = x;
for (auto u : v[x]){
if (u == fa) continue;
dfs2(u, x, now + 1);
}
} int LCA(int a, int b){
if (deep[a] < deep[b]) swap(a, b);
for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i)
if (delta & 1) a = f[a][i]; if (a == b) return a;
dec(i, 19, 0) if (f[a][i] != f[b][i]){
a = f[a][i];
b = f[b][i];
} return f[a][0];
} int dis(int x, int y){
int z = LCA(x, y);
return deep[x] + deep[y] - 2 * deep[z];
} void dfs3(int x, int fa){
vis[x] = 1;
Father[x] = fa;
for (auto u : v[x]){
if (vis[u]) continue;
dfs3(u, x);
++c[x];
}
} int main(){ scanf("%d", &n);
rep(i, 1, n){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} L = 0, cnt = 0;
dfs(1, 0, 0); cnt = 0;
R = 0;
memset(father, 0, sizeof father);
dfs2(L, 0, 0); cnt = 0;
x = R;
while (x){
++cnt;
a[cnt] = x;
x = father[x];
} memset(vis, 0, sizeof vis);
rep(i, 1, cnt) vis[a[i]] = 1;
rep(i, 1, n) isroot[i] = vis[i];
rep(i, 1, n) if (!vis[i]){
int now = max(dis(i, L), dis(i, R));
ans += 1ll * now;
} memset(c, 0, sizeof c);
rep(i, 1, cnt) dfs3(a[i], 0);
rep(i, 1, n) if (c[i] == 0 && !isroot[i]) q.push(i); ans = ans + 1ll * cnt * (cnt - 1) / 2;
printf("%lld\n", ans); while (!q.empty()){
x = q.front(); q.pop();
vis[x] = 1;
int al = dis(x, L), ar = dis(x, R);
if (al > ar) printf("%d %d %d\n", L, x, x);
else printf("%d %d %d\n", R, x, x); int u = Father[x];
--c[u];
if (c[u] == 0 && !isroot[u]) q.push(u);
} rep(i, 1, cnt - 1) printf("%d %d %d\n", a[i], a[cnt], a[i]);
return 0;
}
Codeforces 911F Tree Destruction(贪心 && 树的直径)的更多相关文章
- Codeforces.911F.Tree Destruction(构造 贪心)
题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\ ...
- Codeforces 911F Tree Destruction
Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- [NOI2003]逃学的小孩 (贪心+树的直径+暴力枚举)
Input 第一行是两个整数N(3 <= N <= 200000)和M,分别表示居住点总数和街道总数.以下M行,每行给出一条街道的信息.第i+1行包含整数Ui.Vi.Ti(1<=Ui ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- codeforces#1187E. Tree Painting(树换根)
题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...
随机推荐
- SHIWEITI
//Wannafly挑战赛19(牛客网) //A 队列Q #include <iostream> #include <cstdio> #include <cstring& ...
- 3 View - 状态保持 session
1.状态保持 http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状态 客户端与服务器端的一次通信,就是一次会话 实现状态保持的方式:在客户端或服务器端存储与会话有关的数据 存储方式包 ...
- Android stadio litepal
今天看到技术交流群里有人招聘Android,要求会litepal. 我立马百度了下.嗯,我的学习技术的精神,是值得称赞的. litepal就是操作数据库的一个框架.git地址: https://git ...
- 使用 Dom4j 将 XML 转换为 MAP
本文为转载:http://blog.sina.com.cn/s/blog_6145ed810100z164.html 原文地址. 自己仅作备忘录方便查找留了一份. 这是解析Xml 的辅助类 pack ...
- Ntdsutil.exe
Ntdsutil.exe 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Ntdsutil.exe 是一个为 Active Directory 提供管理设施的命令行工具 ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- ogre3D学习基础19 --- 材质的继承,纹理的滚动与旋转
以上一节为基础,废话不多说. 首先新增一个节点,用于比较显示 //新增一个节点 ent = mSceneMgr->createEntity("Quad"); ent-> ...
- xss games20关小游戏附源代码
1. get方式的的值直接输出来了. ?name=<script>alert(1)</script> 2. 同样没有过滤,不过需要闭合前边的双引号和>. "&g ...
- 【转】Netty系列之Netty编解码框架分析
http://www.infoq.com/cn/articles/netty-codec-framework-analyse/ 1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称 ...
- Java接口对Hadoop集群的操作
Java接口对Hadoop集群的操作 首先要有一个配置好的Hadoop集群 这里是我在SSM框架搭建的项目的测试类中实现的 一.windows下配置环境变量 下载文件并解压到C盘或者其他目录. 链接: ...