// // Created by HandwerSTD on 2019/10/27. // Copyright (c) 2019 HandwerSTD. All rights reserved. // Title: Paint the Tree // // sto Qingyu orz // 感谢真神sqy无私的教诲。膜时队者处处阿克,只因大师sqy在他背后。不膜大师者违背了真神的旨意,真神必将降下天谴, // 使其天天爆零 // 我不由自主地膜拜真神sqy。 //
int n, linkTop; longlongint ans = 10000000000000000; longlongint cost[MAXN][4]; int degree[MAXN]; std::vector<int> head[MAXN]; int next[MAXN]; longlongint col[MAXN], fcol[MAXN];
voidDFS(int now, int pre){ for (auto v : head[now]) { if (v == pre) continue; next[now] = v; DFS(v, now); } } voidSearch(int now, int pre, int ppre){ for (; now; now = next[now]) { if (col[ppre] == 1) { if (col[pre] == 2) col[now] = 3; else col[now] = 2; } elseif (col[ppre] == 2) { if (col[pre] == 1) col[now] = 3; else col[now] = 1; } else { if (col[pre] == 1) col[now] = 2; else col[now] = 1; } ppre = pre; pre = now; } longlongint fcost = 0; for (int i = 1; i <= n; ++i) { fcost += cost[i][col[i]]; } if (ans >= fcost) { ans = fcost; for (int i = 1; i <= n; ++i) { fcol[i] = col[i]; } } }
intmain(){ cin >> n; for (int c = 1; c <= 3; ++c) { for (int i = 1; i <= n; ++i) { cin >> cost[i][c]; } } for (int i = 1; i <= n - 1; ++i) { int u = 0, v = 0; scanf("%d %d", &u, &v); head[u].push_back(v); ++degree[u]; head[v].push_back(u); ++degree[v]; } for (int i = 1; i <= n; ++i) { if (degree[i] > 2) return (0 & puts("-1")); if (degree[i] == 1 && !linkTop) linkTop = i; } DFS(linkTop, 0); for (int i = 1; i <= 3; ++i) { col[linkTop] = i; int linkNext = next[linkTop]; for (int j = 1; j <= 3; ++j) { col[linkNext] = j; if (i == j) continue; Search(next[linkNext], linkNext, linkTop); } } cout << ans << endl; for (int i = 1; i <= n; ++i) { cout << fcol[i] << (i == n ? '\n' : ' '); } return0; }