/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Wrong Answer 49ms 7.191 MiB
#3 Wrong Answer 49ms 7.062 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);
  }

  auto bfs = [&](int start) -> pair<int, vector<int>> {
    vector<int> dist(n, -1);
    queue<int> q;
    q.push(start);
    dist[start] = 0;

    int farthest_node = start;
    int farthest = 0;

    while (!q.empty()) {
      int node = q.front();
      q.pop();

      for (int it : g[node]) {
        if (dist[it] == -1) {
          q.push(it);
          dist[it] = dist[node] + 1;

          if (dist[it] > farthest) {
            farthest = dist[it];
            farthest_node = it;
          }
        }
      }
    }

    return {farthest_node, dist};
  };

  auto [first, distA] = bfs(0);
  auto [second, distB] = bfs(first);

  int diameter = distB[second];

  int center = second;
  for (int i = 0; i < diameter / 2; ++i) {
    center = g[center][0];
  }
  cout << (diameter + 1) / 2 << " " << center + 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:08:12
Judged At
2024-11-11 03:23:09
Judged By
Score
5
Total Time
49ms
Peak Memory
7.191 MiB