#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#include "factories.h"
set<pair<llo,llo>> adj[500001];
llo ss[500001];
llo par[500001];
llo rr=0;
llo mi[500001];
//llo ma[500001];
llo dist[22][500001];
//llo vis[500001];
llo lol[500001];
llo cur;
void dfs(llo no,llo parr=-1){
ss[no]=1;
for(auto j:adj[no]){
if(j.a==parr){
continue;
}
dfs(j.a,no);
ss[no]+=ss[j.a];
}
}
void dfs2(llo no,llo parr=-1,llo lev=0,llo cur2=0){
dist[cur2][no]=lev;
// cout<<cur2<<".."<<no<<".."<<lev<<endl;
for(auto j:adj[no]){
if(j.a==parr){
continue;
}
dfs2(j.a,no,lev+j.b,cur2);
}
}
llo find(llo no,llo par4=-1){
llo st=0;
// cout<<no<<":";
for(auto j:adj[no]){
if(j.a==par4){
continue;
}
if(ss[j.a]>ss[rr]/2){
st=1;
return find(j.a,no);
}
}
if(st==0){
return no;
}
}
void dec(llo no,llo parc=-1,llo pop=0){
rr=no;
cur=pop;
dfs(no);
llo cen=find(no);
lol[cen]=pop;
//llo cen=no;
dfs2(cen,-1,0,pop);
for(auto j:adj[cen]){
adj[j.a].erase({cen,j.b});
}
par[cen]=parc;
for(auto j:adj[cen]){
dec(j.a,cen,pop+1);
}
}
void Init(int nn,int aa[],int bb[],int dd[]){
llo n=(llo)nn;
// for(llo i=0;i<n;i++){
// vis[i]=0;
/* for(llo j=0;j<21;j++){
dist[j][i]=-1;
}*/
// }
for(llo i=0;i<n;i++){
// ma[i]=-1;
mi[i]=-1;
}
for(llo i=0;i<n-1;i++){
adj[aa[i]].insert({(llo)bb[i],(llo)dd[i]});
adj[bb[i]].insert({(llo)aa[i],(llo)dd[i]});
}
dec(0);
/*for(llo i=0;i<n;i++){
cout<<par[i]<<":"<<endl;
}*/
}
llo Query(int s,int x[],int t,int y[]){
llo ans=-1;
vector<llo> rem;
for(llo i=0;i<(llo)s;i++){
llo no;
no=(llo)x[i];
llo pc=0;
while(no!=-1){
if(mi[no]==-1){
mi[no]=dist[lol[x[i]]-pc][(llo)x[i]];
}
else{
mi[no]=min(mi[no],dist[lol[x[i]]-pc][(llo)x[i]]);
}
rem.pb(no);
no=par[no];
pc+=1;
}
}
for(llo i=0;i<(llo)t;i++){
llo no;
no=(llo)y[i];
llo pc=0;
while(no!=-1){
if(mi[no]!=-1 ){
if(ans==-1){
ans=mi[no]+dist[lol[y[i]]-pc][(llo)y[i]];
}
else{
ans=min(ans,mi[no]+dist[lol[y[i]]-pc][(llo)y[i]]);
}
}
no=par[no];
pc+=1;
}
}
for(auto j:rem){
// ma[j]=-1;
mi[j]=-1;
}
return ans;
}
/*int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,q;
cin>>n>>q;
int aa[n];
int bb[n];
int dd[n];
for(llo i=0;i<n-1;i++){
cin>>aa[i]>>bb[i]>>dd[i];
}
Init(n,aa,bb,dd);
for(int i=0;i<q;i++){
int s,t;
cin>>s>>t;
int ac[s];
int bc[t];
for(int j=0;j<s;j++){
cin>>ac[j];
}
for(int j=0;j<t;j++){
cin>>bc[j];
}
cout<<Query(s,ac,t,bc)<<'\n';
}
return 0;
}*/
Compilation message
factories.cpp: In function 'llo find(llo, llo)':
factories.cpp:54:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24320 KB |
Output is correct |
2 |
Correct |
453 ms |
33492 KB |
Output is correct |
3 |
Correct |
487 ms |
33272 KB |
Output is correct |
4 |
Correct |
507 ms |
34172 KB |
Output is correct |
5 |
Correct |
533 ms |
34068 KB |
Output is correct |
6 |
Correct |
348 ms |
33016 KB |
Output is correct |
7 |
Correct |
492 ms |
33272 KB |
Output is correct |
8 |
Correct |
491 ms |
33528 KB |
Output is correct |
9 |
Correct |
521 ms |
34008 KB |
Output is correct |
10 |
Correct |
342 ms |
32888 KB |
Output is correct |
11 |
Correct |
488 ms |
33460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
24192 KB |
Output is correct |
2 |
Correct |
2815 ms |
173940 KB |
Output is correct |
3 |
Correct |
3925 ms |
190596 KB |
Output is correct |
4 |
Correct |
1839 ms |
122864 KB |
Output is correct |
5 |
Correct |
5573 ms |
209968 KB |
Output is correct |
6 |
Correct |
4190 ms |
191920 KB |
Output is correct |
7 |
Correct |
1528 ms |
63944 KB |
Output is correct |
8 |
Correct |
722 ms |
52728 KB |
Output is correct |
9 |
Correct |
1771 ms |
67832 KB |
Output is correct |
10 |
Correct |
1615 ms |
65024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24320 KB |
Output is correct |
2 |
Correct |
453 ms |
33492 KB |
Output is correct |
3 |
Correct |
487 ms |
33272 KB |
Output is correct |
4 |
Correct |
507 ms |
34172 KB |
Output is correct |
5 |
Correct |
533 ms |
34068 KB |
Output is correct |
6 |
Correct |
348 ms |
33016 KB |
Output is correct |
7 |
Correct |
492 ms |
33272 KB |
Output is correct |
8 |
Correct |
491 ms |
33528 KB |
Output is correct |
9 |
Correct |
521 ms |
34008 KB |
Output is correct |
10 |
Correct |
342 ms |
32888 KB |
Output is correct |
11 |
Correct |
488 ms |
33460 KB |
Output is correct |
12 |
Correct |
20 ms |
24192 KB |
Output is correct |
13 |
Correct |
2815 ms |
173940 KB |
Output is correct |
14 |
Correct |
3925 ms |
190596 KB |
Output is correct |
15 |
Correct |
1839 ms |
122864 KB |
Output is correct |
16 |
Correct |
5573 ms |
209968 KB |
Output is correct |
17 |
Correct |
4190 ms |
191920 KB |
Output is correct |
18 |
Correct |
1528 ms |
63944 KB |
Output is correct |
19 |
Correct |
722 ms |
52728 KB |
Output is correct |
20 |
Correct |
1771 ms |
67832 KB |
Output is correct |
21 |
Correct |
1615 ms |
65024 KB |
Output is correct |
22 |
Correct |
3352 ms |
182272 KB |
Output is correct |
23 |
Correct |
3471 ms |
192688 KB |
Output is correct |
24 |
Correct |
5002 ms |
207320 KB |
Output is correct |
25 |
Correct |
5099 ms |
208440 KB |
Output is correct |
26 |
Correct |
5042 ms |
193452 KB |
Output is correct |
27 |
Correct |
6729 ms |
222324 KB |
Output is correct |
28 |
Correct |
2054 ms |
126800 KB |
Output is correct |
29 |
Correct |
4816 ms |
192680 KB |
Output is correct |
30 |
Correct |
4826 ms |
192504 KB |
Output is correct |
31 |
Correct |
4911 ms |
192740 KB |
Output is correct |
32 |
Correct |
2025 ms |
79996 KB |
Output is correct |
33 |
Correct |
691 ms |
53936 KB |
Output is correct |
34 |
Correct |
1131 ms |
59752 KB |
Output is correct |
35 |
Correct |
1111 ms |
59512 KB |
Output is correct |
36 |
Correct |
1553 ms |
62852 KB |
Output is correct |
37 |
Correct |
1444 ms |
62584 KB |
Output is correct |