結果

問題 No.806 木を道に
ユーザー polyomino_24
提出日時 2019-03-22 23:48:16
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,624 bytes
コンパイル時間 773 ms
コンパイル使用メモリ 104,444 KB
実行使用メモリ 16,896 KB
最終ジャッジ日時 2024-09-19 07:08:09
合計ジャッジ時間 2,564 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
using namespace std;
using ll = long long;
using pii = pair<int,int>;
template<typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a){
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os, vector<T> v){
    for(auto x:v)os << x << ' ';
    return os;
}
void debug(){cerr << '\n';}
template<typename H, typename... T>
void debug(H a, T... b){
    cerr << a;
    if(sizeof...(b))cerr << ", ";
    debug(b...);
}
vector<int>g[100005];
int ans;
int dfs(int s,int p){
//    show(s);
    int temp = 0;
    vector<int>a;
    for(int x:g[s]){
        if(x == p)continue;
        a.push_back(dfs(x,s));
    }
    sort(a.rbegin(),a.rend());
    if(a.size()>1)ans = max(ans,a[0]+a[1]+2);
    if(a.size()){
        ans = max(ans, a[0] + 1);
        temp = max(temp, a[0] + 1);
    }
    return temp;
}
int main(){
    int n;
    cin >> n;
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        a--,b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0,-1);
    cout << n-1-ans << endl;
}
0