Advertisement
pb_jiang

abc328f ac

Nov 16th, 2023
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     int n, q;
  18.     cin >> n >> q;
  19.     vl xs(n + 1);
  20.     vi fa(n + 1, -1);
  21.     function<int(int)> ufind = [&](int x) {
  22.         if (fa[x] == -1)
  23.             return x;
  24.         int &pa = fa[x];
  25.         int nf = ufind(pa);
  26.         xs[x] += xs[pa];
  27.         return pa = nf;
  28.     };
  29.     auto ujoin = [&](int x, int y, ll w) {
  30.         int fx = ufind(x), fy = ufind(y);
  31.         if (fx == fy) {
  32.             dbg(x, y, xs[x], xs[y], w, fx);
  33.             return xs[x] == xs[y] + w;
  34.         }
  35.         xs[fy] = -xs[y] + xs[x] - w;
  36.         // xs[fy] = -xs[y] + xs[x] + w; why not + w?
  37.         fa[fy] = fx;
  38.         return true;
  39.     };
  40.     vector<int> ans;
  41.     for (int i = 0, a, b, d; i < q; ++i) {
  42.         cin >> a >> b >> d;
  43.         if (ujoin(a, b, ll(d)))
  44.             ans.push_back(i + 1);
  45.     }
  46.     for (auto x : ans)
  47.         cout << x << ' ';
  48.     cout << endl;
  49.     return 0;
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement