Codeforces Round #425 (Div. 2), problem: (D) Misha, Grisha and Underground Solution In C/C++

#include<cstdio>
#include<iostream>
using namespace std;
struct Edge{
int to,next;
}e[202020];
int head[101010],cnt=0;
void add(int u,int v)
{
cnt++;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
int anc[20][101010];
int dep[101010];
void dfs(int x,int fa)
{
for(int i=head[x];i!=0;i=e[i].next)
{
int to=e[i].to;
if(to!=fa)
{
dep[to]=dep[x]+1;
anc[0][to]=x;
dfs(to,x);
}
}
}

int lca(int u,int v)
{
if(u==v)
return u;
if(dep[u]<dep[v])
swap(u,v);
for(int i=18;i>=0&&dep[u]!=dep[v];i–)
if(dep[anc[i][u]]>=dep[v])
u=anc[i][u];
if(u==v)
return u;
for(int i=18;i>=0;i–)
if(anc[i][u]!=anc[i][v])
{
u=anc[i][u];
v=anc[i][v];
}
return anc[0][u];
}
int dis(int u,int v)
{
int LCA=lca(u,v);
return dep[u]+dep[v]-dep[LCA]-dep[LCA];
}
int main()
{
int n,q;
scanf(“%d%d”,&n,&q);
for(int i=2;i<=n;i++)
{
int p;
scanf(“%d”,&p);
add(i,p);
add(p,i);
}
anc[0][1]=1;
dep[1]=1;
dfs(1,0);
for(int i=1;i<=18;i++)
for(int j=1;j<=n;j++)
anc[i][j]=anc[i-1][anc[i-1][j]];
while(q–)
{
int s,t,f;
scanf(“%d%d%d”,&s,&t,&f);
int mid=1;
int might;
might=lca(s,t);
if(dep[might]>dep[mid])
mid=might;
might=lca(s,f);
if(dep[might]>dep[mid])
mid=might;
might=lca(t,f);
if(dep[might]>dep[mid])
mid=might;
int ans=0;
ans=max(dis(mid,s),max(dis(mid,t),dis(mid,f)));
printf(“%d\n”,ans+1);
}
return 0;
}

Advertisement

spot_img

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Can baby deer walk...

Witnessing a newborn animal take its very first steps...

Is the Anker MagGo...

The Long-Term Durability Assessment: 6 Months as a Daily...

Anker MagGo 3-in-1 vs...

The Heavyweight Showdown: Anker MagGo vs. Belkin BoostCharge Pro For...

Best Protective Travel Pouches...

Protecting Your Premium Travel Power Investment The Anker MagGo 3-in-1...

How Fast Does Anker...

Putting Qi2 15W Speed Claims to the Test When wireless...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Can baby deer walk right away?

Witnessing a newborn animal take its very first steps is one of the most heartwarming sights in the natural world. In the wild, however,...

Is the Anker MagGo 3-in-1 Worth $100? Long-Term Durability Review

The Long-Term Durability Assessment: 6 Months as a Daily Driver Spending $100 on a smartphone charging accessory is a significant consumer investment. When budget 3-in-1...

Anker MagGo 3-in-1 vs Belkin BoostCharge Pro 3-in-1: Which Is Better?

The Heavyweight Showdown: Anker MagGo vs. Belkin BoostCharge Pro For the past four years, if an Apple user wanted the absolute best 3-in-1 wireless charging...

Best Protective Travel Pouches and Cases for Anker MagGo 3-in-1

Protecting Your Premium Travel Power Investment The Anker MagGo 3-in-1 Foldable Charging Station is engineered for life on the move. With its collapsible form factor,...

How Fast Does Anker MagGo Charge iPhone 15 and 16 from 0 to 100%?

Putting Qi2 15W Speed Claims to the Test When wireless charging standards make technological leaps, consumer skepticism is natural. For years, marketing promises of "fast...

Does the Anker MagGo 3-in-1 Come with a 40W USB-C Power Brick and Cable?

The Hidden Cost of Wireless Chargers: What’s Really in the Box? In modern consumer electronics packaging, a frustrating cost-cutting trend has become commonplace: the omitted...

Anker MagGo 3-in-1 StandBy Mode: Best Desk and Nightstand Setup Tips

Transforming Your iPhone into an Intelligent Bedside Dashboard With the release of iOS 17 and iOS 18, Apple introduced StandBy Mode, one of the most...

Can You Use the Anker MagGo 3-in-1 with Android and Samsung Qi2 Phones?

Breaking Down the Cross-Platform Wireless Charging Divide For over a decade, wireless charging in the Android ecosystem was synonymous with the traditional Qi standard. While...