Advertisement
MinhNGUYEN2k4

c4tree

Sep 21st, 2021
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. //Nguyen Huu Hoang Minh
  2. #include <bits/stdc++.h>
  3. #define sz(x) int(x.size())
  4. #define all(x) x.begin(),x.end()
  5. #define reset(x) memset(x, 0,sizeof(x))
  6. #define pb push_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define N 10005
  11. #define remain(x) if (x > MOD) x -= MOD
  12. #define ii pair<int, int>
  13. #define iiii pair< ii , ii >
  14. #define viiii vector< iiii >
  15. #define vi vector<int>
  16. #define vii vector< ii >
  17. #define bit(x, i) (((x) >> (i)) & 1)
  18. #define Task "test"
  19. #define int long long
  20.  
  21. using namespace std;
  22.  
  23. typedef long double ld;
  24. const int inf = 1e10;
  25. const int minf = -1e10;
  26. const int maxc = 4;
  27.  
  28. int n;
  29. vector<int> g[N];
  30. int dp[N][5];
  31. int par[N], r[N];
  32.  
  33. void readfile()
  34. {
  35.     ios_base::sync_with_stdio(false);
  36.     cin.tie(0);cout.tie(0);
  37.     if (fopen(Task".inp","r"))
  38.     {
  39.         freopen(Task".inp","r",stdin);
  40.         //freopen(Task".out","w",stdout);
  41.     }
  42.     cin >> n;
  43.     for(int i=1; i<n; i++){
  44.         int u, v;
  45.         cin >> u >> v;
  46.         g[u].pb(v);
  47.         g[v].pb(u);
  48.     }
  49. }
  50.  
  51. void dfs(int u, int pre){
  52.     for(int i=1; i<=4; i++) dp[u][i] = i;
  53.     for(auto v : g[u]){
  54.         if (v!=pre){
  55.             par[v] = u;
  56.             dfs(v,u);
  57.             for(int i=1; i<=4; i++){
  58.                 int t = -1;
  59.                 for(int j=1; j<=4; j++){
  60.                     if (j!=i && (t<0||dp[v][t]>dp[v][j])) t=j;
  61.                 }
  62.                 dp[u][i]+=dp[v][t];
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void trace(int u, int pre, int c){
  69.     r[u]=c;
  70.     for(auto v: g[u]){
  71.         if (v!=pre){
  72.             int t = -1;
  73.             for(int i=1; i<=4; i++) if (i!=c && ( t<0 || dp[v][t]>dp[v][i])) t=i;
  74.             trace(v,u,t);
  75.         }
  76.     }
  77. }
  78.  
  79. void proc()
  80. {
  81.     dfs(1,1);
  82.     int j=1;
  83.     for(int i=1; i<=maxc; i++) if (dp[1][j]>dp[1][i]) j=i;
  84.     trace(1,1,j);
  85.     cout << dp[1][j] << '\n';
  86.     for(int i=1; i<=n; i++) cout << r[i] << '\n';
  87. }
  88.  
  89. signed main()
  90. {
  91.     readfile();
  92.     proc();
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement