Submission #784606

# Submission time Handle Problem Language Result Execution time Memory
784606 2023-07-16T10:54:48 Z cadmiumsky Mousetrap (CEOI17_mousetrap) C++17
0 / 100
32 ms 15256 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 1e5 + 5;

int dp[nmax], p[nmax];
vector<int> g[nmax];

vector<vector<int>> adj;

void initp(int node, int f) {
  p[node] = f;
  for(auto x : g[node]) if(x != f) initp(x, node);
}

void initdp(int node, int f) {
  //cerr << node << '\n';
  vector<int> vs;
  for(auto x : g[node]) {
    if(x == f) continue;
    initdp(x, node);
    vs.emplace_back(dp[x]);
  }
  sort(all(vs));
  if(sz(vs) < 2) dp[node] = 1;
  else dp[node] = sz(vs) + rbegin(vs)[1];
  return;
}

bool crapa(int threshold) {
  int tolerance = 0;
  for(int i = 0; i < sz(adj); tolerance++, i++) {
    for(auto x : adj[i])
      if(x > threshold) threshold--, tolerance--;
      else break;
    if(tolerance < 0 || threshold < 0) return 1;
  }
  return 0;
  
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  int n, T, M;
  cin >> n >> T >> M;
  for(int i = 1, x, y; i < n; i++) {
    cin >> x >> y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  
  initp(T, T);
  int prv = -1;
  while(M != T) {
    adj.emplace_back();
    for(auto x : g[M]) {
      if(x == p[M] || x == prv) continue;
      initdp(x, M);
      adj.back().emplace_back(dp[x]);
    }
    prv = M;
    //cerr << M << '\n';
    M = p[M];
    sort(all(adj.back()), greater<int>());
    
    //for(auto x : adj.back())
      //cerr << x << ' ';
    //cerr << '\n';
  }
  
  int sum = -1;
  for(int i = 0; i < sz(adj); i++) {
    sum += sz(rbegin(adj)[i]);
    for(auto &x : rbegin(adj)[i]) x += sum;
  }
  
  //for(auto v : adj) {
    //for(auto x : v)
      //cerr << x << ' ';
    //cerr << '\n';
  //}
  
  //cerr << " > " << crapa(3) << '\n';
  
  int lim = -1;
  for(int step = 18; step >= 0; step--)
    if(crapa(lim + (1 << step))) lim += (1 << step);
  
  cout << lim + 1 << '\n';;
  
}

/**
      Anul asta se da centroid.
-- Surse oficiale
*/

# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Incorrect 1 ms 2644 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 32 ms 15256 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Incorrect 1 ms 2644 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Incorrect 1 ms 2644 KB Output isn't correct
10 Halted 0 ms 0 KB -