結果

問題 No.806 木を道に
ユーザー hagyu_ayahagyu_aya
提出日時 2019-03-22 23:01:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,717 bytes
コンパイル時間 1,825 ms
コンパイル使用メモリ 179,284 KB
実行使用メモリ 13,548 KB
最終ジャッジ日時 2023-10-19 10:14:00
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,968 KB
testcase_01 AC 4 ms
5,968 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
5,968 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
5,968 KB
testcase_08 AC 4 ms
5,968 KB
testcase_09 AC 4 ms
5,968 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 22 ms
7,960 KB
testcase_25 AC 31 ms
9,016 KB
testcase_26 AC 20 ms
7,964 KB
testcase_27 AC 61 ms
13,548 KB
testcase_28 AC 5 ms
6,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define CEIL(a,b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))

using namespace std;
using ll = long long;
using pii = pair<int, int>;

constexpr int MOD = int(1e9 + 7);
constexpr int INF = int(1e9 + 1);
constexpr ll LLINF = ll(4 * 1e18 + 1);
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};

struct Edge{
    int to;
    int cost;
};
vector<Edge> g[100000];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int n;
    cin >> n;
    for(int i = 0; i < n - 1; ++i){
        int a, b;
        cin >> a >> b;
        --a; --b;
        g[a].push_back({b, 1});
        g[b].push_back({a, 1});
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
    vector<int> d(n, INF);
    d[0] = 0;
    que.push({int(0), 0});
    
    while(!que.empty()){
        int c = que.top().first;
        int v = que.top().second;
        que.pop();
        
        for(auto && e : g[v]){
            if(d[e.to] > c + e.cost){
                d[e.to] = c + e.cost;
                que.push({d[e.to], e.to});
            }
        }
    }
    
    int ma = -1, mai = -1;
    for(int i = 0; i < n; ++i){
        if(ma < d[i]){
            ma = d[i];
            mai = i;
        }
    }
    
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que2;
    vector<int> d2(n, INF);
    d2[mai] = 0;
    que2.push({int(0), mai});
    
    while(!que2.empty()){
        int c = que2.top().first;
        int v = que2.top().second;
        que2.pop();
        
        for(auto && e : g[v]){
            if(d2[e.to] > c + e.cost){
                d2[e.to] = c + e.cost;
                que2.push({d2[e.to], e.to});
            }
        }
    }
    
    int ma2 = 0, mai2 = -1;;
    for(int i = 0; i < n; ++i){
        ma2 = max(ma2, d2[i]);
        if(ma2 == d2[i]) mai2 = i;
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que3;
    vector<int> d3(n, INF);
    d3[mai2] = 0;
    que3.push({int(0), mai2});
    
    while(!que3.empty()){
        int c = que3.top().first;
        int v = que3.top().second;
        que3.pop();
        
        for(auto && e : g[v]){
            if(d3[e.to] > c + e.cost){
                d3[e.to] = c + e.cost;
                que3.push({d3[e.to], e.to});
            }
        }
    }
    

    int ans = 0;

    for(int i = 0; i < n; ++i){
        if(d2[mai2] == d2[i] + d3[i]) ans += g[i].size();
    }
    
    cout << ans - ma2 * 2 << "\n";
    return 0;
}
0