답안 #699177

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699177 2023-02-15T23:35:32 Z EthanKim8683 공장들 (JOI14_factories) C++17
100 / 100
4438 ms 355828 KB
#ifndef ETHANKIM8683
#include"factories.h"
#endif
#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
using B=bool;
const I N=500000;
const Lli MAX=1e18;
vector<pair<I,I>>adjs[N];
I sizs[N];
B viss[N];
vector<pair<I,Lli>>rels[N];
Lli vals[N];
I tim=0;
I dfs1(I a,I p=-1){
  sizs[a]=1;
  for(auto[b,d]:adjs[a])if(b!=p&&!viss[b])sizs[a]+=dfs1(b,a);
  return sizs[a];
}
I dfs2(I a,I siz,I p=-1){
  for(auto[b,d]:adjs[a])if(b!=p&&!viss[b]&&sizs[b]*2>siz)return dfs2(b,siz,a);
  return a;
}
void dfs4(I a,I r,I p=-1,Lli l=0){
  rels[a].push_back({r,l});
  for(auto[b,d]:adjs[a])if(b!=p&&!viss[b])dfs4(b,r,a,l+d);
}
void dfs3(I a,I p=-1){
  a=dfs2(a,dfs1(a));
  viss[a]=1;
  dfs4(a,a);
  for(auto[b,d]:adjs[a])if(!viss[b])dfs3(b,a);
}
void Init(I n,I a_arr[],I b_arr[],I d_arr[]){
  for(I i=0;i<n-1;i++){
    I a=a_arr[i],b=b_arr[i],d=d_arr[i];
    adjs[a].push_back({b,d});
    adjs[b].push_back({a,d});
  }
  dfs3(0);
  fill_n(vals,n,MAX);
}
Lli Query(I s,I x_arr[],I t,I y_arr[]){
  for(I i=0;i<s;i++)for(auto[j,val]:rels[x_arr[i]])vals[j]=min(vals[j],val);
  Lli res=MAX;
  for(I i=0;i<t;i++)for(auto[j,val]:rels[y_arr[i]])res=min(res,val+vals[j]);
  for(I i=0;i<s;i++)for(auto[j,val]:rels[x_arr[i]])vals[j]=MAX;
  return res;
}
#ifdef ETHANKIM8683
I main(){
  cin.tie(0)->sync_with_stdio(0);
  I n,q;cin>>n>>q;
  I a_arr[n-1],b_arr[n-1],d_arr[n-1];
  for(I i=0;i<n-1;i++)cin>>a_arr[i]>>b_arr[i]>>d_arr[i];
  Init(n,a_arr,b_arr,d_arr);
  while(q--){
    I s,t;cin>>s>>t;
    I x_arr[s],y_arr[t];
    for(I i=0;i<s;i++)cin>>x_arr[i];
    for(I i=0;i<t;i++)cin>>y_arr[i];
    printf("%lli\n",Query(s,x_arr,t,y_arr));
  }
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 24148 KB Output is correct
2 Correct 272 ms 32924 KB Output is correct
3 Correct 285 ms 33288 KB Output is correct
4 Correct 290 ms 33260 KB Output is correct
5 Correct 309 ms 33632 KB Output is correct
6 Correct 208 ms 32360 KB Output is correct
7 Correct 286 ms 33352 KB Output is correct
8 Correct 297 ms 33372 KB Output is correct
9 Correct 307 ms 33640 KB Output is correct
10 Correct 208 ms 32304 KB Output is correct
11 Correct 288 ms 33216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24020 KB Output is correct
2 Correct 1999 ms 188772 KB Output is correct
3 Correct 2973 ms 276116 KB Output is correct
4 Correct 794 ms 104144 KB Output is correct
5 Correct 3726 ms 349588 KB Output is correct
6 Correct 3001 ms 277552 KB Output is correct
7 Correct 883 ms 81100 KB Output is correct
8 Correct 366 ms 59112 KB Output is correct
9 Correct 987 ms 94932 KB Output is correct
10 Correct 887 ms 82624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 24148 KB Output is correct
2 Correct 272 ms 32924 KB Output is correct
3 Correct 285 ms 33288 KB Output is correct
4 Correct 290 ms 33260 KB Output is correct
5 Correct 309 ms 33632 KB Output is correct
6 Correct 208 ms 32360 KB Output is correct
7 Correct 286 ms 33352 KB Output is correct
8 Correct 297 ms 33372 KB Output is correct
9 Correct 307 ms 33640 KB Output is correct
10 Correct 208 ms 32304 KB Output is correct
11 Correct 288 ms 33216 KB Output is correct
12 Correct 12 ms 24020 KB Output is correct
13 Correct 1999 ms 188772 KB Output is correct
14 Correct 2973 ms 276116 KB Output is correct
15 Correct 794 ms 104144 KB Output is correct
16 Correct 3726 ms 349588 KB Output is correct
17 Correct 3001 ms 277552 KB Output is correct
18 Correct 883 ms 81100 KB Output is correct
19 Correct 366 ms 59112 KB Output is correct
20 Correct 987 ms 94932 KB Output is correct
21 Correct 887 ms 82624 KB Output is correct
22 Correct 2434 ms 212640 KB Output is correct
23 Correct 2560 ms 217504 KB Output is correct
24 Correct 3606 ms 283756 KB Output is correct
25 Correct 3507 ms 287764 KB Output is correct
26 Correct 3417 ms 284444 KB Output is correct
27 Correct 4438 ms 355828 KB Output is correct
28 Correct 1070 ms 114672 KB Output is correct
29 Correct 3387 ms 283812 KB Output is correct
30 Correct 3416 ms 284232 KB Output is correct
31 Correct 3311 ms 283976 KB Output is correct
32 Correct 1156 ms 95220 KB Output is correct
33 Correct 429 ms 59584 KB Output is correct
34 Correct 749 ms 73744 KB Output is correct
35 Correct 740 ms 74592 KB Output is correct
36 Correct 908 ms 79372 KB Output is correct
37 Correct 974 ms 79464 KB Output is correct