Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

题意:给出 一颗树,然后q个询问,每个询问给出3个点,让我们自己选择一个起点一个终点,这条路线上的点标记,问第三个点到终点的最多标记点是多少

思路:第三个点到终点的标记点,肯定是第三个点与起点的最近公共祖先到终点的标记点。我们可以求出三个点的的三种最近公共祖先,取深度最大的点K,然后求K到三个点的距离+1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4
 5 const int DEG=17;
 6 vector<int> e[N];
 7 int dep[N];
 8 int fa[N][DEG];
 9
10 void dfs(int u){
11     for(int i=1;i<DEG;i++){
12         fa[u][i]=fa[fa[u][i-1]][i-1];
13     }
14     for(int i=0;i<e[u].size();i++){
15         int v=e[u][i];
16         dep[v]=dep[u]+1;
17         fa[v][0]=u;
18         dfs(v);
19     }
20 }
21 int lca(int u,int v){
22     if(dep[u]<dep[v]) swap(u,v);
23     for(int i=0,d=dep[u]-dep[v];d;i++,d>>=1){
24         if(d&1) u=fa[u][i];
25     }
26     if(u==v) return v;
27     for(int i=DEG-1;i>=0;i--){
28         if(fa[u][i]!=fa[v][i]){
29             u=fa[u][i];v=fa[v][i];
30         }
31     }
32     return fa[u][0];
33 }
34 int hh(int u,int v){
35     return dep[u]+dep[v]-2*dep[lca(u,v)];
36 }
37 int main(){
38     int n,q;
39     int x;
40     scanf("%d%d",&n,&q);
41     for(int i=2;i<=n;i++){
42         scanf("%d",&x);
43         e[x].push_back(i);
44     }
45     dep[1]=1;
46     dfs(1);
47     int k[4];
48     while(q--){
49         for(int i=1;i<=3;i++) scanf("%d",&k[i]);
50         int x1=lca(k[1],k[2]),x2=lca(k[1],k[3]),x3=lca(k[2],k[3]);
51         int x;
52         if(dep[x1]>dep[x2]) x=x1;else x=x2;
53         if(dep[x]<dep[x3]) x=x3;
54         int y1=hh(x,k[1]),y2=hh(x,k[2]),y3=hh(x,k[3]);
55
56         cout<<max(max(y1,y2),y3)+1<<endl;
57     }
58     return 0;
59 }
时间: 2024-11-01 15:54:41

Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground的相关文章

【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树--但是会TLE. LCT一样无脑,但是少一个log,可以过. 正解是分类讨论, 如果t不在lca(s,f)的子树内,答案是dis(lca(s,f),f). 如果t在lca(s,f)的子树内,并且dep(lca(s,t))>dep(lca(f,t)),答案是dis(lca(s,t),f): 否则答案是dis(lca(f,t),f). #in

字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

题目传送门 1 /* 2 题意:给出一系列名字变化,问最后初始的名字变成了什么 3 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 4 在每一次更新时都把初始pos加上去,那么就保证更新了初始的名字,这也是唯一要思考的地方了:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <algorithm> 1

图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

题目传送门 1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorith

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 connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #425 (Div. 2) B. Petya and Exam(暴力大法好)

题目链接:http://codeforces.com/problemset/problem/832/B 题意:给定一些小写字符(定义为好字符,除这些好字符外的其他小写字符都是坏字符).和字符串S,S里面除了小写字母以外还有?字符,?可以用任何的好字符替代:*字符,*只可以用坏字符串和 空替代,然后给出n个字符串去匹配S字符,问是否能成功. 题解:QAQ,这道题写的我头发又掉了好多. 设拿来匹配S的字符串为T 字符串要匹配,长度要相同吧.*既然这么强大,可以空,那么T的长度最多比S的长度少1:然后

Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules

Codeforces Round #425 (Div.2)

A.Sasha and Sticks 题意:有两个人,每次轮流从n个棒子中抽出k个,直到剩余不足k个棒子.Sasha先抽.如果Sasha抽取的多,则输出YES,否则输出NO. 思路:n/k为奇数时,Sasha获胜. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 typedef long long LL; 5 LL n, k; 6 int main() 7 { 8 while (~scanf(&quo

Codeforces Round #285 (Div. 2) C - Misha and Forest

思路:类似一个拓扑排序的题,根据 对度数为1的点,它所连的边的编号即为异或和这一 规律,直接进行拓扑排序即可. 每次对得到的节点度数减一,并且异或上他所连的节点. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<algorithm> #include<queue> using namespace std; typed