Submission #1074611

# Submission time Handle Problem Language Result Execution time Memory
1074611 2024-08-25T11:25:25 Z fryingduc Papričice (COCI20_papricice) C++17
15 / 110
2 ms 5212 KB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 2e5 + 5;
int n;
int sz[maxn];
vector<int> g[maxn];
int ans;

void pre_dfs(int u, int prev) {
  sz[u] = 1;
  for(auto v:g[u]) {
    if(v == prev) continue;
    pre_dfs(v, u);
    sz[u] += sz[v];
  }
}
multiset<int> a, b;
int calc(int a, int b, int c) {
  return max({a, b, c}) - min({a, b, c});
}
void dfs(int u, int prev) {
  for(auto v:g[u]) {
    if(v == prev) continue;
    
    a.insert(sz[u]);
    b.erase(b.find(sz[v]));
    
    
    auto it = a.lower_bound((n + sz[v]) / 2);
    if(it != a.end()) {
      int y = *it;
      ans = min(ans, calc(sz[v], y - sz[v], n - y));
    }
    if(it != a.begin()) {
      --it;
      int y = *it;
      ans = min(ans, calc(sz[v], y - sz[v], n - y));
    }
    it = b.lower_bound((n - sz[v]) / 2);
    if(it != b.end()) {
      int y = *it;
      ans = min(ans, calc(sz[v], y, n - sz[v] - y));
    }    
    if(it != b.begin()) {
      --it;
      int y = *it;
      ans = min(ans, calc(sz[v], y, n - sz[v] - y));
    }
    dfs(v, u);
    
    a.erase(a.find(sz[u]));
    b.insert(sz[v]);
  }
}
void solve() {
  cin >> n;
  for(int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  ans = n + 1;
  pre_dfs(1, 0);
  for(int i = 2; i <= n; ++i) {
    b.insert(sz[i]);
  }
  dfs(1, 0);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4960 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4960 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Incorrect 2 ms 5212 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 2 ms 4960 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Incorrect 2 ms 5212 KB Output isn't correct
8 Halted 0 ms 0 KB -