Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- //#define ull unsigned long long
- #define pii pair <int, int>
- #define vi vector <int>
- #define mi vector <vector <int>>
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define sz(a) (int)(a.size())
- #define pb push_back
- #define mp make_pair
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define EACH(e, a) for (auto& e : a)
- // Pragmas
- #ifndef TIOM4EG
- #pragma GCC optimize("O3") // let the chaos begin!
- #pragma GCC target("avx,avx2,tune=native")
- #pragma GCC comment(linker, "/stack:200000000")
- #endif
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define pbds tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- using namespace __gnu_pbds;
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- mt19937 rng2(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
- //#define int long long
- constexpr int INF = 1e9 + 7, MD = 998244353, MAX = 100007, LG = 19, R = 1 << LG, MOD = 1000000007, MOD2 = 1e9 + 9, B = 256;
- const ll INFLL = 1e18 + 7;
- pii edg[MAX];
- set <int> comp[8];
- int col[MAX];
- int p[MAX];
- int get(int v) {
- while (p[v] != v) v = p[v];
- return v;
- }
- const int THR = 20;
- int used[MAX];
- vi gr[MAX];
- void dfs(int v) {
- used[v] = 1;
- EACH(u, gr[v]) if (!used[u]) dfs(u);
- }
- vi sm;
- bool found;
- void rec(int i) {
- if (found) return;
- if (i == sz(sm)) {
- found = 1;
- return;
- }
- EACH(e, comp[sm[i]]) {
- int u = get(edg[e].F), v = get(edg[e].S);
- if (u != v) {
- p[u] = v;
- rec(i + 1);
- p[u] = u;
- }
- }
- }
- signed main() {
- fastIO;
- int n, m, k; cin >> n >> m >> k;
- FOR(i, 0, n) p[i] = i;
- FOR(i, 0, m) {
- int u, v, c; cin >> u >> v >> c, --u, --v, --c;
- edg[i] = {u, v};
- comp[c].insert(i), col[i] = c;
- gr[u].pb(v), gr[v].pb(u);
- }
- dfs(0);
- bool govno = 0;
- FOR(i, 0, n) govno |= !used[i];
- int q; cin >> q;
- if (n - 1 < k || govno) {
- FOR(i, 0, q) cout << "No\n";
- return 0;
- }
- while (q--) {
- int i, c; cin >> i >> c, --i, --c;
- if (col[i] != c) {
- comp[col[i]].erase(i);
- col[i] = c;
- comp[col[i]].insert(i);
- }
- bool bad = 0;
- sm.clear();
- FOR(c, 0, k) {
- if (comp[c].empty()) {
- cout << "No\n";
- bad = 1;
- break;
- }
- if (sz(comp[c]) <= THR) sm.pb(c);
- }
- if (bad) continue;
- if (sz(sm) <= 1) {
- cout << "Yes\n";
- continue;
- }
- sort(all(sm), [&comp](int i, int j){ return sz(comp[i]) < sz(comp[j]); });
- found = 0;
- rec(0);
- cout << (found ? "Yes\n" : "No\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment