感觉有些题解的建图方式过于抽象,提供一个正常人能想出来的做法。
先把两棵树的限制分开。假如现在只有一棵树,由于限制是关于子树的,也就是只有包含和无交两种关系,于是我们可以把所有对子树的限制,转化为对若干个不交连通块的限制,且这些连通块的并集为 。
转化后的第 条限制形如 ,其中 表示被限制的点集,并且必须从这个点集内选恰好 个点。所有限制的 的并集为 。
把所有限制转化后,再把两棵树放在一起。考虑如下建图( 表示 向 连一条容量为 ,费用为 的边):
- 建立超级源点 以及超级汇点 。
- 对第一棵树的每条限制 新建点 。
- 对第二棵树的每条限制 新建点 。
- 对每个点 分别建两个点 表示分别在两棵树中。
- 对第一棵树的每条限制 ,连边 ;对 ,连边 。
- 对每个点 ,连边 。
- 对第二棵树的每条限制 ,连边 ;对 ,连边 。
直接求 到 的最大费用最大流,如果最大流没有达到所有限制的点数之和则无解;否则答案就是最大费用。
不难理解连边方式正确。
// Problem: E. Politics
// Contest: Codeforces - Codeforces Round 523 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1061/E
// Memory Limit: 256 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define R(i, x, y) for (int i = (x); i >= (y); i--)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
bool Mbe;
struct Flow {
const int inf = 0x3f3f3f3f;
int n, s, t, tot;
struct edge { int to, nxt, w, c; };
vector<int> hd, cr, d, vs;
vector<edge> e;
Flow () { }
Flow (int _n) : n(_n), tot(-1) { hd.resize(n + 5, -1), d.resize(n + 5), vs.resize(n + 5), e.clear(); }
void add_edge(int u, int v, int w, int c) { e.eb((edge) { v, hd[u], w, c }), hd[u] = ++tot; }
void add_flow(int u, int v, int w, int c) { add_edge(u, v, w, c), add_edge(v, u, 0, -c); }
bool spfa() {
fill(d.begin(), d.end(), -inf);
queue<int> q; q.push(s), d[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop(), vs[u] = 0;
for (int i = hd[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if (!e[i].w) continue;
if (d[v] < d[u] + e[i].c) {
d[v] = d[u] + e[i].c;
if (!vs[v]) q.push(v), vs[v] = 1;
}
}
}
return d[t] != -inf;
}
int dfs(int u, int in) {
if (u == t) return in;
vs[u] = 1;
int out = 0;
for (int &i = cr[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if (e[i].w && !vs[v] && d[v] == d[u] + e[i].c) {
int res = dfs(v, min(in, e[i].w));
e[i].w -= res, e[i ^ 1].w += res;
in -= res, out += res;
}
if (!in) break;
}
vs[u] = 0;
return out;
}
pi dinic() {
int mc = 0, mf = 0;
while (spfa()) {
cr = hd;
int res = dfs(s, inf);
mc += res * d[t], mf += res;
}
return mp(mf, mc);
}
};
const int N = 505;
const int M = 2e3 + 5;
int n, q1, q2;
int a[N];
struct Tree {
int rt, res, lim[N];
vector<int> G[N], p;
vector<pair<int, vector<int>>> q;
void add(int u, int v) { G[u].eb(v), G[v].eb(u); }
void gt(int u, int f) {
p.eb(u);
for (int v : G[u]) {
if (v == f) continue;
if (!lim[v]) gt(v, u);
else res -= lim[v];
}
}
void dfs(int u, int f) {
if (lim[u]) {
p.clear(), res = lim[u], gt(u, f);
if (res < 0) cout << -1 << '\n', exit(0);
q.eb(mp(res, p));
}
for (int v : G[u])
if (v != f) dfs(v, u);
}
} T1, T2;
void solve() {
cin >> n >> T1.rt >> T2.rt;
F (i, 1, n) cin >> a[i];
F (i, 1, n - 1) { int u, v; cin >> u >> v, T1.add(u, v); }
F (i, 1, n - 1) { int u, v; cin >> u >> v, T2.add(u, v); }
cin >> q1; F (i, 1, q1) { int x, y; cin >> x >> y, T1.lim[x] = y; }
cin >> q2; F (i, 1, q2) { int x, y; cin >> x >> y, T2.lim[x] = y; }
int _ = T1.lim[T1.rt];
if (T2.lim[T2.rt] != _) return cout << -1 << '\n', void();
T1.dfs(T1.rt, 0), T2.dfs(T2.rt, 0);
Flow G(2 * n + q1 + q2 + 2); G.s = 2 * n + q1 + q2 + 1, G.t = 2 * n + q1 + q2 + 2;
int cur = 2 * n;
for (auto p : T1.q) {
int lim = p.fi;
vector<int> nd = p.se;
cur++, G.add_flow(G.s, cur, lim, 0);
for (int u : nd) G.add_flow(cur, u, 1, 0);
}
for (auto p : T2.q) {
int lim = p.fi;
vector<int> nd = p.se;
cur++, G.add_flow(cur, G.t, lim, 0);
for (int u : nd) G.add_flow(u + n, cur, 1, 0);
}
F (i, 1, n) G.add_flow(i, i + n, 1, a[i]);
pi res = G.dinic();
if (res.fi != _) cout << -1 << '\n';
else cout << res.se << '\n';
}
bool Med;
int main() {
// FIO("");
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cerr << (&Mbe - &Med) / 1048576.0 << " MB\n";
int T = 1;
// cin >> T;
while (T--) solve();
cerr << (int)(1e3 * clock() / CLOCKS_PER_SEC) << " ms\n";
return 0;
}