#include "factories.h"
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N=500005;
long long dist[N],hi[N];
int dp[N][20];
set <pair<int,int> > adj[N];
set< pair<long long,int> > ss[N];
int par[N],sz[N],del[N];
long long dist2[N][20];
int nc;
void dfs0(int node,int pa=0)
{
dp[node][0]=pa;
for(int i=1;i<20;i++)
dp[node][i]=dp[dp[node][i-1]][i-1];
for(auto it=adj[node].begin();it!=adj[node].end();it++)
{
int x=(*it).F,l=(*it).S;
if(x==pa) continue;
dist[x]=dist[node]+l;
hi[x]=hi[node]+1;
dfs0(x,node);
}
}
void dfs1(int node,int pa=0)
{
sz[node]=1;
nc++;
for(auto it=adj[node].begin();it!=adj[node].end();it++)
{
int x=(*it).F,l=(*it).S;
if(x==pa) continue;
if(del[x]) continue;
dfs1(x,node);
sz[node]+=sz[x];
}
}
int getCentroid(int node,int pa=0)
{
for(auto it=adj[node].begin();it!=adj[node].end();it++)
{
int x=(*it).F,l=(*it).S;
if(x==pa) continue;
if(del[x]) continue;
if(sz[x]>nc/2)
return getCentroid(x,node);
}
return node;
}
void decompose(int node,int pa=-1)
{
nc=0;
dfs1(node,node);
int centroid=getCentroid(node,node);
if(pa==-1) pa=centroid;
par[centroid]=pa;
del[centroid]=1;
for(auto it=adj[node].begin();it!=adj[node].end();it++)
{
int x=(*it).F;
adj[x].erase({centroid,(*it).S});
decompose(x,centroid);
}
}
int lca(int x,int y)
{
if(hi[x]<hi[y]) swap(x,y);
for(int i=19;i>=0;i--)
{
if(hi[x]-(1<<i)>=hi[y])
x=dp[x][i];
}
if(x==y) return x;
for(int i=19;i>=0;i--)
{
if(dp[x][i]!=dp[y][i])
{
x=dp[x][i]; y=dp[y][i];
}
}
return dp[x][0];
}
long long getDist(int x,int y)
{
return dist[x]+dist[y]-2*dist[lca(x,y)];
}
void add(int x)
{
int lev=0;
int z=x;
while(true)
{
dist2[x][lev]=(dist2[x][lev]==-1?getDist(z,x):dist2[x][lev]);
ss[z].insert({dist2[x][lev],x});
lev++;
assert(lev<=20);
if(par[z]==z) break;
z=par[z];
}
}
void delet(int x)
{
int z=x;
int lev=0;
while(true)
{
ss[z].clear();
lev++;
assert(lev<=20);
if(par[z]==z) break;
z=par[z];
}
}
long long query(int x)
{
long long ans=(1LL<<60);
int z=x;
int lev=0;
while(true)
{
if(ss[z].size()){
pair<long long,int> pp=(*ss[z].begin());
dist2[x][lev]=(dist2[x][lev]==-1?getDist(z,x):dist2[x][lev]);
ans=min(ans,dist2[x][lev]+pp.F);
}
lev++;
assert(lev<=20);
if(par[z]==z) break;
z=par[z];
}
return ans;
}
void Init(int N, int A[], int B[], int D[]) {
for(int i=0;i<N-1;i++)
{
A[i]++; B[i]++;
adj[A[i]].insert({B[i],D[i]});
adj[B[i]].insert({A[i],D[i]});
}
//cerr << "DONE" << endl;
dfs0(1);
//cerr << "DONE" << endl;
decompose(1);
memset(dist2,-1,sizeof dist2);
//cerr << "DONE" << endl;
}
long long Query(int S, int X[], int T, int Y[]) {
long long ans=(1LL<<60);
for(int i=0;i<S;i++) X[i]++;
for(int i=0;i<T;i++) Y[i]++;
for(int i=0;i<S;i++) add(X[i]);
for(int i=0;i<T;i++) ans=min(ans,query(Y[i]));
for(int i=0;i<S;i++) delet(X[i]);
return ans;
}
Compilation message
factories.cpp: In function 'void dfs1(int, int)':
factories.cpp:40:17: warning: unused variable 'l' [-Wunused-variable]
int x=(*it).F,l=(*it).S;
^
factories.cpp: In function 'int getCentroid(int, int)':
factories.cpp:52:17: warning: unused variable 'l' [-Wunused-variable]
int x=(*it).F,l=(*it).S;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8150 ms |
525312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8132 ms |
525312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8150 ms |
525312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |