답안 #854447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854447 2023-09-27T16:42:06 Z gun_gan 공장들 (JOI14_factories) C++17
0 / 100
2567 ms 307076 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
const int MX = 5e5 + 5;
 
int N, Q;
 
ll sz[MX], ans[MX];
bool removed[MX];
 
vector<pair<int,int>> g[MX];
 
int getSize(int v, int p) {
      sz[v] = 1;
      for(auto [u, w] : g[v]) {
            if(u == p || removed[u]) continue; 
            sz[v] += getSize(u, v);
      }
      return sz[v];
}
 
int getCentroid(int v, int p, int size) {
      for(auto [u, w] : g[v]) {
            if(u != p && sz[u] > size / 2 && !removed[u]) {
                  return getCentroid(u, v, size);
            }
      }
 
      return v;
}
 
vector<pair<ll,ll>> ancestors[MX];
 
void getDists(int v, int centroid, int p, int curDist) {
      for(auto [u, w] : g[v]) {
            if(u == p || removed[u]) continue;
            getDists(u, centroid, v, curDist + w); 
      }
 
      ancestors[v].push_back({centroid, curDist});
}
 
void build(int v) {
      int centroid = getCentroid(v, -1, getSize(v, -1));
 
      for(auto [u, w] : g[centroid]) {
            if(removed[u]) continue;
            getDists(u, centroid, centroid, w);
      }
 
      removed[centroid] = 1;
 
      for(auto [u, w] : g[centroid]) {
            if(removed[u]) continue;
            build(u);
      }
}
 
void Init(int _N, int A[], int B[], int D[]) {
      N = _N;
      for(int i = 0; i < N - 1; i++) {
            g[A[i]].push_back({B[i], D[i]});
            g[B[i]].push_back({A[i], D[i]});
      }
      for(int i = 0; i < N; i++) ans[i] = 1e18;
 
      build(0);
      for(int i = 0; i < N; i++) ancestors[i].push_back({i, 0});
}
 
long long Query(int s, int X[], int t, int Y[]) {
      vector<int> R;
      for(int i = 0; i < s; i++) {
            int x = X[i];
 
            for(auto [y, d] : ancestors[x]) {
                  R.push_back(y);
                  ans[y] = min(ans[y], d);
            }
      }
 
      ll res = 1e18;
      for(int i = 0; i < t; i++) {
            int x = Y[i];
 
            for(auto [y, d] : ancestors[x]) {
                  res = min(res, d + ans[y]);
            }
      }
 
      for(auto x : R) ans[x] = 1e18;
 
      return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45660 KB Output is correct
2 Correct 196 ms 60180 KB Output is correct
3 Incorrect 209 ms 60812 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 45660 KB Output is correct
2 Correct 1741 ms 225084 KB Output is correct
3 Incorrect 2567 ms 307076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 45660 KB Output is correct
2 Correct 196 ms 60180 KB Output is correct
3 Incorrect 209 ms 60812 KB Output isn't correct
4 Halted 0 ms 0 KB -