1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>pii; typedef double db; const int maxn=1e3+5; const ll mod=998244353; const double eps=1e-10; const int inf=1e9; int n,rt[2],q[2],flag=1; int a[maxn]; int cmd[2][maxn],sum[2][maxn],col[2][maxn]; vector<int>e[2][maxn];
struct Edge{ int from,to,cap,flow,cost; Edge(int u,int v,int c,int f,int co):from(u),to(v),cap(c),flow(f),cost(co){} };
struct MCMF{ int n,m,s,t; ll ans; vector<Edge> edges; vector<int> g[maxn]; int inq[maxn]; int d[maxn]; int p[maxn]; int a[maxn];
void init(int n){ this->n=n; for(int i=0;i<n;++i) g[i].clear(); edges.clear(); s=0,t=n-1; ans=0; }
void add(int from,int to,int cap,int cost){ edges.push_back(Edge(from,to,cap,0,cost)); edges.push_back(Edge(to,from,0,0,-cost)); m=edges.size(); g[from].push_back(m-2); g[to].push_back(m-1); }
bool BellmanFord(int s,int t,int& flow,long long& cost){ for(int i=0;i<n;++i) d[i]=inf; memset(inq,0,sizeof(inq)); d[s]=0; inq[s]=1; p[s]=0; a[s]=inf;
queue<int> que; que.push(s); while(!que.empty()){ int u=que.front(); que.pop(); inq[u]=0; for(int i=0;i<g[u].size();++i){ Edge& e=edges[g[u][i]]; if(e.cap>e.flow && d[e.to]>d[u]+e.cost){ d[e.to]=d[u]+e.cost; p[e.to]=g[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]){ que.push(e.to);inq[e.to]=1; } } } } if(d[t]==inf) return false; flow+=a[t]; cost+=(long long)d[t]*(long long)a[t]; for(int u=t;u!=s;u=edges[p[u]].from){ edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; } return true; }
int MincostMaxflow(int s,int t,long long& cost){ int flow=0; cost=0; while(BellmanFord(s,t,flow,cost)); return flow; }
ll check(){ int pre=0,le=0,ri=0; for(int i=0;i<g[s].size();i++){ Edge e=edges[g[s][i]]; le+=e.cap; } for(int i=0;i<g[t].size();i++){ Edge e=edges[g[t][i]^1]; ri+=e.cap; } if(le!=ri)return -1;
MincostMaxflow(s,t,ans); le=0; for(int i=0;i<g[s].size();i++){ Edge e=edges[g[s][i]]; le+=e.flow; }
if(le!=ri)return -1;
return -ans; }
}G;
void dfs(int sgn,int u,int p,int cur){ if(cmd[sgn][u])cur=u; col[sgn][u]=cur; for(auto v:e[sgn][u]){ if(v==p)continue; dfs(sgn,v,u,cur); sum[sgn][u]+=sum[sgn][v]; } if(cmd[sgn][u]){ int pre=cmd[sgn][u]; cmd[sgn][u]-=sum[sgn][u]; sum[sgn][u]=pre; if(cmd[sgn][u]<0)flag=0; } }
void build(){ G.init(n*2+2); for(int i=1;i<=n;i++){ G.add(col[0][i],col[1][i]+n,1,-a[i]); } for(int i=1;i<=n;i++){ if(cmd[0][i]){ G.add(0,i,cmd[0][i],0); } if(cmd[1][i]){ G.add(i+n,2*n+1,cmd[1][i],0); } } } int main(){ scanf("%d%d%d",&n,&rt[0],&rt[1]); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); e[0][u].push_back(v); e[0][v].push_back(u); } for(int i=1;i<n;i++){ int u,v; scanf("%d%d",&u,&v); e[1][u].push_back(v); e[1][v].push_back(u); } scanf("%d",&q[0]); for(int i=1;i<=q[0];i++){ int k,x; scanf("%d%d",&k,&x); cmd[0][k]=x; } scanf("%d",&q[1]); for(int i=1;i<=q[1];i++){ int k,x; scanf("%d%d",&k,&x); cmd[1][k]=x; } for(int i=0;i<2;i++) dfs(i,rt[i],0,rt[i]); build(); if(!flag)puts("-1"); else printf("%lld\n",G.check()); return 0; }
|