Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int main(int argc, char **argv)
- {
- int n, q;
- cin >> n >> q;
- vl xs(n + 1);
- vi fa(n + 1, -1);
- function<int(int)> ufind = [&](int x) {
- if (fa[x] == -1)
- return x;
- int &pa = fa[x];
- int nf = ufind(pa);
- xs[x] += xs[pa];
- return pa = nf;
- };
- auto ujoin = [&](int x, int y, ll w) {
- int fx = ufind(x), fy = ufind(y);
- if (fx == fy) {
- dbg(x, y, xs[x], xs[y], w, fx);
- return xs[x] == xs[y] + w;
- }
- xs[fy] = -xs[y] + xs[x] - w;
- // xs[fy] = -xs[y] + xs[x] + w; why not + w?
- fa[fy] = fx;
- return true;
- };
- vector<int> ans;
- for (int i = 0, a, b, d; i < q; ++i) {
- cin >> a >> b >> d;
- if (ujoin(a, b, ll(d)))
- ans.push_back(i + 1);
- }
- for (auto x : ans)
- cout << x << ' ';
- cout << endl;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement