結果

問題 No.806 木を道に
ユーザー outline
提出日時 2020-03-27 19:10:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,035 bytes
コンパイル時間 1,325 ms
コンパイル使用メモリ 114,196 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2025-01-02 09:12:54
合計ジャッジ時間 3,741 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

struct Diameter{
    using G = vector<vector<int>>;
    const G& g;
    vector<int> dist0, dist1;
    int diameter = 0;
    
    Diameter(const G& g) : g(g) {
        dist0.assign(g.size(), 0);
        dist1.assign(g.size(), 0);
        dfs(0, dist0);
        int root = max_element(dist0.begin(), dist0.end()) - dist0.begin();
        dfs(root, dist1);
        diameter = *max_element(dist1.begin(), dist1.end());
    }

    void dfs(int from, vector<int>& v, int p = -1){
        if(p != -1) v[from] = v[p] + 1;
        for(int to : g[from]){
            if(to == p)  continue;
            dfs(to, v, from);
        }
        return;
    }

    int query() { return diameter; }
};

vector<vector<int>> graph;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    graph.resize(n);
    for(int i = 0; i < n - 1; ++i){
        int a, b;
        cin >> a >> b;
        --a, --b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    Diameter d(graph);
    int ans = n - (d.query() + 1);
    cout << ans << endl;
}
0