結果

問題 No.1976 Cut then Connect
ユーザー 👑 NachiaNachia
提出日時 2022-06-11 01:30:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,084 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 98,632 KB
実行使用メモリ 7,672 KB
最終ジャッジ日時 2024-09-21 08:44:48
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
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 AC 2 ms
6,944 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 WA -
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <vector>
#include <utility>

namespace nachia{
    
struct AdjacencyList{
public:
    struct AdjacencyListRange{
        using iterator = typename std::vector<int>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const int& operator[](int i) const { return begi[i]; }
    };
private:
    int mn;
    std::vector<int> E;
    std::vector<int> I;
public:
    AdjacencyList(int n, std::vector<std::pair<int,int>> edges, bool rev){
        mn = n;
        std::vector<int> buf(n+1, 0);
        for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        E.resize(buf[n]);
        for(int i=(int)edges.size()-1; i>=0; i--){
            auto [u,v] = edges[i];
            E[--buf[u]] = v;
            if(rev) E[--buf[v]] = u;
        }
        I = std::move(buf);
    }
    AdjacencyList(const std::vector<std::vector<int>>& edges = {}){
        int n = mn = edges.size();
        std::vector<int> buf(n+1, 0);
        for(int i=0; i<n; i++) buf[i+1] = buf[i] + edges[i].size();
        E.resize(buf[n]);
        for(int i=0; i<n; i++) for(int j=0; j<(int)edges[i].size(); j++) E[buf[i]+j] = edges[i][j];
        I = std::move(buf);
    }
    static AdjacencyList from_raw(std::vector<int> targets, std::vector<int> bounds){
        AdjacencyList res;
        res.mn = bounds.size() - 1;
        res.E = std::move(targets);
        res.I = std::move(bounds);
        return res;
    }
    AdjacencyListRange operator[](int u) const {
        return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
    }
    int num_vertices() const { return mn; }
    int size() const { return num_vertices(); }
    int num_edges() const { return E.size(); }
    AdjacencyList reversed_edges() const {
        AdjacencyList res;
        int n = res.mn = mn;
        std::vector<int> buf(n+1, 0);
        for(int v : E) ++buf[v];
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.E.resize(buf[n]);
        for(int u=0; u<n; u++) for(int v : operator[](u)) res.E[--buf[v]] = u;
        res.I = std::move(buf);
        return res;
    }
};

struct AdjacencyListEdgeIndexed{
public:
    struct Edge { int to; int edgeidx; };
    struct AdjacencyListRange{
        using iterator = typename std::vector<Edge>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const Edge& operator[](int i) const { return begi[i]; }
    };
private:
    int mn;
    std::vector<Edge> E;
    std::vector<int> I;
public:
    AdjacencyListEdgeIndexed(int n, const std::vector<std::pair<int,int>>& edges, bool rev){
        mn = n;
        std::vector<int> buf(n+1, 0);
        for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        E.resize(buf[n]);
        for(int i=(int)edges.size()-1; i>=0; i--){
            auto [u,v] = edges[i];
            E[--buf[u]] = { v, i };
            if(rev) E[--buf[v]] = { u, i };
        }
        I = std::move(buf);
    }
    AdjacencyListEdgeIndexed() : AdjacencyListEdgeIndexed(0, {}, false) {}
    AdjacencyListRange operator[](int u) const {
        return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
    }
    int num_vertices() const { return mn; }
    int size() const { return num_vertices(); }
    int num_edges() const { return E.size(); }
    AdjacencyListEdgeIndexed reversed_edges() const {
        AdjacencyListEdgeIndexed res;
        int n = res.mn = mn;
        std::vector<int> buf(n+1, 0);
        for(auto [v,i] : E) ++buf[v];
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.E.resize(buf[n]);
        for(int u=0; u<n; u++) for(auto [v,i] : operator[](u)) res.E[--buf[v]] = {u,i};
        res.I = std::move(buf);
        return res;
    }
};

} // namespace nachia

#include <algorithm>

struct S {
    int height;
    int diam;
};

struct Edge {
};


S rake(S l, S r){
    S res;
    res.height = std::max(l.height, r.height);
    res.diam = std::max(l.diam, l.height + r.height);
    return res;
}

S compress(S l, Edge edge){
    S res;
    res.height = l.height + 1;
    res.diam = std::max(l.diam, res.height);
    return res;
}

S e(){ return { 0,0 }; }
S node(int v){ return { 0,0 }; }



struct AnyDirectionTreeDP{
    std::vector<S> low;
    std::vector<S> high;

    AnyDirectionTreeDP(const nachia::AdjacencyList& adj){
        int n = adj.num_vertices();
        std::vector<int> bfs = {0};
        bfs.reserve(n);
        std::vector<int> P(n, -1);
        for(int i=0; i<n; i++){
            int p = bfs[i];
            for(int e : adj[p]) if(P[p] != e){
                P[e] = p;
                bfs.push_back(e);
            }
        }

        low.assign(n, e());
        high.assign(n, e());

        for(int i=0; i<n; i++) low[i] = node(i);
        for(int i=n-1; i>=1; i--){
            int p = bfs[i];
            low[P[p]] = rake(low[P[p]], compress(low[p], {}));
        }

        for(int i=0; i<n; i++) high[i] = node(i);
        for(int i=0; i<n; i++){
            int p = bfs[i];
            int C = adj[p].size();
            for(int ci=0; ci<C; ci++) if(P[p] != adj[p][ci]){
                int c = adj[p][ci];
                high[c] = compress(high[p], {});
            }
            S fold = e();
            for(int ci=C-1; ci>=0; ci--) if(P[p] != adj[p][ci]){
                int c = adj[p][ci];
                high[c] = rake(high[c], fold);
                fold = rake(fold, compress(low[c], {}));
            }
            fold = e();
            for(int ci=0; ci<C; ci++) if(P[p] != adj[p][ci]){
                int c = adj[p][ci];
                high[c] = rake(high[c], fold);
                fold = rake(fold, compress(low[c], {}));
            }
        }
    }
};


#include <iostream>
#include <string>
#include <map>
#include <atcoder/modint>


using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


const i64 INF = 1001001001001001001;
using modint = atcoder::static_modint<1000000007>;


int main(){
    int N; cin >> N;
    vector<pair<int,int>> edges(N-1);
    rep(i,N-1){ int u,v; cin >> u >> v; u--; v--; edges[i] = make_pair(u,v); }
    auto adj = nachia::AdjacencyList(N, edges, true);

    auto treedp = AnyDirectionTreeDP(adj);

    int ans = N;
    rep(i,N) if(i != 0){
        int diam1 = treedp.high[i].diam;
        int diam2 = treedp.low[i].diam;
        int cr_ans = (diam1 + 1) / 2 + (diam2 + 1) / 2 + 1;
        cr_ans = max({ cr_ans, diam1, diam2 });
        ans = min(ans, cr_ans);
    }

    cout << ans << '\n';
    
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0