Codeforces 832 D Misha, Grisha and Underground
题意:Misha 和 Grisha 是2个很喜欢恶作剧的孩子, 每天早上 Misha 会从地铁站 s 通过最短的路到达地铁站 f, 并且在每个地铁站上都写上一句话, 然后Grisha 再从地铁站 t 通过最短的路到达地铁站 f, 并且记录下路途上被Misha写上字的地铁站数目,并且当天晚上会人会将地铁站清理干净,不会干扰第二天的计数, 现在给你3个地铁站 a, b, c, 现在求Misha记录的数目最大能是多少。
代码:求出Lca(a,b) Lca(a,c) Lca(b,c) 再找到深度最大的那个点, 我门可以通过画图发现 这个点是一个3叉路口, 从这个路口往每一地铁站走的路都是有效长度,最后找到有效长度就是答案了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
typedef pair<int,int> pll;
const int N = 1e5+;
struct Node{
int to;
int nt;
}e[N<<];
int head[N], anc[][N], deep[N];
int tot = ;
void add(int u, int v){
e[tot].to = v;
e[tot].nt = head[u];
head[u] = tot++;
}
void dfs(int u, int o){
deep[u] = deep[o] + ;
for(int i = head[u]; ~i; i = e[i].nt){
if(o != e[i].to){
anc[][e[i].to] = u;
for(int j = ; j < ; j++) anc[j][e[i].to] = anc[j-][anc[j-][e[i].to]];
dfs(e[i].to,u);
}
}
}
int lca(int u, int v){
if(deep[u] < deep[v]) swap(u, v);
for(int i = ; i >= ; i--) if(deep[v] <= deep[anc[i][u]]) u = anc[i][u];
if(u == v) return v;
for(int i = ; i >= ; i--) if(anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
return anc[][u];
}
int dis(int u, int v)
{
return deep[u]+deep[v]-*deep[lca(u,v)];
}
int main(){
int n, m, v;
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++){
scanf("%d", &v);
add(i,v);
add(v,i);
}
dfs(,);
int a, b, c;
for(int i = ; i <= m; i++){
scanf("%d%d%d",&a,&b,&c);
int t1 = lca(a, b), t2 = lca(b,c), t3 = lca(a,c);
//cout << t1 <<' ' << t2 << ' ' << t3 << endl;
if(deep[t1] < deep[t2]) t1 = t2;
if(deep[t1] < deep[t3]) t1 = t3;
int ans = max3(dis(t1,a), dis(t1,b), dis(t1,c));
printf("%d\n",ans+);
}
return ;
}
Codeforces 832 D Misha, Grisha and Underground的更多相关文章
- Codeforces 832D - Misha, Grisha and Underground
832D - Misha, Grisha and Underground 思路:lca,求两个最短路的公共长度.公共长度公式为(d(a,b)+d(b,c)-d(a,c))/2. 代码: #includ ...
- Codeforces Round #425 (Div. 2) Misha, Grisha and Underground(LCA)
Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforecs Round #425 D Misha, Grisha and Underground (倍增LCA)
D. Misha, Grisha and Underground time limit per test 2 seconds memory limit per test 256 megabytes i ...
- D. Misha, Grisha and Underground 树链剖分
D. Misha, Grisha and Underground 这个题目算一个树链剖分的裸题,但是这个时间复杂度注意优化. 这个题目可以选择树剖+线段树,时间复杂度有点高,比较这个本身就有n*log ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- Misha, Grisha and Underground CodeForces - 832D (倍增树上求LCA)
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground
[Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
我奇特的脑回路的做法就是 树链剖分 + 树状数组 树状数组是那种 区间修改,区间求和,还有回溯的 当我看到别人写的是lca,直接讨论时,感觉自己的智商收到了碾压... #include<cmat ...
随机推荐
- 【iOS】edgesForExtendedLayout
在 iOS 7.0 中,苹果引入了一个新的属性,叫做 edgesForExtendedLayou,它的默认值为 UIRectEdgeAll. 当你的容器是 navigationController 时 ...
- 云计算网络基础笔记及VLAN交换机配置
- Filebeat6.3文档—Log input配置
Filebeat6.3文档-Log input配置 paths 日志加载的路径.例如加载某一子目录级别下面路径的日志:/var/log/*/*.log.这表示会去加载以.log结尾的/var/log下 ...
- 设计模式:与SpringMVC底层息息相关的适配器模式
目录 前言 适配器模式 1.定义 2.UML类图 3.实战例子 4.总结 SpringMVC底层的适配器模式 参考 前言 适配器模式是最为普遍的设计模式之一,它不仅广泛应用于代码开发,在日常生活里也很 ...
- SQL Labs刷题补坑记录(less1-less30)
补坑加1,这几天快速刷一下sqllabs 来巩固下sql注入基础吧,也算是把很久以前没刷的过一遍,do it! 第一部分: LESS1: 直接报错,有回显的注入, http://localhost/s ...
- 基于python语言使用余弦相似性算法进行文本相似度分析
编写此脚本的目的: 本人从事软件测试工作,近两年发现项目成员总会提出一些内容相似的问题,导致开发抱怨.一开始想搜索一下是否有此类工具能支持查重的工作,但并没找到,因此写了这个工具.通过从纸上谈兵到着手 ...
- [原创实践]RedHat Enterprise Linux 5 安装GCC和redis
Redis的安装需要使用GCC,Red Hat Enterprise 5默认是不安装gcc的,需要自己手动安装. 1:查看系统中是否有gcc gcc -v 查看本机linux版本 lsb_releas ...
- dart的基本语法(一)
Hello world 安装dart的环境就不赘述了,无脑安装就可以了,安装过程中好像需要梯子(vpn),我装的时候失败好多次,我的梯子不能用了,准备不装了的时候,莫名其妙的装好了.迷の操作.惯例 ...
- 一文了解:Redis过期键删除策略
Redis过期键删除策略 Redis中所有的键都可以设置过期策略,就像是所有的键都可以上"生死簿",上了生死簿的键到时间后阎王就会叉掉这个键.同一时间大量的键过期,阎王就会忙不过来 ...
- Liunx之nginx代理
一.代理 正向代理 正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说: 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的 ...