結果

問題 No.2677 Minmax Independent Set
ユーザー じなぺじなぺ
提出日時 2024-03-20 12:38:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 5,005 bytes
コンパイル時間 2,932 ms
コンパイル使用メモリ 259,836 KB
実行使用メモリ 53,956 KB
最終ジャッジ日時 2024-09-30 06:03:19
合計ジャッジ時間 8,024 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 85 ms
17,984 KB
testcase_06 AC 83 ms
18,368 KB
testcase_07 AC 82 ms
18,756 KB
testcase_08 AC 86 ms
19,776 KB
testcase_09 AC 85 ms
21,700 KB
testcase_10 AC 60 ms
14,828 KB
testcase_11 AC 59 ms
14,916 KB
testcase_12 AC 91 ms
53,956 KB
testcase_13 AC 89 ms
27,968 KB
testcase_14 AC 80 ms
27,968 KB
testcase_15 AC 95 ms
36,164 KB
testcase_16 AC 78 ms
14,896 KB
testcase_17 AC 79 ms
14,964 KB
testcase_18 AC 48 ms
10,752 KB
testcase_19 AC 57 ms
12,032 KB
testcase_20 AC 21 ms
6,820 KB
testcase_21 AC 64 ms
13,092 KB
testcase_22 AC 6 ms
6,816 KB
testcase_23 AC 2 ms
6,816 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 2 ms
6,820 KB
testcase_27 AC 2 ms
6,816 KB
testcase_28 AC 61 ms
14,800 KB
testcase_29 AC 109 ms
39,100 KB
testcase_30 AC 2 ms
6,820 KB
testcase_31 AC 2 ms
6,820 KB
testcase_32 AC 2 ms
6,820 KB
testcase_33 AC 2 ms
6,820 KB
testcase_34 AC 2 ms
6,816 KB
testcase_35 AC 2 ms
6,816 KB
testcase_36 AC 2 ms
6,816 KB
testcase_37 AC 1 ms
6,816 KB
testcase_38 AC 2 ms
6,820 KB
testcase_39 AC 2 ms
6,820 KB
testcase_40 AC 3 ms
6,816 KB
testcase_41 AC 3 ms
6,816 KB
testcase_42 AC 5 ms
6,816 KB
testcase_43 AC 9 ms
6,820 KB
testcase_44 AC 19 ms
6,820 KB
testcase_45 AC 36 ms
8,960 KB
testcase_46 AC 81 ms
14,732 KB
testcase_47 AC 79 ms
14,864 KB
testcase_48 AC 79 ms
14,980 KB
testcase_49 AC 83 ms
14,800 KB
testcase_50 AC 24 ms
7,672 KB
testcase_51 AC 4 ms
6,816 KB
testcase_52 AC 51 ms
12,952 KB
testcase_53 AC 47 ms
12,220 KB
testcase_54 AC 3 ms
6,816 KB
testcase_55 AC 64 ms
14,908 KB
testcase_56 AC 66 ms
14,828 KB
testcase_57 AC 66 ms
14,936 KB
testcase_58 AC 62 ms
14,780 KB
testcase_59 AC 60 ms
14,796 KB
testcase_60 AC 56 ms
14,800 KB
testcase_61 AC 54 ms
14,904 KB
testcase_62 AC 64 ms
21,952 KB
testcase_63 AC 73 ms
34,240 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