1244D - Paint the Tree
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(ll i=a;i<=b;i++) #define dec(i,a,b) for(ll i=a;i>=b;i--) #define forn(i, n) for(ll i = 0; i < int(n); i++) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod = 998244353; const int N = 1e6 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } bool prime(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } inline int qpow(int x, ll n) { int r = 1; while (n > 0) { if (n & 1) r = 1ll * r * x % mod; n >>= 1; x = 1ll * x * x % mod; } return r; } inline int add(int x, int y) { return ((x%mod)+(y%mod))%mod; } inline int sub(int x, int y) { x -= y; return x < 0 ? x += mod : x; } inline int mul(int x, int y) { return (1ll * (x %mod) * (y % mod))%mod; } inline int Inv(int x) { return qpow(x, mod - 2); } int main() { int n; cin >> n; vector<vector<int>> c(3, vector<int>(n + 1)), g(n + 1); rep(i, 0, 2) { rep(j, 1, n) cin >> c[i][j]; } rep(i, 1, n - 1) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int fg = 0,head; vector<int> v1; rep(i, 1, n) { if (g[i].size() > 2) { cout << -1 << endl; return 0; } if (g[i].size() == 1) head = i; } v1.push_back(head); int cur = g[head][0], nxt; rep(i, 1, n - 1) { if (i == n - 1) v1.push_back(cur); for (auto x : g[cur]) { if (x != head) { head = cur; v1.push_back(cur); cur = x; break; } } } int a[6][3] = { {1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1} }; ll res1 = INF, res2; rep(i, 0, 5) { int cnt = 0; ll tmp1 = 0; rep(j, 0, n-1) { tmp1 += c[a[i][cnt++]-1][v1[j]]; cnt %= 3; } if (tmp1 < res1) { res1 = tmp1; res2 = i; } } cout << res1 << endl; vector<int> res(n+1); rep(i, 0, n-1) res[v1[i]]=a[res2][i % 3]; rep(i, 1, n) cout << res[i] << " "; return 0; }