Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. //triangulacja == przykro mi
  6.  
  7. using namespace std;
  8.  
  9. typedef long long LL;
  10.  
  11. struct vertex
  12. {
  13.     vector<int> neighbours;
  14.     int cost;
  15.     vector<LL> results = { -1, -1 };
  16. };
  17.  
  18. void input(vector<vertex>& g)
  19. {
  20.     int n;
  21.     cin >> n;
  22.  
  23.     g.assign(n + 1, vertex());
  24.  
  25.     for (int i = 1; i < n + 1; i++)
  26.     {
  27.         int x;
  28.         cin >> x;
  29.  
  30.         g[i].cost = x;
  31.     }
  32.  
  33.     for (int i = 0; i < n - 1; i++)
  34.     {
  35.         int x, y;
  36.         cin >> x >> y;
  37.  
  38.         g[x].neighbours.push_back(y);
  39.         g[y].neighbours.push_back(x);
  40.     }
  41. }
  42.  
  43. LL dfs(vector<vertex>& g, vector<bool>& iG, int v, int a, bool gr) //a - ancestor, gr - guarded
  44. {
  45.     if (g[v].results[gr] != -1)
  46.         return g[v].results[gr];
  47.  
  48.     if (v == 1)
  49.         iG[v] = gr;
  50.  
  51.     LL x = 0;
  52.  
  53.     if (gr)
  54.         x += g[v].cost;
  55.    
  56.     if (!gr)
  57.     {
  58.         for (auto i = g[v].neighbours.begin(); i != g[v].neighbours.end(); i++)
  59.         {
  60.             if (*i == a)
  61.                 continue;
  62.  
  63.             x += dfs(g, iG, *i, v, !gr);
  64.  
  65.             iG[v] = 1 - gr;
  66.         }
  67.     }
  68.     else
  69.     {
  70.         for (auto i = g[v].neighbours.begin(); i != g[v].neighbours.end(); i++)
  71.         {
  72.             if (*i == a)
  73.                 continue;
  74.  
  75.             LL a = dfs(g, iG, *i, v, !gr);
  76.             LL b = dfs(g, iG, *i, v, gr);
  77.  
  78.             if (a < b)
  79.             {
  80.                 iG[v] = 1 - gr;
  81.                 x += a;
  82.             }
  83.             else
  84.             {
  85.                 iG[v] = gr;
  86.                 x += b;
  87.             }
  88.         }
  89.     }
  90.  
  91.     return g[v].results[gr] = x;
  92. }
  93.  
  94. void output(vector<bool>& g, LL r, int size)
  95. {
  96.     cout << r << endl;
  97.  
  98.     for (int i = 1; i < size; i++)
  99.         cout << g[i];
  100.  
  101.     cout << endl;
  102. }
  103.  
  104. int main()
  105. {
  106.     int z;
  107.     cin >> z;
  108.  
  109.     while (z--)
  110.     {
  111.         vector<vertex> graph;
  112.         vector<bool> ifGuarded1, ifGuarded2;
  113.         LL result1, result2;
  114.                            
  115.         input(graph);
  116.  
  117.         ifGuarded1.assign(graph.size(), false);
  118.         ifGuarded2.assign(graph.size(), false);
  119.  
  120.         result1 = dfs(graph, ifGuarded1, 1, 0, true);
  121.        
  122.         result2 = dfs(graph, ifGuarded2, 1, 0, false);
  123.  
  124.         if (result1 < result2)
  125.             output(ifGuarded1, result1, graph.size());
  126.         else
  127.             output(ifGuarded2, result2, graph.size());
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement