题目链接  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(贪心 && 树的直径)的更多相关文章

  1. Codeforces.911F.Tree Destruction(构造 贪心)

    题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\ ...

  2. Codeforces 911F Tree Destruction

    Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...

  3. [Codeforces 911F] Tree Destruction 解题报告(贪心)

    题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...

  4. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  5. Farthest Nodes in a Tree ---LightOj1094(树的直径)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...

  6. [NOI2003]逃学的小孩 (贪心+树的直径+暴力枚举)

    Input 第一行是两个整数N(3 <= N <= 200000)和M,分别表示居住点总数和街道总数.以下M行,每行给出一条街道的信息.第i+1行包含整数Ui.Vi.Ti(1<=Ui ...

  7. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  8. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径

    传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...

  9. codeforces#1187E. Tree Painting(树换根)

    题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...

随机推荐

  1. python双向链表的疑问(Question)

    Table of Contents 1. 问题 问题 在看 collections.OrderedDict 的源码时,对于它如何构造有序的结构这一部分不是很理解,代码如下: class Ordered ...

  2. SVM python小样例

    SVM有很多种实现,但是本章只关注其中最流行的一种实现,即序列最小化(SMO)算法在此之后,我们将介绍如何使用一种称为核函数的方式将SVM扩展到更多的数据集上基于最大间隔的分割数据优点:泛化错误率低, ...

  3. Django admin模块使用search时报错:django.core.exceptions.FieldError: Related Field got invalid lookup: contains

    日志如下: <class 'django.core.handlers.wsgi.WSGIRequest'> ------------registered_admins: {'spaceCl ...

  4. 67、activity中调用fragment内部自定义的方法

    fragment: /** * author: Created by zzl on 15/11/19. */ @SuppressLint("validFragment") publ ...

  5. RSA进阶之共模攻击

    适用场景: 同一个n,对相同的m进行了加密,e取值不一样. e1和e2互质,gcd(e1,e2)=1 如果满足上述条件,那么就可以在不分解n的情况下求解m 原理 复杂的东西简单说: 如果gcd(e1, ...

  6. 性能测试工具—Jmeter

    Jmeter视频教程: 在我要自学网搜索:关键字即可

  7. LeetCode661图片平滑器

    题目描述:包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个, ...

  8. Linux内核使用毫秒延时函数

    毫秒延时函数:mdelay() 微妙延时函数:ndelay() #ifndef mdelay #define mdelay(n) (/ (__builtin_constant_p(n) &&a ...

  9. 【Appnium+C#+Winform自动化测试系列】一、获取本机连接的设备、启动多个Appnium和获取本机启动的Appnium

    本系列内容,准备根据所完成的项目为基线,一步一步的把整个设计和实现过程梳理. 先从基本的一些环境问题入手,梳理清楚关于手机设备和Appnium.因为我们在后面的建立Appnium连接时,需要设备名字和 ...

  10. POJ 3111 K Best(01分数规划)

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 9876   Accepted: 2535 Case Time ...