QUPC2018 G - Tapu & Tapi 2
部分点
たぷとたぴが連結にならないようにする→カットなので最小カットを求めればよい。N<=500ならば最大フローを求めるアルゴリズムで間に合うのでdinicなどのライブラリを貼ればよい。
満点解法
木DPをする。dp[i][j] = (頂点iの部分木でiと連結な成分に{両方いない、たぷがいる、たぴがいる}(=j)のときの辺を切断する最小コスト) とする。cをiの子、iとcの辺の重みをwとする。c以下の部分木が条件を満たさないならiとcの辺を切断しなければならない。逆に条件を満たすなら辺を切断する必要はない。したがって、このDPの遷移は
- dp[i][両方いない] = min(dp[c][両方いない], dp[c][たぷがいる] + w, dp[c][たぴがいる] + w) (頂点iにはたぷもたぴもいない)
- dp[i][たぷがいる] = min(dp[c][両方いない], dp[c][たぷがいる], dp[c][たぴがいる] + w) (頂点iにたぴはいない)
- dp[i][たぴがいる] = min(dp[c][両方いない], dp[c][たぷがいる] + w, dp[c][たぴがいる]) (頂点iにたぷはいない)
となる。これを木DPとして葉から計算していけばよい。
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll using PII = pair<int, int>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; } template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); } template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); } template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t, const V &v) { t=v; } template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); } template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.first<<','<<a.second<<')'; return out; } template<typename T> istream& operator >> (istream& is, vector<T>& vec){ for(T& x: vec) {is >> x;} return is; } template<class T> ostream &operator <<(ostream& out,const vector<T>& a){ out<<'['; for(T i: a) {out<<i<<',';} out<<']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL const int INF = 1<<30; const ll LLINF = 1LL<<60; const int MOD = 1000000007; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, x, y; cin >> n >> x >> y; vector<vector<pair<ll,ll>>> g(n); REP(i, n-1) { int a, b, c; cin >> a >> b >> c; a--, b--; g[a].push_back({b, c}); g[b].push_back({a, c}); } vector<ll> exist_a(n), exist_b(n); REP(i, x) { int p; cin >> p, p--, exist_a[p] = 1; } REP(i, y) { int q; cin >> q, q--, exist_b[q] = 1; } // dp[i][mask] = (頂点i以下の部分木の連結成分にたぷ、たぴを含んでいる状況がmaskのときのminコスト) auto dp = make_v<ll>(n, 3); fill_v(dp, LLINF); function<void(int,int)> dfs = [&](int v, int p) { if(g[v].size()==1 && p!=-1) { if(exist_a[v]) { dp[v][1] = 0; } else if(exist_b[v]) { dp[v][2] = 0; } else { dp[v][0] = 0; } return; } if(!exist_a[v] && !exist_b[v]) dp[v][0] = 0; if(!exist_b[v]) dp[v][1] = 0; if(!exist_a[v]) dp[v][2] = 0; for(auto to: g[v]) { if(to.first == p) continue; dfs(to.first, v); if(!exist_a[v] && !exist_b[v]) { dp[v][0] += min({dp[to.first][0], dp[to.first][1]+to.second, dp[to.first][2]+to.second}); } if(!exist_b[v]) { dp[v][1] += min({dp[to.first][0], dp[to.first][1], dp[to.first][2]+to.second}); } if(!exist_a[v]) { dp[v][2] += min({dp[to.first][0], dp[to.first][1]+to.second, dp[to.first][2]}); } } }; dfs(0, -1); cout << min({dp[0][0], dp[0][1], dp[0][2]}) << endl; return 0; }