Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 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, 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.     LL x = 0;
  49.  
  50.     if (gr)
  51.         x += g[v].cost;
  52.  
  53.     if (!gr)
  54.     {
  55.         for (auto i = g[v].neighbours.begin(); i != g[v].neighbours.end(); i++)
  56.         {
  57.             if (*i == a)
  58.                 continue;
  59.  
  60.             x += dfs(g, *i, v, !gr);
  61.         }
  62.     }
  63.     else
  64.     {
  65.         for (auto i = g[v].neighbours.begin(); i != g[v].neighbours.end(); i++)
  66.         {
  67.             if (*i == a)
  68.                 continue;
  69.  
  70.             LL a = dfs(g, *i, v, !gr);
  71.             LL b = dfs(g, *i, v, gr);
  72.  
  73.             a < b ? x += a : x += b;
  74.         }
  75.     }
  76.  
  77.     return g[v].results[gr] = x;
  78. }
  79.  
  80. void guards(vector<vertex>& g, vector<bool>& iG, int v, int a, bool gr)
  81. {
  82.     iG[v] = gr;
  83.  
  84.     if (gr)
  85.     {
  86.         for (int i = 0; i < g[v].neighbours.size(); i++)
  87.         {
  88.             if (i == a)
  89.                 continue;
  90.  
  91.             if (g[(i)].results[0] < g[(i)].results[1])
  92.                 guards(g, iG, i, a, false);
  93.             else
  94.                 guards(g, iG, i, a, true);
  95.         }
  96.     }
  97.     else
  98.     {
  99.         for (int i = 0; i < g[v].neighbours.size(); i++)
  100.         {
  101.             if (i == a)
  102.                 continue;
  103.  
  104.             guards(g, iG, i, a, true);
  105.         }
  106.     }
  107. }
  108.  
  109. void output(vector<bool>& g, LL r, int size)
  110. {
  111.     cout << r << endl;
  112.  
  113.     for (int i = 1; i < size; i++)
  114.         cout << g[i];
  115.  
  116.     cout << endl;
  117. }
  118.  
  119. int main()
  120. {
  121.     int z;
  122.     cin >> z;
  123.  
  124.     while (z--)
  125.     {
  126.         vector<vertex> graph;
  127.         vector<bool> ifGuarded1, ifGuarded2;
  128.         LL result1, result2;
  129.  
  130.         input(graph);
  131.  
  132.         ifGuarded1.assign(graph.size(), false);
  133.         ifGuarded2.assign(graph.size(), false);
  134.  
  135.         result1 = dfs(graph, 1, 0, true);
  136.         guards(graph, ifGuarded1, 1, 0, true);
  137.         result2 = dfs(graph, 1, 0, false);
  138.         guards(graph, ifGuarded2, 1, 0, false);
  139.  
  140.         if (result1 < result2)
  141.             output(ifGuarded1, result1, graph.size());
  142.         else
  143.             output(ifGuarded2, result2, graph.size());
  144.     }
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement