結果

問題 No.806 木を道に
ユーザー betrue12betrue12
提出日時 2019-03-22 21:27:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,519 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 211,192 KB
実行使用メモリ 12,256 KB
最終ジャッジ日時 2023-10-19 06:25:50
合計ジャッジ時間 4,472 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,000 KB
testcase_01 AC 4 ms
6,000 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 4 ms
6,000 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
6,000 KB
testcase_08 AC 3 ms
6,000 KB
testcase_09 AC 4 ms
6,000 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 41 ms
8,684 KB
testcase_25 AC 60 ms
10,248 KB
testcase_26 AC 23 ms
7,796 KB
testcase_27 AC 73 ms
12,256 KB
testcase_28 AC 5 ms
6,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Diameter{
    vector<int> center;
    int N;

    pair<vector<int>, vector<int>> calc_dist(int start, const vector<int> G[]){
        vector<int> dist(N, -1), prev(N);
        dist[start] = 0;
        prev[start] = -1;
        queue<int> que;
        que.push(start);
        while(que.size()){
            int i = que.front(); que.pop();
            for(auto j : G[i]){
                if(dist[j] == -1){
                    dist[j] = dist[i]+1;
                    prev[j] = i;
                    que.push(j);
                }
            }
        }
        return {dist, prev};
    }

    int calculate(int n, const vector<int> G[]){
        N = n;
        auto res1 = calc_dist(0, G);
        auto d1 = res1.first;
        int v1 = max_element(d1.begin(), d1.end()) - d1.begin();
        auto res2 = calc_dist(v1, G);
        auto d2 = res2.first;
        auto p2 = res2.second;
        int v2 = max_element(d2.begin(), d2.end()) - d2.begin();
        int diam = d2[v2];
        while(v2 != -1){
            if(d2[v2] == diam/2 || d2[v2] == (diam+1)/2) center.push_back(v2);
            v2 = p2[v2];
        }
        return diam;
    }
};

vector<int> edges[100000];

int main(){
    int N;
    cin >> N;
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        edges[a-1].push_back(b-1);
        edges[b-1].push_back(a-1);
    }

    Diameter diam;
    int d = diam.calculate(N, edges);
    cout << (N-1) - d << endl;
    return 0;
}
0