Sponsors

Codeforces Round #411 (Div. 2), problem: (F) Expected diameter of a tree Solution In C/C++

#include <set>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200050;
vector<int>vec[N],dis[N],sum[N];
struct Node{int x,y;double ans;}JY;
bool operator<(Node a,Node b){if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}
set<Node>s;set<Node>::iterator it;
int n,m,q,first[N],nxt[N],v[N],w[N],tot,root,xx,yy,vis[N];
int maxx[N][2],rec[N][2],f[N],Root[N],vv[N],fi,flag,size[N];
void add(int x,int y){w[tot]=1,v[tot]=y,nxt[tot]=first[x],first[x]=tot++;}
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
void get_dia(int x,int fa){
rec[x][0]=rec[x][1]=x;
for(int i=first[x];~i;i=nxt[i])if(v[i]!=fa){
get_dia(v[i],x);
if(maxx[v[i]][0]+w[i]>maxx[x][0])
maxx[x][1]=maxx[x][0],rec[x][1]=rec[x][0],
maxx[x][0]=maxx[v[i]][0]+w[i],rec[x][0]=rec[v[i]][0];
else if(maxx[v[i]][0]+w[i]>maxx[x][1])
maxx[x][1]=maxx[v[i]][0]+w[i],rec[x][1]=rec[v[i]][0];
}if(maxx[x][0]+maxx[x][1]>maxx[root][0]+maxx[root][1])root=x;
}
void DFS(int x,int fa,int deep){
vv[x]=max(vv[x],deep);
if(flag)vec[fi].push_back(vv[x]),size[fi]++;
for(int i=first[x];~i;i=nxt[i])if(v[i]!=fa){
DFS(v[i],x,deep+w[i]);
}
}
int main(){
memset(first,-1,sizeof(first));
scanf(“%d%d%d”,&n,&m,&q);
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++)
scanf(“%d%d”,&xx,&yy),add(xx,yy),add(yy,xx),f[find(xx)]=find(yy);
for(int i=1;i<=n;i++){
fi=find(i);
if(!rec[fi][0]){
root=flag=0,get_dia(i,0),Root[fi]=root;
DFS(rec[root][0],0,0),flag=1,DFS(rec[root][1],0,0);
}
}
for(int i=1;i<=n;i++){
fi=find(i);
if(!vis[fi]){
vis[fi]=1;
dis[fi].resize(size[fi]+1),sum[fi].resize(size[fi]+1);
for(int j=0;j<vec[fi].size();j++)
dis[fi][vec[fi][j]]++,sum[fi][vec[fi][j]]+=vec[fi][j];
for(int j=size[fi]-1;j>=0;j–)
dis[fi][j]+=dis[fi][j+1],sum[fi][j]+=sum[fi][j+1];
}
}
while(q–){
scanf(“%d%d”,&xx,&yy);
int fx=find(xx),fy=find(yy);
if(fx==fy){puts(“-1”);continue;}
if(vec[fx].size()>vec[fy].size())swap(xx,yy),swap(fx,fy);
JY.x=fx,JY.y=fy;
if((it=s.find(JY))!=s.end()){printf(“%.10lf\n”,it->ans);continue;}
int dis1=maxx[Root[fx]][0]+maxx[Root[fx]][1];
int dis2=maxx[Root[fy]][0]+maxx[Root[fy]][1];
int Dis=max(dis1,dis2);
long long ans=0;
for(int i=0;i<vec[fx].size();i++){
ans+=sum[fy][Dis-vec[fx][i]]+1ll*dis[fy][Dis-vec[fx][i]]*(vec[fx][i]+1);
ans+=1ll*Dis*(size[fy]-dis[fy][Dis-vec[fx][i]]);
}
JY.ans=(double)ans/size[fx]/size[fy],s.insert(JY);
printf(“%.10lf\n”,(double)ans/size[fx]/size[fy]);
}
// for(int i=1;i<=n;i++)printf(“%d “,vv[i]);
}

Is A Battery-Powered Cooler...

Is a Battery-Powered Cooler Like the Anker SOLIX EverFrost...

One to watch: Macquarie...

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group,...

THE FREEPER CANTEEN –...

The Freeper Canteen: A Look That Up, It Must...

215% PENGU Rally Incoming?...

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency...

Junk is the new...

Junk is the New Punk: Why We're Falling Back...

Are weighted vests good...

Unlocking the Power of AI-Generated Content for SEO In today's...

Is A Battery-Powered Cooler Like The Anker SOLIX EverFrost 2 Worth It?

Is a Battery-Powered Cooler Like the Anker SOLIX EverFrost 2 Worth It? The summer heat is on, and keeping your drinks refreshingly cold can be...

One to watch: Macquarie tips double-digit returns for this ASX healthcare stock

Macquarie Predicts Double-Digit Returns for ASX Healthcare Stock Macquarie Group, a prominent Australian financial services company, has issued a bullish prediction for a specific ASX-listed...

THE FREEPER CANTEEN – The Look That Up, It Must Be On My Bucket List Quiz – Wednesday, August 27, 2025

The Freeper Canteen: A Look That Up, It Must Be On My Bucket List The Freeper Canteen, a unique online community, has recently launched a...

215% PENGU Rally Incoming? Analyst Says Token ‘Inches’ From Next Leg Up

215% PENGU Rally Incoming? Analyst Predicts Imminent Breakout The cryptocurrency market is constantly fluctuating, and predicting the next big move is a challenge. However, one...

Junk is the new punk: Why we’re falling back in love with retro tech

Junk is the New Punk: Why We're Falling Back in Love with Retro Tech In a world dominated by sleek, minimalist designs and cutting-edge technology,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content for SEO In today's digital landscape, search engine optimization (SEO) is paramount for online success. But creating high-quality,...

Are weighted vests good for bones and muscle? Fact-checking a fitness trend – NPR

Unlocking the Power of AI-Generated Content: A Boon or Bane for SEO? The world of search engine optimization (SEO) is constantly evolving, and the recent...

TSMC cuts Chinese tools from cutting-edge chip production to avoid US ire – Nikkei Asia

TSMC Ditches Chinese Tools for Cutting-Edge Chip Production Taiwan Semiconductor Manufacturing Company (TSMC), the world's leading chipmaker, is reportedly phasing out Chinese-made equipment from its...

Google Messages beta rolling out QR code key verification – 9to5Google

Google Messages Adds QR Code Key Verification for Enhanced Security Google is bolstering the security of its Messages app with a new feature rolling out...