이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const ll inf=7e18;
const int N=1000050;
const int L=21;
int n;
vector<pair<int,int>> E[N],B[N];
void AddEdge(int u, int v, int w){ B[u].pb({v,w});B[v].pb({u,w});}
void Build(int u, int p)
{
vector<pair<int,int>> es;
for(auto e:E[u]) if(e.first!=p) es.pb(e);
int last=u;
for(int i=0;i<es.size();i++)
{
AddEdge(last,es[i].first,es[i].second);
if(i+2<es.size())
{
n++;
AddEdge(last,n,0);
last=n;
}
}
for(auto e:es) Build(e.first,u);
}
int go[N][L];
ll dep[N][L];
bool was[N];
void DFS(int u, int p, int l, int g, ll d)
{
go[u][l]=g;
dep[u][l]=d;
for(auto e:B[u]) if(e.first!=p && !was[e.first]) DFS(e.first,u,l,g,d+e.second);
}
int sz[N];
void DFSSZ(int u, int p){ sz[u]=1;for(auto e:B[u]) if(e.first!=p && !was[e.first]) DFSSZ(e.first,u),sz[u]+=sz[e.first];}
int Find(int u, int p, int n){ for(auto e:B[u]) if(e.first!=p && !was[e.first] && sz[e.first]*2>n) return Find(e.first,u,n);return u;}
int Find(int u){ DFSSZ(u,u);return Find(u,u,sz[u]);}
int cen_dep[N];
void Decompose(int u, int l)
{
was[u=Find(u)]=1;
cen_dep[u]=l;
go[u][l]=3;
for(int i=0;i<B[u].size();i++) if(!was[B[u][i].first])
{
DFS(B[u][i].first,u,l,i,B[u][i].second);
}
for(auto e:B[u]) if(!was[e.first]) Decompose(e.first,l+1);
}
void Init(int N, int A[], int B[], int D[])
{
n=N;
for(int i=0;i<N-1;i++) A[i]++,B[i]++,E[A[i]].pb({B[i],D[i]}),E[B[i]].pb({A[i],D[i]});
Build(1,0);
Decompose(1,0);
}
void ckmn(ll &a, ll b){ a=min(a,b);}
int nxt[N][3],root,tsz;
ll mn[N][4];
void Clear(){ tsz=root=0;}
void cl(int c){ nxt[c][0]=nxt[c][1]=nxt[c][2]=0;mn[c][0]=mn[c][1]=mn[c][2]=mn[c][3]=inf;}
void Set(int &c, int u, int l=0)
{
if(!c) c=++tsz,cl(c);
ckmn(mn[c][go[u][l]],dep[u][l]);
if(cen_dep[u]==l) return;
Set(nxt[c][go[u][l]],u,l+1);
}
ll Get(int c, int u, int l=0)
{
if(!c) return inf;
ll ans=inf;
for(int i=0;i<4;i++) if(i!=go[u][l]) ckmn(ans,dep[u][l]+mn[c][i]);
if(cen_dep[u]!=l) ckmn(ans,Get(nxt[c][go[u][l]],u,l+1));
return ans;
}
ll Query(int S, int X[], int T, int Y[])
{
Clear();
for(int i=0;i<S;i++) Set(root,X[i]+1);
ll ans=inf;
for(int i=0;i<T;i++) ans=min(ans,Get(root,Y[i]+1));
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
factories.cpp: In function 'void Build(int, int)':
factories.cpp:17:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<es.size();i++)
~^~~~~~~~~~
factories.cpp:20:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i+2<es.size())
~~~^~~~~~~~~~
factories.cpp: In function 'void Decompose(int, int)':
factories.cpp:48:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<B[u].size();i++) if(!was[B[u][i].first])
~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |