Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 4;
const int logn = 19;
int F[30][maxn], dep[maxn];
inline int lca(int a,int b)
{
if(dep[a] > dep[b]) swap(a,b);
if(dep[a] != dep[b])
{
for(int i = logn;i >= 0;--i) if(dep[F[i][b]] >= dep[a]) b = F[i][b];
}
if(a == b) return a;
for(int i = logn; i >= 0; --i)
{
if(F[i][a] != F[i][b]) a = F[i][a], b = F[i][b];
}
return F[0][a];
}
int main()
{
// freopen("input.txt","r",stdin);
int n, point1 = 1, point2 = 1;
scanf("%d",&n);
dep[1] = 1;
for(int i = 2;i <= n; ++i)
{
int fa; scanf("%d",&fa);
F[0][i] = fa, dep[i] = dep[fa] + 1;
for(int j = 1;j <= logn; ++j)F[j][i] = F[j - 1][F[j-1][i]];
int ans1 = dep[point1] + dep[point2] - 2 * dep[lca(point1,point2)];
int ans2 = dep[point1] + dep[i] - 2 * dep[lca(point1, i)];
int ans3 = dep[point2] + dep[i] - 2 * dep[lca(point2, i)];
int maxv = max(ans1, max(ans2, ans3));
printf("%d\n",maxv);
if(ans1 == maxv) continue;
if(ans2 == maxv) point2 = i;
else if(ans3 == maxv) point1 = i;
}
return 0;
}
Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导的更多相关文章
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Brain Network (medium)
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...
- Brain Network (easy)
Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
随机推荐
- 1069. The Black Hole of Numbers
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- hdu 2089 数位dp入门题
#include<stdio.h> //dp[i][0]代表不存在不吉利数字 //dp[i][1]代表不存在不吉利数字但是以2开头 //dp[i][2]代表存在不吉利数字 #define ...
- 于工具类中@Autowired注入为NULL的问题记录
记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- spring-boot-starter-actuator监控接口详解
spring-boot-starter-actuator 是什么 一句话,actuator是监控系统健康情况的工具. - 怎么用? 1. 添加 POM依赖 <dependency> < ...
- Solr部分更新MultiValued的Date日期字段时报错及解决方案:Invalid Date String:'Mon Sep 14 01:48:38 CST 2015'
问题描述如标题. 异常信息如下: Result Caused by: org.apache.solr.common.SolrException: Invalid Date String:'Mon Se ...
- UDP bind() IP和prot
http://blog.csdn.net/qq_28171461/article/details/69523604
- js动态创建表格------Day59
刚刚不知道怎么回事,CSDN博客一直打不开,就在博客园完毕了今天的记录,结果临关机,登录了下.发现又好了,就再多花个几分钟转下吧,也无论到底在意的是什么了,权当强迫症了... 前几天记录了动态的加入一 ...
- QT官方下载地址
http://download.qt.io/official_releases watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/ ...
- setOutputFormat called in an invalid state: 1
在编写一个简单的录像应用程序的时候,爆出例如以下异常: E MediaRecorder: setOutputFormat called in an invalid state: 1 E Android ...
- maven+springMVC+mybatis+easyUI管理用户增删改查
1.项目演示图 2.项目简单介绍 项目分为两个projectdomain和manager.project结构例如以下图所看到的.当中domain是Maven javaproject主要完毕对数据库的操 ...