/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Wrong Answer 78ms 7.188 MiB
#3 Accepted 82ms 7.078 MiB
#4 Accepted 90ms 7.152 MiB
#5 Accepted 94ms 7.078 MiB
#6 Accepted 83ms 7.109 MiB
#7 Wrong Answer 80ms 7.074 MiB

Code

#include <bits/stdc++.h>
using namespace std;

void solve(int cs) {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  vector<int> dist(n, -1), par(n, -1);

  auto bfs = [&](int st) -> vector<int> {
    queue<int> q;
    q.push(st);
    dist[st] = 0;
    par[st] = -1;

    int farthest = st;
    while (!q.empty()) {
      int node = q.front();
      q.pop();
      for (int neighbor : g[node]) {
        if (dist[neighbor] == -1) {
          q.push(neighbor);
          par[neighbor] = node;
          dist[neighbor] = dist[node] + 1;
          if (dist[neighbor] > dist[farthest]) {
            farthest = neighbor;
          }
        }
      }
    }
    return par;
  };

  par = bfs(0);
  int A = max_element(dist.begin(), dist.end()) - dist.begin();

  dist.assign(n, -1);
  par = bfs(A);
  int B = max_element(dist.begin(), dist.end()) - dist.begin();

  vector<int> path;
  for (int v = B; v != -1; v = par[v]) {
    path.push_back(v);
  }

  int len = path.size();
  int days = (len - 1) / 2;
  int node = path[len / 2];
  cout << days << " " << node + 1 << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  // cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1069 Vaccination
Contest
Brain Booster #4
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-14 17:03:43
Judged At
2024-11-11 03:23:12
Judged By
Score
25
Total Time
94ms
Peak Memory
7.188 MiB