結果

問題 No.806 木を道に
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-22 21:39:42
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,589 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 174,536 KB
実行使用メモリ 25,720 KB
最終ジャッジ日時 2023-10-19 06:38:23
合計ジャッジ時間 5,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
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 56 ms
13,408 KB
testcase_25 AC 81 ms
19,324 KB
testcase_26 AC 34 ms
9,888 KB
testcase_27 AC 113 ms
25,720 KB
testcase_28 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LINF = 1e18;
class Edge{
public:
    LL to,from,value;
    Edge(LL t,LL f,LL c){
        to = t;
        from = f;
        value = c;
    }
};
#define P pair<LL,LL>
std::vector<LL> dijkstra(int s,int N,vector<Edge> E){
    std::vector<LL> ans(N,LINF);
    std::vector<std::vector<Edge>> Edges(N);
    for(auto e : E){
        Edges.at(e.from).push_back(e);
    }
    priority_queue<P, std::vector<P>, greater<P>> que;
    ans.at(s)= 0;
    que.push(make_pair(0,s));
    while(!que.empty()){
        P p = que.top();que.pop();
        if(ans.at(p.second) < p.first)continue;
        for(int i = 0;i < Edges.at(p.second).size();i++){
                if(ans.at(p.second) + Edges.at(p.second).at(i).value < ans.at(Edges.at(p.second).at(i).to)){
                ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).value;
                que.push(make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to));
            }
        }
    }
    return ans;
}
int main(){
    int N;cin >> N;
    vector<Edge> Edges;
    for(int a = 0;a < N-1;a++){
        int b,c;cin >> b >> c;
        Edges.push_back(Edge(b-1,c-1,1));
        Edges.push_back(Edge(c-1,b-1,1));
    }
    vector<LL> di = dijkstra(0,N,Edges);
    int idx = -1;
    LL id = 0;
    for(int a = 0;a < N;a++){
        if(id < di.at(a)){
            id = di.at(a);
            idx = id;
        }
    }
    vector<LL> ansns = dijkstra(idx,N,Edges);
    cout<<N-*max_element(ansns.begin(),ansns.end())-1<<endl;;
}
0