結果
問題 |
No.806 木を道に
|
ユーザー |
![]() |
提出日時 | 2020-05-10 14:55:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,167 bytes |
コンパイル時間 | 1,656 ms |
コンパイル使用メモリ | 170,324 KB |
実行使用メモリ | 12,416 KB |
最終ジャッジ日時 | 2024-07-07 18:18:43 |
合計ジャッジ時間 | 3,375 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 WA * 18 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; const int maxV = 101010; vector<int> G[maxV]; // 頂点情報のみのグラフ // treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離, 根から最大の距離となる頂点 void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { // 距離と終点を更新 if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { // 逆流を防ぐ if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } int getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); return maxDist; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> ms[n]; rep(i, n - 1){ int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } int max_dist = getTreeDiameter(); cout << n - max_dist - 1<< endl; }