Codeforces题号:#379F

出处: Codeforces

主要算法:LCA+树的直径

难度:4.4

思路分析:

  给出q个操作,每次在一个节点上接上两个叶子。每一次询问树的直径。

  暴力做法:每一次操作暴力BFS两遍……然而……复杂度时\(O(Q * 2n\),爆到不知哪里去了。

  其实我们会发现,除非新加进来的两个点能够对直径产生影响,直径根本不会变。所以我们只需要考虑新加进来的点对原图的影响。

  那么如何操作呢?先假设没经过任何操作之前,直径的端点时2和3(事实上2,3,4中任意两个都可以),设直径的端点1为A,端点2为B。每加进来两个点x,y时,若dist(x,A)或dist(x,B)大于原先的直径,则用它们更新,并且将直径的另一个端点设置为x或y。

  下面来证明这样的做法的正确性:

  由于在讲树的直径的时候我们提到过,从任意一个点出发进行BFS,所能到达的最远点一定是树的一个直径之一。

  先假设新加进来的两个点x,y不存在,那么原先的树的直径就是A->B,并且一定是最长的了。设x,y的父亲节点为v。那么从v所能到达的最远的点一定是A或B。并且x或y到v的距离只有1,也只能是1。所以从x或y出发遍历所能够到达的最远的点一定也是A或B。而由于之前的直径是最长的,所以我当前的直径要比上一轮更长,只能是加了一,而这个1就是从x或y到v的距离的距离中产生的。

  所以我们选择x来更新(因为x和y其实是一样的,你不可能有一条直径是从x到y的,因为这样只能是2,而刚开始就已经是2了)。分别求出x到A与B的距离,如果x到A更新成功,则B=x;如果x到B更新成功,则A=x;事实上,它们只有一个能更新成功。

  于是现在问题就转化为了求两点之间距离了,LCA随便搞一搞就好了。这题还不用Dfs预处理,真是太可爱了……

代码注意点:

  由于有q次操作,每次操作增加两个点,所以点的数目是\(2q+4\),而不是\(q+4\)

Code

/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
vector <int> G[N];
int dep[N],f[N][];
int Q,v,cur_node,A,B,cur1,cur2,ans,Tmp_Dist;
inline void AddEdge(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
}
inline void Init(){
AddEdge(,), AddEdge(,), AddEdge(,);
cur_node = ;
A = , B = ;
ans = ;
f[][] = f[][] = f[][] = ;
dep[] = ;
dep[] = dep[] = dep[] = ;
}
inline void Update(int u, int v){
f[v][] = u;
dep[v] = dep[u] + ;
for(int i = ; (<<i) <= dep[v]; ++i){
f[v][i] = f[f[v][i-]][i-];
}
}
inline int LCA(int _a, int _b){
if(dep[_a] < dep[_b]){
swap(_a, _b);
}
int a = _a, b = _b;
for(int i = ; i >= ; --i){
if(dep[a] - (<<i) < dep[b]) continue;
a = f[a][i];
}
if(a == b) return a;
for(int i = ; i >= ; --i){
if(f[a][i] == f[b][i]) continue;
a = f[a][i];
b = f[b][i];
}
return f[a][];
}
inline int GetDist(int _a, int _b){
int __lca = LCA(_a, _b);
return dep[_a]-dep[__lca]+dep[_b]-dep[__lca];
}
int main(){
Init();
Q = r;
while(Q--){
v = r;
AddEdge(v,++cur_node);
Update(v,cur_node);
AddEdge(v,++cur_node);
Update(v,cur_node);
cur1 = cur_node;
Tmp_Dist = GetDist(cur1,A);
if(Tmp_Dist > ans){
ans = Tmp_Dist;
B = cur1;
}
Tmp_Dist = GetDist(cur1,B);
if(Tmp_Dist > ans){
ans = Tmp_Dist;
A = cur1;
}
printf("%d\n",ans);
}
/*
for(int i = 1; i <= cur_node; ++i){
for(int j = 0; j <= 3; ++j){
printf("f[%d][%d] = %d\n",i,j,f[i][j]);
}
}
for(int i = 1; i <= cur_node; ++i){
printf("dep[%d] = %d\n",i,dep[i]);
}
*/
return ;
}

Codeforces379 F. New Year Tree的更多相关文章

  1. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

  2. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  3. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  4. 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)

    Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...

  5. 2018HDU多校训练-3-Problem F. Grab The Tree

    Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...

  6. Problem F. Grab The Tree HDU - 6324

    题意:给出一棵n个节点的树,每个节点有一个权值,Q和T玩游戏,Q先选一些不相邻的节点,T选剩下的节点,每个人的分数是所选节点的权值的异或和,权值大的胜出,问胜出的是谁. 题解: 话说,这题后面的边跟解 ...

  7. AtCoder Grand Contest 023 F - 01 on Tree

    Description 题面 Solution HNOI-day2-t2 复制上去,删点东西,即可 \(AC\) #include<bits/stdc++.h> using namespa ...

  8. 泛函编程(8)-数据结构-Tree

    上节介绍了泛函数据结构List及相关的泛函编程函数设计使用,还附带了少许多态类型(Polymorphic Type)及变形(Type Variance)的介绍.有关Polymorphism的详细介绍会 ...

  9. codeforces 675D D. Tree Construction(线段树+BTS)

    题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...

随机推荐

  1. 【redis】windows 怎样关闭redis

    安装redis之后在命令行窗口中输入 redis-server redis.windows.conf 启动redis关闭命令行窗口就是关闭 redis.---redis作为windows服务启动方式r ...

  2. H5 32-百度首页

    32-百度首页 新 闻 网 页 贴 吧 知 道 音 乐 图 片 视 频 地 图 百科 文库 hao123 | 更多>> 百度地图带你吃喝玩乐,全心全意为人民服务 把百度设为主页 安装百度卫 ...

  3. codeforces#766 D. Mahmoud and a Dictionary (并查集)

    题意:给出n个单词,m条关系,q个询问,每个对应关系有,a和b是同义词,a和b是反义词,如果对应关系无法成立就输出no,并且忽视这个关系,如果可以成立则加入这个约束,并且输出yes.每次询问两个单词的 ...

  4. java开发中使用枚举表述数据字典

    一.用枚举表述数据字典 1.代码: package com.inspire.jdk.caculate; /** * Created by yaming * 用枚举表述常量数据字段 */ public ...

  5. #Leetcode# 985. Sum of Even Numbers After Queries

    https://leetcode.com/problems/sum-of-even-numbers-after-queries/ We have an array A of integers, and ...

  6. MYSQL: 1292 - Truncated incorrect DOUBLE value: '184B3C0A-C411-47F7-BE45-CE7C0818F420'

    MySQL Bugs: #63112: Truncated incorrect DOUBLE valuehttps://bugs.mysql.com/bug.php?id=63112 Error Co ...

  7. swagger 指定字段不显示到文档里

    Swagger UI 隐藏指定接口类或方法 - 宁静致远 - CSDN博客https://blog.csdn.net/lqh4188/article/details/53538201 swagger ...

  8. SQL性能优化-order by语句的优化

    原文:http://bbs.landingbj.com/t-0-243203-1.html 在某些情况中,MySQL可以使用一个索引来满足ORDER BY子句,而不需要额外的排序.where条件和or ...

  9. laravel门面和服务提供者使用

      关于laravel门面和服务提供者使用的一点见解,门面之词,不足之处,还请多多指教. 在laravel中,我们可能需要用到自己添加的类时,可以建立一个文件夹专门存放类文件,也可以使用laravel ...

  10. linux redis服务安装

    redis下载 官网地址:https://redis.io/download 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget htt ...