답안 #915264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915264 2024-01-23T15:15:36 Z uped 공장들 (JOI14_factories) C++14
100 / 100
5562 ms 374464 KB
#include "factories.h"
#include <bits/stdc++.h>

#define DEBUG(x) cout << #x << ": " << x << '\n';
using namespace std;
using ll = long long;


const int MAXN = 5e5;

vector<pair<int, ll>> adj[MAXN];
int sz[MAXN];
int parent[MAXN];
bool removed[MAXN];
vector<pair<ll, int>> dist[MAXN];

int get_sz(int n, int p = -1) {
  sz[n] = 1;
  for (auto& [x, _] : adj[n]) {
    if (removed[x] || x == p) continue;
    sz[n] += get_sz(x, n);
  }
  return sz[n];
}

int get_c(int d, int n, int p = -1) {
  for (auto& [x, _] : adj[n]) {
    if (removed[x] || x == p) continue;
    if (sz[x] > d) return get_c(d, x, n);
  }
  return n;
}

void dfs(int r, int n, ll s, int p = -1) {
  dist[n].emplace_back(s, r);
  for (auto& [x, w] : adj[n]) {
    if (removed[x] || x == p) continue;
    dfs(r, x, s + w, n);
  }
}

void decompose(int n = 0, int p = -1) {
  int c = get_c(get_sz(n) / 2, n);
  removed[c] = true;
  parent[c] = p;
  dist[c].emplace_back(0, c);
  for (auto& [x, w] : adj[c]) {
    if (removed[x]) continue;
    dfs(c, x, w, c);
  }

  for (auto& [x, _] : adj[c]) {
    if (removed[x]) continue;
    decompose(x, c); 
  }
}

const ll INF = 1e18;
ll best[MAXN];

void Init(int N, int A[], int B[], int D[]) {
  for (int i = 0; i < N - 1; ++i) {
    adj[A[i]].emplace_back(B[i], D[i]);
    adj[B[i]].emplace_back(A[i], D[i]);
  }
  for (int i =0; i < MAXN; ++i) {
    best[i] = INF;
  } 
  decompose();
}


long long Query(int S, int X[], int T, int Y[]) {
  for (int i = 0; i < S; ++i) {
    for (auto& [d, n] : dist[X[i]]) {
      best[n] = min(best[n], d);
    }
  }
  ll ans = LLONG_MAX;
  for (int i = 0; i < T; ++i) {
    
    for (auto& [d, n] : dist[Y[i]]) {
      if (best[n] == INF) continue;
      ans = min(ans, best[n] + d);
    }
  }
  for (int i = 0; i < S; ++i) {
    for (auto& [_, n] : dist[X[i]]) {
      best[n] = INF;
    }
  }

  return ans;
}

Compilation message

factories.cpp: In function 'int get_sz(int, int)':
factories.cpp:19:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   19 |   for (auto& [x, _] : adj[n]) {
      |              ^
factories.cpp: In function 'int get_c(int, int, int)':
factories.cpp:27:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   27 |   for (auto& [x, _] : adj[n]) {
      |              ^
factories.cpp: In function 'void dfs(int, int, ll, int)':
factories.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |   for (auto& [x, w] : adj[n]) {
      |              ^
factories.cpp: In function 'void decompose(int, int)':
factories.cpp:47:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   47 |   for (auto& [x, w] : adj[c]) {
      |              ^
factories.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |   for (auto& [x, _] : adj[c]) {
      |              ^
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:75:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |     for (auto& [d, n] : dist[X[i]]) {
      |                ^
factories.cpp:82:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |     for (auto& [d, n] : dist[Y[i]]) {
      |                ^
factories.cpp:88:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |     for (auto& [_, n] : dist[X[i]]) {
      |                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 50012 KB Output is correct
2 Correct 210 ms 64340 KB Output is correct
3 Correct 227 ms 64752 KB Output is correct
4 Correct 225 ms 64864 KB Output is correct
5 Correct 231 ms 65108 KB Output is correct
6 Correct 158 ms 63980 KB Output is correct
7 Correct 224 ms 64876 KB Output is correct
8 Correct 224 ms 64848 KB Output is correct
9 Correct 243 ms 65544 KB Output is correct
10 Correct 156 ms 63828 KB Output is correct
11 Correct 224 ms 64756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 49752 KB Output is correct
2 Correct 2259 ms 226736 KB Output is correct
3 Correct 3368 ms 297752 KB Output is correct
4 Correct 838 ms 120748 KB Output is correct
5 Correct 4262 ms 370140 KB Output is correct
6 Correct 3526 ms 298596 KB Output is correct
7 Correct 853 ms 101904 KB Output is correct
8 Correct 251 ms 78156 KB Output is correct
9 Correct 873 ms 115336 KB Output is correct
10 Correct 835 ms 102412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 50012 KB Output is correct
2 Correct 210 ms 64340 KB Output is correct
3 Correct 227 ms 64752 KB Output is correct
4 Correct 225 ms 64864 KB Output is correct
5 Correct 231 ms 65108 KB Output is correct
6 Correct 158 ms 63980 KB Output is correct
7 Correct 224 ms 64876 KB Output is correct
8 Correct 224 ms 64848 KB Output is correct
9 Correct 243 ms 65544 KB Output is correct
10 Correct 156 ms 63828 KB Output is correct
11 Correct 224 ms 64756 KB Output is correct
12 Correct 13 ms 49752 KB Output is correct
13 Correct 2259 ms 226736 KB Output is correct
14 Correct 3368 ms 297752 KB Output is correct
15 Correct 838 ms 120748 KB Output is correct
16 Correct 4262 ms 370140 KB Output is correct
17 Correct 3526 ms 298596 KB Output is correct
18 Correct 853 ms 101904 KB Output is correct
19 Correct 251 ms 78156 KB Output is correct
20 Correct 873 ms 115336 KB Output is correct
21 Correct 835 ms 102412 KB Output is correct
22 Correct 2847 ms 229892 KB Output is correct
23 Correct 2905 ms 233316 KB Output is correct
24 Correct 4280 ms 302436 KB Output is correct
25 Correct 4364 ms 304880 KB Output is correct
26 Correct 4204 ms 303608 KB Output is correct
27 Correct 5562 ms 374464 KB Output is correct
28 Correct 958 ms 127120 KB Output is correct
29 Correct 4000 ms 302976 KB Output is correct
30 Correct 3990 ms 303084 KB Output is correct
31 Correct 3848 ms 302940 KB Output is correct
32 Correct 1146 ms 115176 KB Output is correct
33 Correct 243 ms 78044 KB Output is correct
34 Correct 617 ms 95056 KB Output is correct
35 Correct 650 ms 95864 KB Output is correct
36 Correct 823 ms 100880 KB Output is correct
37 Correct 821 ms 100952 KB Output is correct