Submission #668387

# Submission time Handle Problem Language Result Execution time Memory
668387 2022-12-03T18:39:12 Z MilosMilutinovic Unique Cities (JOI19_ho_t5) C++14
0 / 100
229 ms 23224 KB
/**
 *    author:  wxhtzdy
 *    created: 03.12.2022 19:26:33
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, m;
  cin >> n >> m;
  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);
  }      
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  assert(*min_element(c.begin(), c.end()) == 1);
  assert(*max_element(c.begin(), c.end()) == 1);
  vector<int> dep(n);
  function<void(int, int)> Go = [&](int v, int pv) {
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      dep[u] = dep[v] + 1;
      Go(u, v);
    }
  };
  Go(0, 0);
  int x = max_element(dep.begin(), dep.end()) - dep.begin();
  dep[x] = 0;
  Go(x, x);
  int y = max_element(dep.begin(), dep.end()) - dep.begin();
  const int L = 25;
  vector<vector<int>> pr(n, vector<int>(L));
  function<void(int, int)> Dfs = [&](int v, int pv) {
    pr[v][0] = pv;
    for (int u : g[v]) {
      if (u == pv) {
        continue;  
      }
      dep[u] = dep[v] + 1;
      Dfs(u, v);
    }
  };
  Dfs(0, 0);
  for (int j = 1; j < L; j++) {
    for (int i = 0; i < n; i++) {
      pr[i][j] = pr[pr[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[pr[u][i]] >= dep[v]) {
        u = pr[u][i];
      }
    }
    if (u == v) {
      return u;
    }
    for (int i = L - 1; i >= 0; i--) {
      if (pr[u][i] != pr[v][i]) {
        u = pr[u][i];
        v = pr[v][i];
      }
    }
    return pr[u][0];
  };
  auto GetDist = [&](int u, int v) {
    return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
  };
  for (int i = 0; i < n; i++) {
    cout << (GetDist(i, x) != GetDist(i, y) ? 1 : 0) << '\n';   
  }                                      
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 724 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 229 ms 23224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 83 ms 20136 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 724 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -