結果

問題 No.2677 Minmax Independent Set
ユーザー じなぺじなぺ
提出日時 2024-03-20 12:38:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 5,005 bytes
コンパイル時間 4,416 ms
コンパイル使用メモリ 260,180 KB
実行使用メモリ 54,204 KB
最終ジャッジ日時 2024-03-20 12:38:55
合計ジャッジ時間 12,814 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 179 ms
17,980 KB
testcase_06 AC 153 ms
18,492 KB
testcase_07 AC 152 ms
18,876 KB
testcase_08 AC 156 ms
19,900 KB
testcase_09 AC 155 ms
21,820 KB
testcase_10 AC 85 ms
15,004 KB
testcase_11 AC 82 ms
15,004 KB
testcase_12 AC 110 ms
54,204 KB
testcase_13 AC 170 ms
28,092 KB
testcase_14 AC 127 ms
28,092 KB
testcase_15 AC 150 ms
36,412 KB
testcase_16 AC 129 ms
15,004 KB
testcase_17 AC 126 ms
15,004 KB
testcase_18 AC 68 ms
10,752 KB
testcase_19 AC 83 ms
12,060 KB
testcase_20 AC 25 ms
6,676 KB
testcase_21 AC 97 ms
13,084 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 96 ms
15,036 KB
testcase_29 AC 178 ms
39,224 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 4 ms
6,676 KB
testcase_42 AC 6 ms
6,676 KB
testcase_43 AC 11 ms
6,676 KB
testcase_44 AC 22 ms
6,676 KB
testcase_45 AC 49 ms
8,960 KB
testcase_46 AC 125 ms
14,716 KB
testcase_47 AC 126 ms
15,004 KB
testcase_48 AC 130 ms
15,004 KB
testcase_49 AC 131 ms
15,004 KB
testcase_50 AC 31 ms
7,800 KB
testcase_51 AC 4 ms
6,676 KB
testcase_52 AC 67 ms
13,076 KB
testcase_53 AC 61 ms
12,340 KB
testcase_54 AC 3 ms
6,676 KB
testcase_55 AC 83 ms
15,004 KB
testcase_56 AC 82 ms
15,004 KB
testcase_57 AC 81 ms
15,004 KB
testcase_58 AC 79 ms
15,004 KB
testcase_59 AC 79 ms
15,004 KB
testcase_60 AC 62 ms
15,004 KB
testcase_61 AC 66 ms
15,004 KB
testcase_62 AC 73 ms
22,204 KB
testcase_63 AC 84 ms
34,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;

//多倍長整数//
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//using Bint = mp::cpp_int;

const int INF = 1e9;
const int MOD = 998244353;
const long long LINF = 4e18;

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvl = vector<vector<long long>>;
using vvvl = vector<vector<vector<long long>>>;
using vvvvl = vector<vector<vector<vector<long long>>>>;
using vvc = vector<vector<char>>;
using vvb = vector<vector<bool>>;
using vvvb = vector<vector<vector<bool>>>;
using vvvvb = vector<vector<vector<vector<bool>>>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define ALL(obj) (obj).begin(),(obj).end()

//https://trap.jp/post/1702/

template <class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)>
struct RerootingDP {
    struct edge {
        int to, idx, xdi;
    };
    RerootingDP(int n_ = 0) : n(n_), inner_edge_id(0) {
        es.resize(2*n-2);
        start.resize(2*n-2);
        if (n == 1) es_build();
    }
    void add_edge(int u, int v, int idx, int xdi){
        start[inner_edge_id] = u;
        es[inner_edge_id] = {v,idx,xdi};
        inner_edge_id++;
        start[inner_edge_id] = v;
        es[inner_edge_id] = {u,xdi,idx};
        inner_edge_id++;
        if (inner_edge_id == 2*n-2){
            es_build();
        }
    }
    vector<V> build(int root_ = 0){
        root = root_;
        vector<V> subdp(n); subdp[0] = put_vertex(e(),0);
        outs.resize(n);
        vector<int> geta(n+1,0);
        for (int i = 0; i < n; i++) geta[i+1] = start[i+1] - start[i] - 1;
        geta[root+1]++;
        for (int i = 0; i < n; i++) geta[i+1] += geta[i];
        auto dfs = [&](auto sfs, int v, int f) -> void {
            E val = e();
            for (int i = start[v]; i < start[v+1]; i++){
                if (es[i].to == f){
                    swap(es[start[v+1]-1],es[i]);
                }
                if (es[i].to == f) continue;
                sfs(sfs,es[i].to,v);
                E nval = put_edge(subdp[es[i].to],es[i].idx);
                outs[geta[v]++] = nval;
                val = merge(val,nval);
            }
            subdp[v] = put_vertex(val, v);
        };
        dfs(dfs,root,-1);
        return subdp;
    }
    vector<V> reroot(){
        vector<E> reverse_edge(n);
        reverse_edge[root] = e();
        vector<V> answers(n);
        auto dfs = [&](auto sfs, int v) -> void {
            int le = outs_start(v);
            int ri = outs_start(v+1);
            int siz = ri - le;
            vector<E> rui(siz+1);
            rui[siz] = e();
            for (int i = siz-1; i >= 0; i--){
                rui[i] = merge(outs[le+i],rui[i+1]);
            }
            answers[v] = put_vertex(merge(rui[0],reverse_edge[v]),v);
            E lui = e();
            for (int i = 0; i < siz; i++){
                V rdp = put_vertex(merge(merge(lui,rui[i+1]),reverse_edge[v]),v);
                reverse_edge[es[start[v]+i].to] = put_edge(rdp,es[start[v]+i].xdi);
                lui = merge(lui,outs[le+i]);
                sfs(sfs,es[start[v]+i].to);
            }
        };
        dfs(dfs,root);
        return answers;
    }
    private:
    int n, root, inner_edge_id;
    vector<E> outs;
    vector<edge> es;
    vector<int> start;
    int outs_start(int v){
        int res = start[v] - v;
        if (root < v) res++;
        return res;
    }
    void es_build(){
        vector<edge> nes(2*n-2);
        vector<int> nstart(n+2,0);
        for (int i = 0; i < 2*n-2; i++) nstart[start[i]+2]++;
        for (int i = 0; i < n; i++) nstart[i+1] += nstart[i];
        for (int i = 0; i < 2*n-2; i++) nes[nstart[start[i]+1]++] = es[i];
        swap(es,nes);
        swap(start,nstart);
    }
};

pair<int,int> merge(pair<int,int> a,pair<int,int> b){
	return {a.first + b.first,a.second + b.second};
}

pair<int,int> e(){
	return make_pair(0,0);
}

pair<int,int> put_e(pair<int,int> x,int i){
	return {x.first,max(x.first,x.second)};
}

pair<int,int> put_v(pair<int,int> x,int i){
	return {x.second,x.first + 1};
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin >> n;
	RerootingDP<pair<int,int>, pair<int,int>,merge, e,put_e,put_v> dp(n);
	rep(i,n - 1){
		int a,b;
		cin >> a >> b;
		a--;b--;
		dp.add_edge(a,b,i,i + n);
	}
	int ans = INF;
	dp.build();
	auto ret = dp.reroot();
	// cout << endl;
	// rep(i,n) cout << ret[i].first << ' ' << ret[i].second << endl;
	rep(i,n){
		ans = min(ans,ret[i].second);
	}
	cout << ans << endl;
	return 0;
}
0