結果

問題 No.806 木を道に
ユーザー hamuhei4869hamuhei4869
提出日時 2019-03-22 21:36:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,497 bytes
コンパイル時間 1,615 ms
コンパイル使用メモリ 175,068 KB
実行使用メモリ 19,960 KB
最終ジャッジ日時 2023-10-19 06:35:33
合計ジャッジ時間 4,244 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 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 49 ms
9,268 KB
testcase_25 AC 73 ms
12,876 KB
testcase_26 AC 31 ms
7,916 KB
testcase_27 AC 105 ms
19,960 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:
    int to,from,value;
    Edge(int t,int f,int 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 = max_element(di.begin(),di.end())-di.begin();
    vector<LL> ansns = dijkstra(idx,N,Edges);
    cout<<N-*max_element(ansns.begin(),ansns.end())-1<<endl;;
}
0