Submission #854441

# Submission time Handle Problem Language Result Execution time Memory
854441 2023-09-27T16:32:25 Z gun_gan Factories (JOI14_factories) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MX = 2e5 + 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);
      }
}

int main() {
      cin.tie(0); ios_base::sync_with_stdio(0);

      for(int i = 0; i < MX; i++) ans[i] = 1e18;

      cin >> N >> Q;
      for(int i = 0; i < N - 1; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            g[u].push_back({v, w});
            g[v].push_back({u, w});
      }

      build(1);
      for(int i = 1; i <= N; i++) ancestors[i].push_back({i, 0});

      for(int i = 0; i < Q; i++) {
            int s, t;
            cin >> s >> t;

            vector<int> R;
            for(int i = 0; i < s; i++) {
                  int x;
                  cin >> x;

                  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;
                  cin >> x;

                  for(auto [y, d] : ancestors[x]) {
                        res = min(res, d + ans[y]);
                  }
            }

            cout << res << '\n';

            for(auto x : R) ans[x] = 1e18;
      }
}

Compilation message

/usr/bin/ld: /tmp/ccixU7sD.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccHlobgF.o:factories.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccixU7sD.o: in function `main':
grader.cpp:(.text.startup+0x37d): undefined reference to `Init(int, int*, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x412): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status