Submission #618098

# Submission time Handle Problem Language Result Execution time Memory
618098 2022-08-01T22:24:29 Z narvalo Inspection (POI11_ins) C++17
100 / 100
1303 ms 131072 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0 << '\n';
    return 0;
  }
  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 , int , bool)> Get = [&](int node , int pre , int depth , int sub , bool now) {
    if (now) {
      max_depth = max(depth , max_depth);
    }
    ans += 1LL * depth * 2;
    for (auto x : adj[node]) {
      if (x == pre) continue;
      bool c1 = (x == sub || sub == -1);
      Get(x , node , depth + 1 , sub , now | c1);
    }
    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<pair<int , int>> comps;
    int id = 0 , cnt = 0;
    for (auto x : adj[node]) {
      int sz = (x == dp[node].second ? n - 1 - sum[node] : dp[x].first);
      comps.emplace_back(sz , x);
      if (comps[id].first <= sz) {
        id = cnt;
      }
      cnt += 1;
    }
    int mx = comps[id].first;
    int sub = comps[id].second;
    if (mx > n - mx) {
      cout << -1 << '\n';
      continue;     
    }
    else {
      if (n - 2 * mx == 0) {
        Get(node , -1 , 0 , sub , 0);        
      }
      else {
        Get(node , -1 , 0 , -1 , 0);
      }
      cout << ans - max_depth << '\n';
      Init();
    }
  }
  return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1752 KB Output is correct
2 Correct 10 ms 2272 KB Output is correct
3 Correct 10 ms 3024 KB Output is correct
4 Correct 9 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 3136 KB Output is correct
2 Correct 22 ms 4576 KB Output is correct
3 Correct 22 ms 5664 KB Output is correct
4 Correct 16 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 7340 KB Output is correct
2 Correct 59 ms 11168 KB Output is correct
3 Correct 65 ms 13920 KB Output is correct
4 Correct 49 ms 8612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 575 ms 35568 KB Output is correct
2 Correct 547 ms 49232 KB Output is correct
3 Correct 561 ms 69400 KB Output is correct
4 Correct 406 ms 42340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1267 ms 71088 KB Output is correct
2 Correct 1226 ms 111560 KB Output is correct
3 Correct 1208 ms 131072 KB Output is correct
4 Correct 932 ms 84488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1261 ms 70880 KB Output is correct
2 Correct 1219 ms 111612 KB Output is correct
3 Correct 1216 ms 131072 KB Output is correct
4 Correct 929 ms 84456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1303 ms 70956 KB Output is correct
2 Correct 1228 ms 111688 KB Output is correct
3 Correct 1192 ms 131072 KB Output is correct
4 Correct 918 ms 84476 KB Output is correct