CF911F Tree Destruction (树的直径,贪心)
题目链接
Solution
1.先找出树的直径.
2.遍历直径沿途的每一个节点以及它的子树.
3.然后对于每个非直径节点直接统计答案,令直径的两个端点为 \(x_1,x_2\) .
\]
最后再单独把直径拎出来,单独统计一次就好了.
正确性证明:
redbag 一句话解决:
如果说这其中的某个答案不是最优,那么找的直径不就是错的么。
"跑的飞快。"
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=200008;
struct sj{
int to,next;
}a[maxn*2];
int head[maxn],size,x,y,road[maxn];
int root,flag,n,cnt,far[5],kk,vis[maxn];
int dis[maxn][3],v[maxn],num;
long long ans;
int ans1[maxn],ans2[maxn],siz;
void add(int x,int y)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
}
void dfs(int x)
{
v[x]=1;
if(cnt>kk)
far[root==1?1:2]=x,kk=cnt;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
cnt++,dfs(tt);
}
v[x]=0; cnt--;
}
void fuck(int x)
{
v[x]=1;
if(x==far[2]){flag=1;}
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
fuck(tt);
if(flag){road[++num]=x;vis[x]=1;break;}
}
v[x]=0;
}
void getans(int x)
{
v[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt]&&!vis[tt])
getans(tt),ans+=max(dis[tt][1],dis[tt][2]);
}
if(dis[x][1]>dis[x][2])
ans1[++siz]=far[1];
else ans1[++siz]=far[2];
ans2[siz]=x;
v[x]=0;
}
void getdis(int x,int to)
{
v[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
{dis[tt][to]=dis[x][to]+1; getdis(tt,to);}
}
v[x]=0;
}
int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y),
add(x,y), add(y,x);
}
root=1; dfs(root);
root=far[1]; kk=0;
cnt=0; dfs(root);
fuck(root);
getdis(far[1],1); getdis(far[2],2);
for(int i=2;i<num;i++)
getans(road[i]),siz--;
for(int i=1;i<=kk;i++)ans+=i;
cout<<ans<<endl;
for(int i=1;i<=siz;i++)
cout<<ans1[i]<<' '<<ans2[i]<<' '<<ans2[i]<<endl;
for(int i=1;i<num;i++)
cout<<far[1]<<' '<<road[i]<<' '<<road[i]<<endl;
}
CF911F Tree Destruction (树的直径,贪心)的更多相关文章
- CF911F Tree Destruction 解题报告
CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)
题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- SP1437 Longest path in a tree(树的直径)
应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...
- [TJOI2017] 城市 (树的直径,贪心)
题目链接 Solution 这道题,调了我一晚上... 一直80分 >_<|| ... 考虑到几点: 分开任意一条边 \(u\) ,那么其肯定会断成两棵树. 肯定是分开直径上的边最优,否则 ...
- [CF911F]Tree Destruction
题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 首先我们知道,到一个点距离最远的点是直径的端点.考虑贪心,如果我 ...
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
随机推荐
- Problem O: 国家排序
Problem O: 国家排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 405 Solved: 253[Submit][Status][Web ...
- no pointer in java
Why there are no pointers in Java? In Java there are references instead of pointers. These reference ...
- SC || Git 相关知识
发现大佬的博客:https://www.cnblogs.com/onetwo/p/4157610.html 注:图片来自小姐姐的笔记~ ┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉┉∞ ∞┉┉┉┉∞ ...
- JS事件类型--1
滚轮事件其实就是一个mousewheel事件,这个事件跟踪鼠标滚轮,类似Mac的触屏版. 一.客户区坐标位置 鼠标事件都是在浏览器视口的特定位置上发生的.这个位置信息保存在事件对象的clientX和c ...
- php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分
我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...
- java,求1-100以内所有偶数的和。
package study01; public class Even { public static void main(String[] args) { int sum = 0; for (int ...
- Ubuntu Server 18.04 LTS安装
Please choose your preferred language. 选择您喜欢的语言 这里直接选择[English] Keyboard configuration 键盘配置 Please s ...
- 批量ping IP并检测IP延迟率和丢包率脚本
脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...
- PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序
PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...
- java web项目(spring项目)中集成webservice ,实现对外开放接口
什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...