Advertisement
Dang_Quan_10_Tin

UPGRADE (CSPTST 2022-2023)

Oct 4th, 2022 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #define task "UPGRADE"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. struct dsu
  14. {
  15.     int par[N];
  16.     dsu()
  17.     {
  18.         memset(par, -1, sizeof par);
  19.     }
  20.  
  21.     int findpar(int v)
  22.     {
  23.         return par[v] < 0 ? v : par[v] = findpar(par[v]);
  24.     }
  25.  
  26.     bool Merge(int u, int v)
  27.     {
  28.         u = findpar(u);
  29.         v = findpar(v);
  30.  
  31.         if (u == v)
  32.             return false;
  33.  
  34.         if (par[u] < par[v])
  35.             swap(u, v);
  36.  
  37.         par[v] += par[u];
  38.         par[u] = v;
  39.  
  40.         return true;
  41.     }
  42. } f[500];
  43.  
  44. int m, n, w, q, id[N];
  45. struct Edge
  46. {
  47.     int u, v, w;
  48. } e[N];
  49.  
  50. void Read()
  51. {
  52.     cin >> n >> m >> w;
  53.  
  54.     for (int i = 1; i <= m; ++i)
  55.         cin >> e[id[i] = i].u >> e[i].v >> e[i].w;
  56.  
  57.     cin >> q;
  58. }
  59.  
  60. void Solve()
  61. {
  62.     sort(id + 1, id + m + 1, [&](const int &x, const int &y)
  63.          { return e[y].w < e[x].w; });
  64.  
  65.     ll ans(0);
  66.  
  67.     for (int i = 1; i <= m; ++i)
  68.         if (f[0].Merge(e[id[i]].u, e[id[i]].v))
  69.         {
  70.             for (int j = e[id[i]].w; j > 0; --j)
  71.                 f[j - 1].Merge(e[id[i]].u, e[id[i]].v);
  72.  
  73.             ans += e[id[i]].w;
  74.         }
  75.  
  76.     while (q--)
  77.     {
  78.         int x, v;
  79.         cin >> x >> v;
  80.  
  81.         if (v == 0)
  82.         {
  83.             cout << ans << "\n";
  84.             continue;
  85.         }
  86.  
  87.         if (f[e[x].w + v - 1].Merge(e[x].u, e[x].v))
  88.         {
  89.             ans += e[x].w + v;
  90.  
  91.             for (int i = e[x].w + v - 1; i > 0; --i)
  92.                 if (!f[i - 1].Merge(e[x].u, e[x].v))
  93.                 {
  94.                     ans -= i;
  95.                     break;
  96.                 }
  97.         }
  98.         e[x].w += v;
  99.  
  100.         cout << ans << "\n";
  101.     }
  102. }
  103.  
  104. int32_t main()
  105. {
  106.     ios_base::sync_with_stdio(0);
  107.     cin.tie(0);
  108.     cout.tie(0);
  109.  
  110.     if (fopen(task ".INP", "r"))
  111.     {
  112.         freopen(task ".INP", "r", stdin);
  113.         freopen(task ".OUT", "w", stdout);
  114.     }
  115.  
  116.     Read();
  117.     Solve();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement