Submission #583431

# Submission time Handle Problem Language Result Execution time Memory
583431 2022-06-25T10:53:17 Z 600Mihnea Mousetrap (CEOI17_mousetrap) C++17
25 / 100
1057 ms 77320 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int) 1e6 + 7;
int n, tr, mo;
vector<int> g[N];
int par[N], cost[N];

void build_tree(int a, int p = -1) {
  {
    vector<int> Kids;
    for (auto &b : g[a]) {
      if (b != p) {
        Kids.push_back(b);
      }
    }
    g[a] = Kids;
  }
  cost[a] += (int) g[a].size() - 1 + 1;
  for (auto &b : g[a]) {
    cost[b] += cost[a];
    build_tree(b, a);
  }
  par[a] = p;
}

void dfs(int a) {
  for (auto &b : g[a]) {
    dfs(b);
  }
  if ((int) g[a].size() == 0) {
    return;
  }
  if ((int) g[a].size() == 1) {
    return;
  }
  assert((int) g[a].size() >= 2);
  vector<int> Values;
  for (auto &b : g[a]) {
    Values.push_back(cost[b]);
  }
  sort(Values.rbegin(), Values.rend());
  assert((int) Values.size() >= 2);
  cost[a] = Values[1];
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

///  freopen ("input.txt", "r", stdin);

  cin >> n >> tr >> mo;

  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  build_tree(tr);
  dfs(tr);

  cout << cost[mo] - 1 << "\n";


}

# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 23732 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 368 ms 62884 KB Output is correct
2 Correct 358 ms 70988 KB Output is correct
3 Correct 952 ms 77320 KB Output is correct
4 Correct 423 ms 50256 KB Output is correct
5 Correct 1057 ms 77236 KB Output is correct
6 Correct 1014 ms 77228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 23732 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 23732 KB Output isn't correct
2 Halted 0 ms 0 KB -