codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接:
2 seconds
256 megabytes
standard input
standard output
Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consumes a brain, a new brain appears in its nervous system and gets immediately connected to one of the already existing brains using a single brain connector. Researchers are now interested in monitoring the brain latency of a zombie. Your task is to write a program which, given a history of evolution of a zombie's nervous system, computes its brain latency at every stage.
The first line of the input contains one number n – the number of brains in the final nervous system (2 ≤ n ≤ 200000). In the second line a history of zombie's nervous system evolution is given. For convenience, we number all the brains by 1, 2, ..., n in the same order as they appear in the nervous system (the zombie is born with a single brain, number 1, and subsequently brains 2, 3, ..., n are added). The second line contains n - 1 space-separated numbers p2, p3, ..., pn, meaning that after a new brain k is added to the system, it gets connected to a parent-brain .
Output n - 1 space-separated numbers – the brain latencies after the brain number k is added, for k = 2, 3, ..., n.
- 6
1
2
2
1
5
- 1 2 2 3 4
- 题意:
- 给一棵树的生成过程,问在每次添加一个节点后这棵树的直径是多少;
- 思路:
- 在新加的一个节点w前的直径是(u,v),加入w后,直径就变成了max{(u,v)(u,w)(v,w)};
然后就是把lca约束成RMQ问题来了;- AC代码:
- #include <bits/stdc++.h>
- /*
- #include <vector>
- #include <iostream>
- #include <queue>
- #include <cmath>
- #include <map>
- #include <cstring>
- #include <algorithm>
- #include <cstdio>
- */
- using namespace std;
- #define For(i,j,n) for(int i=j;i<=n;i++)
- #define mst(ss,b) memset(ss,b,sizeof(ss));
- typedef long long LL;
- template<class T> void read(T&num) {
- char CH; bool F=false;
- for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
- for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
- F && (num=-num);
- }
- int stk[], tp;
- template<class T> inline void print(T p) {
- if(!p) { puts(""); return; }
- while(p) stk[++ tp] = p%, p/=;
- while(tp) putchar(stk[tp--] + '');
- putchar('\n');
- }
- const LL mod=1e9+;
- const double PI=acos(-1.0);
- const LL inf=1e18;
- const int N=2e5+;
- const int maxn=;
- const double eps=1e-;
- int n,in[N],a[*N],dep[N],cnt=,dp[*N][],dis[N];
- vector<int>ve[N];
- void dfs(int x,int deep)
- {
- //cout<<x<<" "<<deep<<endl;
- in[x]=cnt;
- a[cnt++]=x;
- dep[x]=deep;
- int len=ve[x].size();
- For(i,,len-)
- {
- int y=ve[x][i];
- dis[y]=dis[x]+;
- dfs(y,deep+);
- a[cnt++]=x;
- }
- }
- int RMQ()
- {
- for(int i=;i<cnt;i++)
- dp[i][]=a[i];
- for(int j=;(<<j)<=cnt;j++)
- {
- for(int i=;i+(<<j)-<cnt;i++)
- {
- if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])dp[i][j]=dp[i][j-];
- else dp[i][j]=dp[i+(<<(j-))][j-];
- }
- }
- }
- int query(int l ,int r)
- {
- if(l>r)swap(l,r);
- int temp=(int)(log((r-l+)*1.0)/log(2.0));
- if(dep[dp[l][temp]]<dep[dp[r-(<<temp)+][temp]])return dp[l][temp];
- return dp[r-(<<temp)+][temp];
- }
- int s=,e=,pre=;
- int check(int x,int y)
- {
- //cout<<x<<" "<<y<<" "<<pre<<" @@@@"<<endl;
- int temp=query(in[x],in[y]);
- if(dis[x]+dis[y]-*dis[temp]>pre)
- {
- s=x;
- e=y;
- pre=dis[x]+dis[y]-*dis[temp];
- }
- }
- int main()
- {
- read(n);
- int u;
- For(i,,n)
- {
- read(u);
- ve[u].push_back(i);
- }
- dis[]=;
- dfs(,);
- RMQ();
- For(i,,n)
- {
- int fs=s,fe=e;
- check(fs,fe);
- check(fs,i);
- check(fe,i);
- cout<<pre<<" ";
- }
- return ;
- }
codeforces 690C3 C3. Brain Network (hard)(lca)的更多相关文章
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- 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 ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- 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 ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
随机推荐
- android studio AndroidManifest
一.目录结构 1. AndroidManifest.xml 它是一个清单文件,提供应用的基本信息 <?xml version="1.0" encoding="utf ...
- DBCP,C3P0与Tomcat jdbc pool 连接池的比较
hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法reconnect,告诉连接被重置 ...
- Volley 源码解析 StringRequest解析
Android Vollety是一个很有用的框架,所以想借鉴前人思想,分析这个源代码. 参考: http://blog.csdn.net/crazy__chen/article/details/464 ...
- 利用BURPSUITE检测CSRF漏洞
CSRF漏洞的手动判定:修改referer头或直接删除referer头,看在提交表单时,网站是否还是正常响应. 下面演示用Burpsuite对CSRF进行鉴定. 抓包. 成功修改密码完成漏洞的利用.
- Spring实战Day7面向切面编程术语介绍
#### 面向切面编程 为什么需要切面? 有些功能需要在应用中的多个地方使用到,但是我们又不想在着每个地方都调用他们 切面术语 通知(advice):切面需要完成的工作 通知的类型(什么时间完成工作) ...
- 最近遇到的C++数字和字符串的转换问题
1. 用itoa 和atoi 在头文件#include<cstidlib> itoa用法: char * itoa ( int value, char * str, int base ) ...
- SolidEdge 如何由装配图快速进行标注和零件序号编写 制作BOM表
点击"零件明细表",然后点击要生成序号的视图,然后点击前面两项(自动标号和放置清单),点击完成后效果如下图所示. 在点击完成之前,先点击他前面的一个按钮,取消勾选"项 ...
- 微信小程序 - 提取字体图标与其优化
微信小程序,无论是字体图标还是图标,都差不多,只不过是为了以后字体图标修改方便,或者加效果方便而使用它而已! 1. 下载font-awesome http://fontawesome.dashgame ...
- 在java中String类为什么要设计成final?
大神链接:在java中String类为什么要设计成final? - 程序员 - 知乎 我进行了重新排版,并且更换了其中的一个例子,让我们更好理解. String很多实用的特性,比如说“不可变性”,是工 ...
- asp.net mvc 性能优化——(1)静态化
asp.net mvc 性能优化--(1)静态化 在改善页面性能的同时,可能会采用静态化的策略,对于不能实时静态化的内容,则采用缓存.本文主要讨论如何实现cshtml的静态化(实际上还不是完全的htm ...