Submission #587369

# Submission time Handle Problem Language Result Execution time Memory
587369 2022-07-01T17:43:29 Z MilosMilutinovic Spring cleaning (CEOI20_cleaning) C++14
0 / 100
1000 ms 28804 KB
/**
 *    author:  wxhtzdy
 *    created: 01.07.2022 19:07:12
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, q;
  cin >> n >> q;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  while (q--) {
    int d;
    cin >> d;
    vector<int> a(d);      
    g.resize(n + d);
    for (int i = 0; i < d; i++) {
      cin >> a[i];
      --a[i];
      g[a[i]].push_back(n + i);
    }
    vector<int> sz(n + d);
    function<void(int, int)> Dfs = [&](int v, int pr) { 
      sz[v] = 1;
      for (int u : g[v]) {
        if (u != pr) {
          Dfs(u, v);
          sz[v] += sz[u];
        }
      }                                            
    };
    Dfs(0, 0);
    int root = -1;
    Dfs = [&](int v, int pr) {
      bool ok = true;
      for (int u : g[v]) {
        if (sz[u] > (n + d) / 2) {
          ok = false;
        }
      }
      if (v < n && ok) {
        root = v;
      }
      for (int u : g[v]) {
        if (u != pr) {
          sz[v] -= sz[u];
          sz[u] += sz[v];
          Dfs(u, v);
          sz[u] -= sz[v];
          sz[v] += sz[u];
        }
      }
    };
    Dfs(0, 0);
    assert(root != -1);
    const int L = 20;
    vector<vector<int>> jump(n + d, vector<int>(L));
    vector<int> dep(n);
    vector<int> ver;
    Dfs = [&](int v, int pr) {
      if ((int) g[v].size() <= 1) {
        ver.push_back(v);
      }
      jump[v][0] = pr;
      dep[v] = dep[pr] + 1;
      for (int u : g[v]) {
        if (u != pr) {
          Dfs(u, v);
        }
      }
    };
    Dfs(root, root);
    for (int j = 1; j < L; j++) {
      for (int i = 0; i < n; i++) {
        jump[i][j] = jump[jump[i][j - 1]][j - 1];
      }
    }
    auto LCA = [&](int u, int v) {
      if (dep[u] > dep[v]) {
        swap(u, v);
      }
      for (int i = L - 1; i >= 0; i--) {
        if (dep[jump[v][i]] >= dep[u]) {
          v = jump[v][i];
        }
      }            
      for (int i = L - 1; i >= 0; i--) {
        if (jump[u][i] != jump[v][i]) {
          u = jump[u][i];
          v = jump[v][i];
        }
      }
      return u == v ? v : jump[v][0];
    }; 
    auto Dist = [&](int u, int v) {
      return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
    };
    if ((int) ver.size() % 2 == 1) {
      cout << -1 << '\n';
    } else {
      int ans = 0;
      int sz = (int) ver.size();
      for (int i = 0; i < sz / 2; i++) {
        ans += Dist(ver[i], ver[sz / 2 + i]);
      }
      cout << ans << '\n';
    } 
    for (int i = 0; i < d; i++) {
      g[a[i]].pop_back();
    }
  }                                                                  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 46 ms 8280 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 39 ms 26944 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 41 ms 28804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 10228 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 13508 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1091 ms 20244 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 46 ms 8280 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -