Submission #618079

# Submission time Handle Problem Language Result Execution time Memory
618079 2022-08-01T21:30:51 Z narvalo Inspection (POI11_ins) C++17
0 / 100
1360 ms 84572 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  for (int i = 0 ; i < n - 1 ; i += 1) {
    int a , b;
    cin >> a >> b;
    a -= 1 , b -= 1;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  vector<pair<int , int>> dp(n);
  function<pair<int , int>(int , int)> Dfs = [&](int node , int pre) {
    int ans = 1;
    for (auto x : adj[node]) {
      if (x == pre) continue;
      ans += Dfs(x , node).first;
    }
    return dp[node] = make_pair(ans , pre);
  };
  Dfs(0 , -1);
  long long ans = 0;
  int max_depth = 0;
  function<void(int , int , int)> Get = [&](int node , int pre , int depth) {
    max_depth = max(depth , max_depth);
    ans += 1LL * depth * 2;
    for (auto x : adj[node]) {
      if (x == pre) continue;
      Get(x , node , depth + 1);
    }
    return ;
  };
  auto Init = [&]() {
    max_depth = 0;
    ans = 0;
    return ;
  };
  vector<int> sum(n);
  for (int i = 0 ; i < n ; i++) {
    for (auto x : adj[i]) {
      if (x == dp[i].second) continue;
      sum[i] += dp[x].first;
    }
  }
  for (int node = 0 ; node < n ; node += 1) {
    vector<int> comps;
    for (auto x : adj[node]) {
      int sz = (x == dp[node].second ? n - 1 - sum[node] : dp[x].first);
      comps.emplace_back(sz);
    }
    int mx = *max_element(comps.begin() , comps.end());
    if (mx > n - mx) {
      cout << -1 << '\n';
      continue;
    }
    else {
      Get(node , -1 , 0);
      cout << ans - max_depth << '\n';      
      Init(); 
    }
  }
  return 0; 
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1876 KB Output is correct
2 Incorrect 10 ms 2364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 3532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 8464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 612 ms 42384 KB Output is correct
2 Incorrect 532 ms 53920 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1291 ms 84440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1296 ms 84404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1360 ms 84572 KB Output isn't correct
2 Halted 0 ms 0 KB -