f for A
f[u][0] as the min-time to conquer subtree u, u-th city is conquered by A and the start node of all the most nearly continuous-conquer path from each son had not been considered yet
f[u][1] as the min-time to conquer subtree u, u-th city is conquered by A and the start node of all the most nearly continuous-conquer path from each son had been considered
the same as g for B
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
using namespace std;
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (200)
#define INFI (0x7fffffff)
struct Edge
{
int v, next;
};
vector<Edge> E;
int L[MAXN];
void G_ini()
{
E.clear();
memset(L, -1, sizeof L);
}
void AE(int u, int v)
{
Edge te = {v, L[u]};
L[u] = E.size();
E.push_back(te);
}
int n, ta[MAXN], tb[MAXN], f[MAXN][2], g[MAXN][2];
bool vis[MAXN];
void TDP(int u)
{
vis[u] = 1;
bool lef = 1;
int ami = INFI, bmi = INFI; //a/bmi as the minimum increment value to chose a start point given by all path from son
for (int i = L[u]; i != -1; i = E[i].next)
{
int v = E[i].v;
if (vis[v]) continue;
lef = 0;
TDP(v);
int tma = min(f[v][0], g[v][1]),
tmb = min(g[v][0], f[v][1]);
ami = min(ami, f[v][1] - tma);
bmi = min(bmi, g[v][1] - tmb);
f[u][0] += tma;
g[u][0] += tmb;
}
if (lef)
{
f[u][1] = ta[u];
g[u][1] = tb[u];
f[u][0] = ta[u] >> 1;
g[u][0] = tb[u] >> 1;
}
else
{
f[u][1] = f[u][0] + min(ta[u], ta[u] / 2 + ami);
g[u][1] = g[u][0] + min(tb[u], tb[u] / 2 + bmi);
f[u][0] += ta[u] >> 1;
g[u][0] += tb[u] >> 1;
}
}
int main()
{
while (~scanf("%d", &n))
{
G_ini();
Rep(i, 0, n) scanf("%d", ta + i);
Rep(i, 0, n) scanf("%d", tb + i);
Rep(i, 0, n - 1)
{
int u, v;
scanf("%d%d", &u, &v);
u--; v--;
AE(u, v);
AE(v, u);
}
memset(vis, 0, sizeof vis);
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
TDP(0);
printf("%d\n", min(f[0][1], g[0][1]));
}
return 0;
}
it's a liittle complicated :{