[CF911F]Tree Destruction
题意翻译
给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少.
首先我们知道,到一个点距离最远的点是直径的端点。考虑贪心,如果我要最大化一个不在选定直径上的点的贡献,一定是将它和某个直径的端点连在一起算贡献。也就是说在这个点消失前直径一直存在,所以我们先把直径外的点全部删除并选择一个较远的直径端点计算贡献,然后再删除直径上的点即可。
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define M 200010
using namespace std;
int read()
{
char ch=getchar();int x=;
while(ch>''||ch<'') ch=getchar();
while(ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x;
}
int n,m,num,L,R,cnt;
int head[M],id[M],deep[M],f[M],fa[M],in[M];
int res1[M],res2[M],res3[M];
bool vis[M];
long long ans;
queue<int>Q;
struct point{int to,next;}e[M<<];
void add(int from,int to)
{
e[++num].next=head[from];
e[num].to=to;
head[from]=num;
}
void dfs(int x,int fa)
{
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(to==fa) continue;
deep[to]=deep[x]+;
dfs(to,x);
}
}
void mark(int x,int tar)
{
while()
{
vis[x]=true;int nxt=;
if(x==tar) return;
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
if(deep[to]==deep[x]-)
{
nxt=to;
break;
}
}
fa[x]=nxt;x=nxt;
}
}
int main()
{
n=read();
for(int a,b,i=;i<n;i++)
{
a=read();b=read();
add(a,b); add(b,a);
in[a]++,in[b]++;
}
deep[]=;
dfs(,);
for(int i=;i<=n;i++) if(deep[i]>deep[L]) L=i;
deep[L]=; dfs(L,);
for(int i=;i<=n;i++) f[i]=deep[i]-,id[i]=L;
for(int i=;i<=n;i++) if(deep[i]>deep[R]) R=i;
deep[R]=; dfs(R,);
for(int i=;i<=n;i++) if(deep[i]->f[i]) f[i]=deep[i]-,id[i]=R;
deep[L]=; dfs(L,); mark(R,L);
for(int i=;i<=n;i++) if(in[i]==&&!vis[i]) Q.push(i);
while(!Q.empty())
{
int x=Q.front();Q.pop();
res1[++cnt]=x;res2[cnt]=id[x];res3[cnt]=x;
ans+=f[x];
for(int i=head[x];i;i=e[i].next)
{
int to=e[i].to;
in[to]--;
if(!vis[to]&&in[to]==) Q.push(to);
}
}
for(int i=R;i!=L;i=fa[i])
{
res1[++cnt]=i;res2[cnt]=L;res3[cnt]=i;
ans+=deep[i]-deep[L];
}
printf("%lld\n",ans);
for(int i=;i<=cnt;i++) printf("%d %d %d\n",res1[i],res2[i],res3[i]);
return ;
}
[CF911F]Tree Destruction的更多相关文章
- CF911F Tree Destruction 解题报告
CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...
- CF911F Tree Destruction (树的直径,贪心)
题目链接 Solution 1.先找出树的直径. 2.遍历直径沿途的每一个节点以及它的子树. 3.然后对于每个非直径节点直接统计答案,令直径的两个端点为 \(x_1,x_2\) . \[Ans=\su ...
- Codeforces 911F Tree Destruction
Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...
- 「CF911F」Tree Destruction
传送门 Luogu 解题思路 显然的贪心策略,因为每次都要尽量使得删点后的收益最大. 我们可以求出树的直径(因为树上的任意一个节点与其距离最远的点一定是直径的端点). 然后我们对于所有不是直径上的点, ...
- Codeforces 911F Tree Destruction(贪心 && 树的直径)
题目链接 Tree Destructi 题意 给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个.答案每次加上两个选定的叶子之间的距离. 求最后答案的最大值. 首先求出树的某一条直径,令其端 ...
- Codeforces.911F.Tree Destruction(构造 贪心)
题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\ ...
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
- Educational Codeforces Round 35
Nearest Minimums 相同的数里最小的数里的最小距离 Solution Two Cakes Solution Three Garlands 瞎比试 Solution Inversion C ...
- CodeForces - 963B Destruction of a Tree (dfs+思维题)
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
随机推荐
- Zabbix监控Windows主机
一,下载zabbix-agent 下载地址:http://www.zabbix.com/downloads/3.0.0/zabbix_agents_3.0.0.win.zip 已经下载好的文件 zab ...
- http 服务器编程 适配器
小结: 1.HandleFunc 只是一个适配器 go http 服务器编程(1) - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1 ...
- wiki配置文件
jira数据库配置文件(链接:https://blog.csdn.net/jiangguilong2000/article/details/39718407) /var/atlassian/appli ...
- [报错]Fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this
今天写了下面的快速枚举for循环代码,从按钮数组subButtons中取出button,然后修改button的样式,在添加到view中 for (UIButton *button in subButt ...
- MySQL数据库主从同步延迟分析及解决方案
一.MySQL的数据库主从复制原理 MySQL主从复制实际上基于二进制日志,原理可以用一张图来表示: 分为四步走: 1. 主库对所有DDL和DML产生的日志写进binlog: 2. 主库生成一个 lo ...
- C#-using用法详解
转自:http://blog.csdn.net/wanderocn/article/details/6659811 using 关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其 ...
- matrix---简单dp,边界边界-_-
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5569 简单dp,恶心的边界处理,无语: if((i+j)%2==1) dp[i][j]=a[i-1][ ...
- Linux升级python至3.4.4
wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz ls .tgz mkdir /usr/local/python3 cd Py ...
- Jacl 是 TCL 的一个备用实现
Jacl 是 TCL 的一个备用实现,它是完全使用 Java 代码编写的. wsadmin 工具使用 Jacl V1.3.2. 建议不要在 wsadmin 工具中使用 Jacl 语法 建议不要使用一个 ...
- NSUserDefaults保存对象数组报错
在使用NSUserDefaults的时候插入数据有时候会报以下错误:Attempt to set a non-property-list objec 这种错误的原因是插入了不识别的数据类型,NSUse ...