結果

問題 No.806 木を道に
ユーザー misora192misora192
提出日時 2020-05-10 14:55:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,167 bytes
コンパイル時間 1,633 ms
コンパイル使用メモリ 169,068 KB
実行使用メモリ 12,236 KB
最終ジャッジ日時 2023-09-22 01:26:34
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,732 KB
testcase_01 AC 4 ms
5,708 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 4 ms
5,744 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
5,784 KB
testcase_08 AC 4 ms
5,728 KB
testcase_09 AC 4 ms
5,812 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 21 ms
10,068 KB
testcase_25 AC 31 ms
12,236 KB
testcase_26 AC 12 ms
7,400 KB
testcase_27 AC 31 ms
11,192 KB
testcase_28 AC 5 ms
5,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0