結果

問題 No.806 木を道に
ユーザー outlineoutline
提出日時 2020-03-27 19:10:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,035 bytes
コンパイル時間 1,046 ms
コンパイル使用メモリ 115,828 KB
実行使用メモリ 10,384 KB
最終ジャッジ日時 2023-08-30 16:43:45
合計ジャッジ時間 2,779 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 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 18 ms
8,020 KB
testcase_25 AC 26 ms
10,384 KB
testcase_26 AC 9 ms
5,088 KB
testcase_27 AC 27 ms
9,532 KB
testcase_28 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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