結果

問題 No.806 木を道に
コンテスト
ユーザー kuromath
提出日時 2026-10-04 19:34:39
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 12,397 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,303 ms
コンパイル使用メモリ 384,700 KB
実行使用メモリ 20,960 KB
最終ジャッジ日時 2026-10-04 19:34:51
合計ジャッジ時間 6,863 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
// using mint = modint1000000007;
#endif
//#pragma GCC target("avx2")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
using namespace std;

inline void scan(int &a) { cin >> a; }
inline void scan(unsigned int &a) { cin >> a; }
inline void scan(long long &a) { cin >> a; }
inline void scan(unsigned long long &a) { cin >> a; }
inline void scan(float &a) { cin >> a; }
inline void scan(double &a) { cin >> a; }
inline void scan(long double &a) { cin >> a; }
inline void scan(char &a) { cin >> a; }
inline void scan(string &a) { cin >> a; }
inline void scan(vector<bool> &vec) { for (int i = 0; i < int(vec.size()); i++) { int a; scan(a); vec[i] = a; } }

template<typename T> inline void scan(vector<T> &vec);
template<typename T, size_t size> inline void scan(array<T, size> &vec);
template<typename T> inline void scan(deque<T> &deq);
template<typename T, typename U> inline void scan(pair<T, U> &p);

template<typename T> inline void scan(vector<T> &vec) { for (auto &i : vec) scan(i); }
template<typename T, size_t size> inline void scan(array<T, size> &vec) { for (auto &i : vec) scan(i); }
template<typename T> inline void scan(deque<T> &deq) { for (auto &i : deq) scan(i); }
template<typename T, typename U> inline void scan(pair<T, U> &p) { scan(p.first); scan(p.second); }
template<typename T> inline void scan(T &a) { cin >> a; }
inline void read() { }
template<typename T, typename... U> inline void read(T &a, U &...b) { scan(a); read(b...); }

inline void write(const int &a) { cout << a; }
inline void write(const unsigned int &a) { cout << a; }
inline void write(const long long &a) { cout << a; }
inline void write(const unsigned long long &a) { cout << a; }
inline void write(const float &a) { cout << a; }
inline void write(const double &a) { cout << a; }
inline void write(const long double &a) { cout << a; }
inline void write(const char &a) { cout << a; }
inline void write(const string &a) { cout << a; }

template<typename T> inline void write(const vector<T> &vec);
template<typename T, size_t size> inline void write(const array<T, size> &vec);
template<typename T> inline void write(const deque<T> &deq);
template<typename T, typename U> inline void write(const pair<T, U> &p);

template<typename T> inline void write(const vector<T> &vec) {
    for (int i = 0; i < int(vec.size()); i++) {
        if (i) write(' ');
        write(vec[i]);
    }
}

template<typename T, size_t size> inline void write(const array<T, size> &vec) {
    for (int i = 0; i < int(vec.size()); i++) {
        if (i) write(' ');
        write(vec[i]);
    }
}

template<typename T> inline void write(const deque<T> &deq) {
    for (int i = 0; i < int(deq.size()); i++) {
        if (i) write(' ');
        write(deq[i]);
    }
}

template<typename T, typename U> inline void write(const pair<T, U> &p) {
    write(p.first);
    write(' ');
    write(p.second);
}

template<typename T> inline void write(const T &a) { cout << a; }

inline void print() { cout << '\n'; }
template<typename T> inline void print(const T &a) { write(a); cout << '\n'; }
template<typename T, typename... U> inline void print(const T &a, const U &...b) {
    write(a);
    ((cout << ' ', write(b)), ...);
    write('\n');
}
template<typename T> inline void debug_impl(const char *name, const T &value) {
    cerr << name << " = ";
    write(value);
    cerr << '\n';
}

template<typename T, typename ... U> inline void debug_impl(const char *names, const T &value, const U &...values) {
    const char *comma = strchr(names, ',');
    cerr.write(names, comma - names);
    cerr << " = ";
    write(value);
    cerr << ", ";
    while (*++comma == ' ');
    debug_impl(comma, values...);
}

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T> using pq = priority_queue<T>;
template<typename T> using pq_g = priority_queue<T, vector<T>, greater<T>>;

#define overload3(a, b, c, d, ...) d
#define overload4(a, b, c, d, e, ...) e
#define rep1(n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, a, b) for (ll i = (a); i < (b); i++)
#define rep4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep4(i, a, b, c) for (ll i = (a) + ((b) - (a) - 1) / (c) * (c); i >= (a); i -= (c))
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all1(v) (v).begin(), (v).end()
#define all2(v, n) (v).begin(), (v).begin() + (n)
#define all3(v, a, b) (v).begin() + (a), (v).begin() + (b)
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define rall(v) (v).rbegin(), (v).rend()
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end())
#define NP next_permutation
#define endl '\n'
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) long long __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) unsigned long long __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define CHAR(...) char __VA_ARGS__; read(__VA_ARGS__);
#define LD(...) long double __VA_ARGS__; read(__VA_ARGS__);
#define VEC(type, name, size) vector<type> name(size); read(name);
#define VV(type, name, h, w) vector<vector<type>> name(h, vector<type>(w)); read(name);
#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) ((void)0)
#endif

inline void Yes(bool a = true) { cout << (a ? "Yes" : "No") << endl; }
inline void No(bool a = true) { cout << (a ? "No" : "Yes") << endl; }
inline void YES(bool a = true) { cout << (a ? "YES" : "NO") << endl; }
inline void NO(bool a = true) { cout << (a ? "NO" : "YES") << endl; }
inline void Takahashi(bool a = true) { cout << (a ? "Takahashi" : "Aoki") << endl; }
inline void Alice(bool a = true) { cout << (a ? "Alice" : "Bob") << endl; }
inline void Possible(bool a = true) { cout << (a ? "Possible" : "Impossible") << endl; }

template<typename T, typename U>
auto operator+(const pair<T, U>& a, const pair<T, U>& b) { return pair<T, U>{a.first + b.first, a.second + b.second}; };
template <typename T, typename U>
auto operator-(const pair<T, U>& a, const pair<T, U>& b) { return pair<T, U>{a.first - b.first, a.second - b.second}; };

constexpr int INF = 1001001001;
constexpr ll LINF = 4004004004004004004LL;
constexpr ld pi = 3.14159265358979323846;
constexpr ld eps = 1e-9;
constexpr int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
constexpr int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

ll modPow(ll a, ll n, ll mod = LINF) { ll ret = 1; ll x = a % mod; while (n) { if (n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; }
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
ll isqrt(ll x) { ll n = sqrtl(x); while ((n + 1) * (n + 1) <= x) n++; while (n * n > x) n--; return n; }
template<typename T> T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); }
template<typename T> T ceil(T a, T b) { return floor(a + b - 1, b); }
template<typename T> T bmod(T a, T b) { return a - b * floor(a, b); }
template<typename T> pair<T, T> divmod(T a, T b) { return { floor(a, b), a - b * floor(a, b) }; }
template<typename T> auto MIN(const vector<T> &a) { return *min_element(all(a)); }
template<typename T> auto MAX(const vector<T> &a) { return *max_element(all(a)); }
template<typename T> auto SUM(const vector<T> &a) { return accumulate(all(a), T(0)); }
template<typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }

void cincout() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
}

template<typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge() = default;
    Edge(int from_, int to_, T cost_ = 1, int idx_ = -1) 
        : from(from_), to(to_), cost(cost_), idx(idx_) {}
};  

template<typename T>
struct Graph {
    vector<vector<Edge<T>>> G;
    int idx;

    Graph() = default;
    explicit Graph(int n) : G(n), idx(0) {}

    void add_edge(int u, int v, T cost = 1) {
        G[u].emplace_back(u, v, cost, idx);
        G[v].emplace_back(v, u, cost, idx++);
    }

    void add_directed_edge(int u, int v, T cost = 1) {
        G[u].emplace_back(u, v, cost, idx++);
    }

    void read(int M, bool weighted = false, bool directed = false, bool is_1origin = true) {
        for (int i = 0; i < M; i++) {
            int u, v;
            cin >> u >> v;
            if (is_1origin) u--, v--;
            T w = T(1);
            if (weighted) cin >> w;
            if (directed) {
                add_directed_edge(u, v, w);
            } else {
                add_edge(u, v, w);
            }
        }
    }

    size_t size() const { return G.size(); }
    vector<Edge<T>> &operator[](int v) { return G[v]; }
    const vector<Edge<T>> &operator[](int v) const { return G[v]; }
};

template<typename Graph>
struct Tree {
private:
    const Graph &G;
    int root;
    vector<array<int, 24>> doubling; 
    vector<int> dep;

    void build() {
        dep.resize(int(G.size()));
        doubling.resize(int(G.size()));
        for (auto &v : doubling) ranges::fill(v, -1);
        dfs(root, -1, 0);
    }

    void dfs(int v, int par, int d) {
        dep[v] = d;
        for (int u = par, x = 0; u != -1;) {
            doubling[v][x] = u;
            u = doubling[u][x];
            x++;
        }

        for (auto &e : G[v]) {
            if (e.to == par) continue;
            dfs(e.to, v, d + 1);
        }
    }

    int farthest(int v) {
        int mx = 0, ret = v;
        for (int i = 0; i < int(G.size()); i++) {
            int d = dist(v, i);
            if (mx < d) {
                mx = d;
                ret = i;
            }
        }
        return ret;
    }

public:
    Tree(const Graph &G, int root = 0) : G(G), root(root) { build(); }

    int depth(int v) const {
        return dep[v];
    }

    int par(int v) const {
        return (v == root ? -1 : doubling[v][0]);
    }

    int kth_ancestor(int v, int k) const {
        if (k == 0) return v;
        if (dep[v] < k) return -1;
        for (int i = 0; i <= __lg(k); i++) if (k >> i & 1) v = doubling[v][i];
        return v;
    }

    int lca(int u, int v) const {
        if (dep[u] > dep[v]) swap(u, v);
        v = kth_ancestor(v, dep[v] - dep[u]);
        if (u == v) return u;
        for (int i = __lg(dep[u]); i >= 0; i--) {
            if (doubling[u][i] != doubling[v][i]) {
                u = doubling[u][i];
                v = doubling[v][i];
            }
        }
        return doubling[u][0]; 
    }

    int dist(int u, int v) const {
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }

    bool is_on_path(int u, int v, int x) const {
        return dist(u, v) == dist(u, x) + dist(x, v);
    }

    vector<int> path(int u, int v) const {
        int l = lca(u, v);
        vector<int> prefix, suffix;
        while (u != l) {
            prefix.push_back(u);
            u = par(u);
        }
        prefix.push_back(l);

        while (v != l) {
            suffix.push_back(v);
            v = par(v);
        }
        
        ranges::reverse(suffix);
        prefix.append_range(suffix);
        return prefix;
    }

    pair<int, int> diameter() {
        int s = root;
        int u = farthest(s);
        int v = farthest(u);
        return make_pair(u, v);
    }
};

void solve() {
    INT(N);
    Graph<int> G(N);
    G.read(N - 1);
    Tree tree(G);
    auto [u, v] = tree.diameter();
    print(N - 1 - tree.dist(u, v));
}

int main() {
    cincout();
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}
0