Submission #587374

# Submission time Handle Problem Language Result Execution time Memory
587374 2022-07-01T17:51:07 Z MilosMilutinovic Spring cleaning (CEOI20_cleaning) C++14
0 / 100
1000 ms 21232 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;
  const int MAX = 200000;
  vector<vector<int>> g(MAX);
  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);      
    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)> DfsSz = [&](int v, int pr) { 
      sz[v] = 1;
      for (int u : g[v]) {
        if (u != pr) {
          DfsSz(u, v);
          sz[v] += sz[u];
        }
      }                                            
    };
    DfsSz(0, 0);
    int root = -1;
    function<void(int, int)> DfsCen = [&](int v, int pr) {
      bool ok = true;
      for (int u : g[v]) {
        if (sz[u] * 2 > n + d) {
          ok = false;
        }
      }
      if (v < n && ok) {
        root = v;
        return;
      }
      for (int u : g[v]) {
        if (u != pr) {
          sz[v] -= sz[u];
          sz[u] += sz[v];
          DfsCen(u, v);
          if (root != -1) {
            return;
          }
          sz[u] -= sz[v];
          sz[v] += sz[u];
        }
      }
    };
    DfsCen(0, 0);
    assert(root != -1);
    const int L = 19;
    vector<vector<int>> jump(n + d, vector<int>(L));
    vector<int> dep(n + d);
    vector<int> ver;
    function<void(int, int)> 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 + d; 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 3 ms 4948 KB Output is correct
2 Execution timed out 1087 ms 8276 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 50 ms 16832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 17788 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1091 ms 9200 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1100 ms 15564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1093 ms 21232 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Execution timed out 1087 ms 8276 KB Time limit exceeded
3 Halted 0 ms 0 KB -