結果
問題 |
No.2677 Minmax Independent Set
|
ユーザー |
|
提出日時 | 2024-03-15 22:50:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 408 ms / 2,000 ms |
コード長 | 1,742 bytes |
コンパイル時間 | 2,375 ms |
コンパイル使用メモリ | 204,048 KB |
最終ジャッジ日時 | 2025-02-20 05:47:20 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 61 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> Graph(N); for(int i=0; i<N-1; i++){ int u,v; cin >> u >> v; u--; v--; Graph.at(u).push_back(v); Graph.at(v).push_back(u); } if(N == 1){cout << 1 << endl; return 0;} vector<vector<int>> ko1(N),ko2(N); vector<int> par(N,-1); auto d1 = [&](auto d1,int pos,int back) -> pair<int,int> { int w = 0,b = 1; int siz = Graph.at(pos).size(); for(int i=0; i<siz; i++){ int to = Graph.at(pos).at(i); if(to == back){par.at(pos) = i; ko1.at(pos).push_back(0),ko2.at(pos).push_back(0); continue;} auto[kow,kob] = d1(d1,to,pos); ko1.at(pos).push_back(max(kow,kob)),ko2.at(pos).push_back(kow); w += max(kow,kob),b += kow; } return {w,b}; }; d1(d1,0,-1); int answer = 1e9; auto d2 = [&](auto d2,int pos,int back,int pw,int pb) -> void { if(back != -1) ko1.at(pos).at(par.at(pos)) = max(pb,pw), ko2.at(pos).at(par.at(pos)) = pw; int siz = Graph.at(pos).size(); vector<int> l1 = ko1.at(pos),r1 = ko1.at(pos),l2 = ko2.at(pos),r2 = ko2.at(pos); for(int i=1; i<siz; i++) l1.at(i) += l1.at(i-1),l2.at(i) += l2.at(i-1); for(int i=siz-2; i>=0; i--) r1.at(i) += r1.at(i+1),r2.at(i) += r2.at(i+1); answer = min(answer,l2.back()+1); for(int i=0; i<siz; i++){ int to = Graph.at(pos).at(i); if(to == back) continue; int gw = 0,gb = 1; if(i) gw += l1.at(i-1),gb += l2.at(i-1); if(i != siz-1) gw += r1.at(i+1),gb += r2.at(i+1); d2(d2,to,pos,gw,gb); } }; d2(d2,0,-1,0,0); cout << answer << endl; }