Tree Destruction

先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, deg[N], deptha[N], depthb[N];
vector<int> G[N];
queue<int> que;
vector<pair<PII, int>> vc; PII dfs(int u, int fa, int* depth) {
if(fa) depth[u] = depth[fa] + ;
else depth[u] = ;
PII ans = mk(depth[u], u);
for(auto& v : G[u]) {
if(v == fa) continue;
ans = max(ans, dfs(v, u, depth));
}
return ans;
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
deg[u]++; deg[v]++;
}
int A = dfs(, , deptha).se;
int B = dfs(A, , deptha).se;
dfs(B, , depthb);
for(int i = ; i <= n; i++)
if(i != A && i != B && deg[i] == )
que.push(i);
LL ans = ;
while(!que.empty()) {
int u = que.front(); que.pop();
if(deptha[u] > depthb[u]) {
ans += deptha[u];
vc.push_back(mk(mk(A, u), u));
} else {
ans += depthb[u];
vc.push_back(mk(mk(B, u), u));
}
for(auto& v : G[u])
if(--deg[v] == )
que.push(v);
}
que.push(B);
while(!que.empty()) {
int u = que.front(); que.pop();
if(u == A) break;
ans += deptha[u];
vc.push_back(mk(mk(A, u), u));
for(auto& v : G[u])
if(--deg[v] == )
que.push(v);
}
printf("%lld\n", ans);
for(auto& t : vc) printf("%d %d %d\n", t.fi.fi, t.fi.se, t.se);
return ;
} /*
*/

Codeforces 911F Tree Destruction的更多相关文章

  1. Codeforces 911F Tree Destruction(贪心 && 树的直径)

    题目链接  Tree Destructi 题意  给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个.答案每次加上两个选定的叶子之间的距离. 求最后答案的最大值. 首先求出树的某一条直径,令其端 ...

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

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

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

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

  4. CF911F Tree Destruction 解题报告

    CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...

  5. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  6. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  7. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  8. Codeforces 23E Tree

    http://codeforces.com/problemset/problem/23/E 题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大.思路:f[i][j]代表当前把j个贡献给i的父亲 ...

  9. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

随机推荐

  1. (常用)configparser,hashlib,hamc模块

    configparser模块 #专门解析my.ini这种形式的文件(cnf) import configparser  config=configparser.ConfigParser()  conf ...

  2. 解决ftp客户端连接验证报错Server sent passive reply with unroutable address. Using server address instead

    最近在linux服务器安装vsftp服务.经过一轮设置,终于可以连接上了,用winSCP连接,刷新目录就提示这个错误. 解决办法: vim /etc/vsftpd.conf  ,编辑配置文件,最后加上 ...

  3. jquery中的attr与prop的区别,什么时候用attr,什么时候用prop

    只要有 Boolean() 属性的,简单说就是具有true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),(其实这些都是表单类的), ...

  4. redhat7.3 superset的离线安装

    superset是一个python 开发的可视化工具,可以与kylin连接进行数据分析,在官网的讲解中,采用了在线安装方式,生产环境中有yum源,但是没有网,不得不采用离线安装方式.(我们先在有网的环 ...

  5. js——this

    每个函数的this是在调用时绑定的,完全取决于函数的调用位置 1. 绑定规则总结 一般情况下,按下列顺序从下至上来判断this的绑定对象(绑定的优先级从下至上递减) 默认:在严格模式下绑定到undef ...

  6. Confluence 6 手动安装语言包和找到更多语言包

    手动安装语言包 希望以手动的方式按照语言包,你需要按照下面描述的方式上传语言包.一旦你安装成功后,语言包插件将会默认启用. 插件通常以 JAR 或者 OBR(OSGi Bundle Repositor ...

  7. OC对象本质

    @interface person:NSObject{ @public int _age; } @end @implementation person @end @interface student: ...

  8. SpringMVC文件下载与JSON格式

    点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...

  9. python网络爬虫笔记(九)

    4.1.1 urllib2 和urllib是两个不一样的模块 urllib2最简单的就是使用urllie2.urlopen函数使用如下 urllib2.urlopen(url[,data[,timeo ...

  10. cf自训6

    cf946D 背包+区间dp 好题 /* 先预处理出每行消去i个1后可以的到的最小时间: 先求每行的前缀和,枚举左端点和右端点,消去的1 cost=tot-sum[r]+sum[l-1],区间长度=r ...