結果

問題 No.2677 Minmax Independent Set
ユーザー 👑 potato167potato167
提出日時 2024-03-15 21:43:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 4,398 bytes
コンパイル時間 2,452 ms
コンパイル使用メモリ 220,052 KB
実行使用メモリ 84,224 KB
最終ジャッジ日時 2024-09-30 00:38:32
合計ジャッジ時間 10,939 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 225 ms
38,620 KB
testcase_06 AC 232 ms
41,888 KB
testcase_07 AC 218 ms
42,616 KB
testcase_08 AC 215 ms
41,596 KB
testcase_09 AC 223 ms
44,392 KB
testcase_10 AC 144 ms
34,524 KB
testcase_11 AC 139 ms
34,468 KB
testcase_12 AC 250 ms
84,224 KB
testcase_13 AC 224 ms
50,924 KB
testcase_14 AC 199 ms
51,252 KB
testcase_15 AC 248 ms
61,256 KB
testcase_16 AC 231 ms
34,872 KB
testcase_17 AC 223 ms
34,940 KB
testcase_18 AC 135 ms
23,424 KB
testcase_19 AC 170 ms
27,056 KB
testcase_20 AC 42 ms
11,648 KB
testcase_21 AC 185 ms
29,668 KB
testcase_22 AC 10 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,816 KB
testcase_27 AC 2 ms
6,820 KB
testcase_28 AC 120 ms
35,308 KB
testcase_29 AC 248 ms
65,264 KB
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 2 ms
6,816 KB
testcase_33 AC 2 ms
6,816 KB
testcase_34 AC 2 ms
6,820 KB
testcase_35 AC 2 ms
6,816 KB
testcase_36 AC 2 ms
6,816 KB
testcase_37 AC 2 ms
6,820 KB
testcase_38 AC 2 ms
6,820 KB
testcase_39 AC 2 ms
6,820 KB
testcase_40 AC 4 ms
6,816 KB
testcase_41 AC 5 ms
6,816 KB
testcase_42 AC 10 ms
6,816 KB
testcase_43 AC 17 ms
7,168 KB
testcase_44 AC 39 ms
11,264 KB
testcase_45 AC 99 ms
19,072 KB
testcase_46 AC 248 ms
34,716 KB
testcase_47 AC 241 ms
35,384 KB
testcase_48 AC 245 ms
35,124 KB
testcase_49 AC 246 ms
35,472 KB
testcase_50 AC 43 ms
15,064 KB
testcase_51 AC 5 ms
6,820 KB
testcase_52 AC 99 ms
30,984 KB
testcase_53 AC 98 ms
29,956 KB
testcase_54 AC 5 ms
6,816 KB
testcase_55 AC 143 ms
33,708 KB
testcase_56 AC 142 ms
34,188 KB
testcase_57 AC 140 ms
34,184 KB
testcase_58 AC 137 ms
34,188 KB
testcase_59 AC 138 ms
34,616 KB
testcase_60 AC 98 ms
35,940 KB
testcase_61 AC 111 ms
34,664 KB
testcase_62 AC 113 ms
45,012 KB
testcase_63 AC 132 ms
61,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a,bool upp=0){if(a) cout<<(upp?"YES\n":"Yes\n"); else cout<<(upp?"NO\n":"No\n");}
template<class T> void vec_out(vector<T> &p,int ty=0){
if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}

//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 from, to, idx, rev_idx;
    };
    RerootingDP (int _n = 0) : n(_n) { es.resize(n);}
    void add_edge(int u, int v, int idx1, int idx2){
        es[u].push_back({u,v,idx1,idx2});
        es[v].push_back({v,u,idx2,idx1});
    }
    V build(int v = 0){
        root = v;
        vis.resize(n,0);
        outs.resize(n);
        return dfs(root);
    }
    vector<V> reroot(){
        reverse_edge.resize(n);
        reverse_edge[root] = e();
        answers.resize(n);
        bfs(root);
        return answers;
    }
  private:
    int n, root;
    vector<vector<edge>> es;
    vector<int> vis;
    vector<vector<E>> outs;
    vector<E> reverse_edge;
    vector<V> answers;
    V dfs(int v){
        vis[v]++;
        E val = e();
        for (auto &p : es[v]){
            if (vis[p.to] > 0 && p.to != es[v].back().to) swap(p,es[v].back());
            if (vis[p.to] > 0) continue;
            E nval = put_edge(dfs(p.to),p.idx);
            outs[v].emplace_back(nval);
            val = merge(val,nval);
        }
        return put_vertex(val,v);
    }
    void bfs(int v){
        int siz = outs[v].size();
        vector<E> lui(siz+1), rui(siz+1);
        lui[0] = e(), rui[siz] = e();
        for (int i = 0; i < siz; i++) lui[i+1] = merge(lui[i],outs[v][i]);
        for (int i = siz-1; i >= 0; i--) rui[i] = merge(outs[v][i],rui[i+1]);
        for (int i = 0; i < siz; i++){
            reverse_edge[es[v][i].to] = put_edge(put_vertex(merge(merge(lui[i],rui[i+1]),reverse_edge[v]),v),es[v][i].rev_idx);
            bfs(es[v][i].to);
        }
        answers[v] = put_vertex(merge(lui[siz],reverse_edge[v]), v);
    }
};

struct reroot_F{
    int black;
    int white;
    int large;
};

reroot_F merge(reroot_F a, reroot_F b){
    return {a.black+b.black,a.white+b.white,a.large+b.large};
}
reroot_F e(){
    return {0,0,0};
}
reroot_F put_edge(reroot_F v, int i){
    return v;
}
reroot_F put_vertex(reroot_F e, int v){
    e.black=e.white+1;
    e.white=e.large;
    e.large=max(e.black,e.white);
    return e;
}
#define reroot_calc reroot_F,reroot_F,merge,e,put_edge,put_vertex


void solve();
// oddloop
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t=1;
    //cin>>t;
    rep(i,0,t) solve();
}

void solve(){
    int N;
    cin>>N;
    RerootingDP<reroot_calc> G(N);
    rep(i,0,N-1){
        int a,b;
        cin>>a>>b;
        G.add_edge(a-1,b-1,0,0);
    }
    G.build();
    auto res=G.reroot();
    int ans=INF;
    rep(i,0,N) chmin(ans,res[i].black);
    cout<<ans<<"\n";
}
0