Untitled

 avatar
unknown
c_cpp
a year ago
3.7 kB
6
Indexable
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x...)
#endif

using ll = long long;

vector<pair<int*, int>> hist;

struct dsus {
    int size;
    vector<int> p, s;

    dsus(int n) {
        size = n;
        p.resize(size);
        iota(p.begin(), p.end(), 0);
        s.assign(size, 1);
    }

    int leader(int x) {
        return (x == p[x] ? x : p[x] = leader(p[x]));
    }

    bool merge(int a, int b) {
        a = leader(a);
        b = leader(b);
        if (a == b) return false;
        if (s[a] < s[b]) {
            swap(a, b);
        }
        p[b] = a;
        s[a] += s[b];
        return true;
    }

    int sz(int x) {
        return s[leader(x)];
    }
};

struct dsur {
    int size;
    vector<int> p, r;

    dsur(int n) {
        size = n;
        p.assign(size, -1);
        r.assign(size, 0);
    }

    int leader(int x) {
        while (p[x] != -1) {
            x = p[x];
        }
        return x;
    }

    bool same(int a, int b) {
        return leader(a) == leader(b);
    }

    void merge(int a, int b) {
        a = leader(a);
        b = leader(b);
        if (a == b) return;
        if (r[a] > r[b]) {
            hist.emplace_back(&p[b], p[b]);
            p[b] = a;
        } else if (r[a] < r[b]) {
            hist.emplace_back(&p[a], p[a]);
            p[a] = b;
        } else {
            hist.emplace_back(&p[a], p[a]);
            hist.emplace_back(&r[b], r[b]);
            p[a] = b;
            ++r[b];
        }
    }
};

void test_case() {
    int n, m, k;
    cin >> n >> m >> k;
    dsus D(n);
    vector<array<int, 3>> edges(m);
    vector<int> colors(k);
    vector<vector<int>> cnt(n, vector<int> (8));
    vector<set<int>> edc(k);
    for (int i = 0; i < m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        u--; v--; c--;
        edges[i] = {u, v, c};
        edc[c].insert(i);
        colors[c]++;
        cnt[u][c]++;
        cnt[v][c]++;
        D.merge(u, v);
    }
    int q;
    cin >> q;
    if (D.sz(0) != n) {
        for (int i = 0; i < q; i++) {
            cout << "No\n";
        }
        return;
    }
    dsur R(n);
    vector<int> por(k);
    iota(por.begin(), por.end(), 0);
    auto check = [&](auto self, int c, int t) -> bool {
        if (c >= k) return true;
        if (colors[por[c]] >= t) return true;
        for (int e : edc[por[c]]) {
            if (!R.same(edges[e][0], edges[e][1])) {
                int start = hist.size();
                R.merge(edges[e][0], edges[e][1]);
                bool f = self(self, c + 1, t);
                while ((int)hist.size() > start) {
                    *hist.back().first = hist.back().second;
                    hist.pop_back();
                }
                if (f) return true;
            }
        }
        return false;
    };
    while (q--) {
        int e, w;
        cin >> e >> w;
        e--; w--;
        auto [u, v, c] = edges[e];
        colors[c]--;
        colors[w]++;
        cnt[u][c]--;
        cnt[v][c]--;
        cnt[u][w]++;
        cnt[v][w]++;
        edc[c].erase(e);
        edc[w].insert(e);
        edges[e][2] = w;
        sort(por.begin(), por.end(), [&](int a, int b) {
            return colors[a] < colors[b];
        });
        if (check(check, 0, 10)) {
            cout << "Yes\n";
        } else {
            cout << "No\n";
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tests = 1;
    // cin >> tests;
    while (tests--) {
        test_case();
    }
    return 0;
}
Editor is loading...
Leave a Comment