SHARE
TWEET

Untitled

lalalalalalalaalalla Oct 13th, 2019 (edited) 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <tuple>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <numeric>
  12. #include <map>
  13. #include <bitset>
  14. #include <set>
  15. #include <stack>
  16. #include <queue>
  17. #include <unordered_set>
  18. #include <cassert>
  19.  
  20.  
  21. //#pragma GCC optimize("Ofast,no-stack-protector")
  22. //#pragma GCC optimize("O3")
  23. //#pragma GCC target("avx2")
  24. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
  25. //#pragma GCC optimize("unroll-loops")
  26. //#pragma GCC optimize("fast-math")
  27. //#pragma GCC optimize("section-anchors")
  28. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  29. //#pragma GCC optimize("vpt")
  30. //#pragma GCC optimize("rename-registers")
  31. //#pragma GCC optimize("move-loop-invariants")
  32. //#pragma GCC optimize("unswitch-loops")
  33. //#pragma GCC optimize("function-sections")
  34. //#pragma GCC optimize("data-sections")
  35. //#pragma GCC optimize("branch-target-load-optimize")
  36. //#pragma GCC optimize("branch-target-load-optimize2")
  37. //#pragma GCC optimize("btr-bb-exclusive")
  38. //#pragma GCC optimize("O0")
  39.  
  40.  
  41. #define int long long
  42. #define ll long long
  43. #define ull unsigned long long
  44. #define all(a) a.begin(), a.end()
  45. #define pii pair<int, int>
  46. #define pb push_back
  47. #define ld long double
  48.  
  49.  
  50. using namespace std;
  51.  
  52. const int INF = 9223372036854775806;
  53. //const int mod = 2600000069;
  54. //const int p = 179;
  55.  
  56. vector<vector<int>> g, c;
  57. vector<int> perm(3), answer, color;
  58. int cur, ans = INF;
  59.  
  60. void dfs(int v, int ind) {
  61.     color[v] = perm[ind];
  62.     cur += c[perm[ind]][v];
  63.     for (auto u : g[v]) {
  64.         if (color[u] == -1) {
  65.             dfs(u, (ind + 1) % 3);
  66.         }
  67.     }
  68. }
  69.  
  70. signed main() {
  71.     ios_base::sync_with_stdio(0);
  72.     cin.tie(0);
  73.     cout.tie(0);
  74.     int n;
  75.     cin >> n;
  76.     g.resize(n);
  77.     c.assign(3, vector<int>(n));
  78.     answer.resize(n);
  79.     color.assign(n, 0);
  80.     vector<int> pw(n, 0);
  81.     for (int i = 0; i < 3; i++) {
  82.         for (int j = 0; j < n; j++) cin >> c[i][j];
  83.     }
  84.     int u, v;
  85.     for (int i = 0; i < n-1; i++) {
  86.         cin >> u >> v;
  87.         u--;v--;
  88.         g[u].pb(v);
  89.         g[v].pb(u);
  90.         pw[u]++;
  91.         pw[v]++;
  92.         if (pw[u] > 2 || pw[v] > 2) {
  93.             cout << -1;
  94.             return 0;
  95.         }
  96.     }
  97.     int start = 0;
  98.     for (int i =0 ; i < n; i++)  {
  99.         if (pw[i] == 1) {start = i; break;}
  100.     }
  101.     for (int f = 0; f < 3; f++) {
  102.         for (int s = 0; s < 3; s++) {
  103.             if (f == s) continue;
  104.             for (int t = 0; t < 3; t++) {
  105.                 if (t == s || t ==f ) continue;
  106. //                cout << f << " " << s << " " << t << "\n";
  107.                 perm[0] = f;
  108.                 perm[1] = s;
  109.                 perm[2] = t;
  110.                 cur = 0;
  111.                 color.assign(n, -1);
  112.                 dfs(start, 0);
  113.                 if (cur < ans) {
  114.                     ans = cur;
  115.                     answer = color;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.     cout << ans << "\n";
  121.     for (auto k : answer) {cout << k + 1 << " ";}
  122. }
  123. /*
  124.  
  125. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top