結果

問題 No.806 木を道に
ユーザー kagasankagasan
提出日時 2020-04-25 17:27:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,496 bytes
コンパイル時間 1,947 ms
コンパイル使用メモリ 172,164 KB
実行使用メモリ 11,152 KB
最終ジャッジ日時 2024-04-25 00:51:51
合計ジャッジ時間 5,043 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 4 ms
6,016 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
6,016 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
6,016 KB
testcase_08 AC 4 ms
5,888 KB
testcase_09 AC 4 ms
5,888 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 40 ms
7,936 KB
testcase_25 AC 59 ms
8,960 KB
testcase_26 AC 24 ms
7,448 KB
testcase_27 AC 72 ms
11,152 KB
testcase_28 AC 5 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P>IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P> > G;
void g_dir(G &graph, ll a, ll b, ll w = 1){graph[a].push_back(P(b, w));}
void g_undir(G &graph, ll a, ll b, ll w = 1){g_dir(graph, a, b, w);g_dir(graph, b, a, w);}
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

V path[101010];
ll dep[101010];
ll N;
void bfs(ll rt){
    rep1(i, N)dep[i] = INF;
    queue<ll>Q;
    Q.push(rt);
    dep[rt] = 0;
    while(!Q.empty()){
        ll idx = Q.front();
        Q.pop();
        for(auto to : path[idx]){
            if(dep[to] == INF){
                dep[to] = dep[idx] + 1;
                Q.push(to);
            }
        }
    }
}


int main(){

    cin >> N;
    rep(i, N - 1){
        ll a, b;
        cin >> a >> b;
        path[a].push_back(b);
        path[b].push_back(a);
    }
    bfs(1);
    ll nxt = 1;
    rep1(i, N)if(dep[i] > dep[nxt])nxt = i;
    bfs(nxt);
    ll tmp = 0;
    rep1(i, N)chmax(tmp, dep[i]);
    cout << N - 1 - tmp << endl;

    

    return 0;
}
0