typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include "factories.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
int n;
vector<pair<int,ll>>komsu[500000];
int dep[500000],par[500000][19],sub[500000];
ll dis[500000][19];
ll mn[500000];
ll Query(int S,int X[],int T,int Y[]){
ll ans=inf;
for(int j=0;j<S;j++){
int x=X[j];
for(int i=0;i<=dep[x];i++){
mn[par[x][i]]=min(mn[par[x][i]],dis[x][i]);
}
}
for(int j=0;j<T;j++){
int x=Y[j];
for(int i=0;i<=dep[x];i++){
ans=min(ans,mn[par[x][i]]+dis[x][i]);
}
}
for(int j=0;j<S;j++){
int x=X[j];
for(int i=0;i<=dep[x];i++){
mn[par[x][i]]=inf;
}
}
return ans;
}
void dfs(int pos,int root){
sub[pos]=1;
for(auto x:komsu[pos]){
if(x.fr==root)continue;
if(dep[x.fr]!=-1)continue;
dfs(x.fr,pos);
sub[pos]+=sub[x.fr];
}
}
void deco(int pos,int h){
dfs(pos,pos);
int total=sub[pos];
while(true){
int nex=-1;
for(auto x:komsu[pos]){
if(sub[x.fr]>sub[pos])continue;
if(dep[x.fr]!=-1)continue;
if(sub[x.fr]>total/2){
nex=x.fr;
break;
}
}
if(nex==-1)break;
pos=nex;
}
dep[pos]=h;
queue<int>q;
q.push(pos);
while(q.size()){
int loc=q.front();
q.pop();
par[loc][h]=pos;
for(auto x:komsu[loc]){
if(dep[x.fr]!=-1)continue;
if(dis[x.fr][h]!=0)continue;
dis[x.fr][h]=dis[loc][h]+x.sc;
q.push(x.fr);
}
}
for(auto x:komsu[pos]){
if(dep[x.fr]!=-1)continue;
deco(x.fr,h+1);
}
}
void Init(int N,int A[],int B[],int D[]){
n=N;
for(int i=0;i<n;i++){
mn[i]=inf;
}
for(int i=0;i<n-1;i++){
komsu[A[i]].pb({B[i],D[i]});
komsu[B[i]].pb({A[i],D[i]});
}
for(int i=0;i<n;i++){
dep[i]=-1;
}
deco(0,0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37456 KB |
Output is correct |
2 |
Correct |
182 ms |
49888 KB |
Output is correct |
3 |
Correct |
186 ms |
49992 KB |
Output is correct |
4 |
Correct |
187 ms |
49992 KB |
Output is correct |
5 |
Correct |
184 ms |
50264 KB |
Output is correct |
6 |
Correct |
152 ms |
49760 KB |
Output is correct |
7 |
Correct |
181 ms |
49992 KB |
Output is correct |
8 |
Correct |
188 ms |
49880 KB |
Output is correct |
9 |
Correct |
182 ms |
50412 KB |
Output is correct |
10 |
Correct |
137 ms |
49992 KB |
Output is correct |
11 |
Correct |
195 ms |
49884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37456 KB |
Output is correct |
2 |
Correct |
969 ms |
199372 KB |
Output is correct |
3 |
Correct |
1319 ms |
203464 KB |
Output is correct |
4 |
Correct |
431 ms |
198824 KB |
Output is correct |
5 |
Correct |
1884 ms |
233540 KB |
Output is correct |
6 |
Correct |
1499 ms |
204360 KB |
Output is correct |
7 |
Correct |
421 ms |
83020 KB |
Output is correct |
8 |
Correct |
221 ms |
82888 KB |
Output is correct |
9 |
Correct |
514 ms |
87052 KB |
Output is correct |
10 |
Correct |
397 ms |
83804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37456 KB |
Output is correct |
2 |
Correct |
182 ms |
49888 KB |
Output is correct |
3 |
Correct |
186 ms |
49992 KB |
Output is correct |
4 |
Correct |
187 ms |
49992 KB |
Output is correct |
5 |
Correct |
184 ms |
50264 KB |
Output is correct |
6 |
Correct |
152 ms |
49760 KB |
Output is correct |
7 |
Correct |
181 ms |
49992 KB |
Output is correct |
8 |
Correct |
188 ms |
49880 KB |
Output is correct |
9 |
Correct |
182 ms |
50412 KB |
Output is correct |
10 |
Correct |
137 ms |
49992 KB |
Output is correct |
11 |
Correct |
195 ms |
49884 KB |
Output is correct |
12 |
Correct |
7 ms |
37456 KB |
Output is correct |
13 |
Correct |
969 ms |
199372 KB |
Output is correct |
14 |
Correct |
1319 ms |
203464 KB |
Output is correct |
15 |
Correct |
431 ms |
198824 KB |
Output is correct |
16 |
Correct |
1884 ms |
233540 KB |
Output is correct |
17 |
Correct |
1499 ms |
204360 KB |
Output is correct |
18 |
Correct |
421 ms |
83020 KB |
Output is correct |
19 |
Correct |
221 ms |
82888 KB |
Output is correct |
20 |
Correct |
514 ms |
87052 KB |
Output is correct |
21 |
Correct |
397 ms |
83804 KB |
Output is correct |
22 |
Correct |
1077 ms |
204156 KB |
Output is correct |
23 |
Correct |
1166 ms |
205308 KB |
Output is correct |
24 |
Correct |
1524 ms |
208044 KB |
Output is correct |
25 |
Correct |
1586 ms |
210456 KB |
Output is correct |
26 |
Correct |
1598 ms |
208616 KB |
Output is correct |
27 |
Correct |
2064 ms |
232264 KB |
Output is correct |
28 |
Correct |
552 ms |
205228 KB |
Output is correct |
29 |
Correct |
1536 ms |
208100 KB |
Output is correct |
30 |
Correct |
1481 ms |
207432 KB |
Output is correct |
31 |
Correct |
1529 ms |
208228 KB |
Output is correct |
32 |
Correct |
434 ms |
87624 KB |
Output is correct |
33 |
Correct |
222 ms |
83128 KB |
Output is correct |
34 |
Correct |
333 ms |
81224 KB |
Output is correct |
35 |
Correct |
314 ms |
81388 KB |
Output is correct |
36 |
Correct |
359 ms |
82248 KB |
Output is correct |
37 |
Correct |
399 ms |
82248 KB |
Output is correct |