tiom4eg

openol E 100

Jan 16th, 2024
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. //#define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define pbds tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. using namespace __gnu_pbds;
  37. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  38. using namespace std;
  39. mt19937 rng2(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  40. //#define int long long
  41. constexpr int INF = 1e9 + 7, MD = 998244353, MAX = 100007, LG = 19, R = 1 << LG, MOD = 1000000007, MOD2 = 1e9 + 9, B = 256;
  42. const ll INFLL = 1e18 + 7;
  43.  
  44. pii edg[MAX];
  45. set <int> comp[8];
  46. int col[MAX];
  47.  
  48. int p[MAX];
  49.  
  50. int get(int v) {
  51.     while (p[v] != v) v = p[v];
  52.     return v;
  53. }
  54.  
  55. const int THR = 20;
  56.  
  57. int used[MAX];
  58. vi gr[MAX];
  59. void dfs(int v) {
  60.     used[v] = 1;
  61.     EACH(u, gr[v]) if (!used[u]) dfs(u);
  62. }
  63.  
  64. vi sm;
  65. bool found;
  66.  
  67. void rec(int i) {
  68.     if (found) return;
  69.     if (i == sz(sm)) {
  70.         found = 1;
  71.         return;
  72.     }
  73.     EACH(e, comp[sm[i]]) {
  74.         int u = get(edg[e].F), v = get(edg[e].S);
  75.         if (u != v) {
  76.             p[u] = v;
  77.             rec(i + 1);
  78.             p[u] = u;
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84. signed main() {
  85.     fastIO;
  86.     int n, m, k; cin >> n >> m >> k;
  87.     FOR(i, 0, n) p[i] = i;
  88.     FOR(i, 0, m) {
  89.         int u, v, c; cin >> u >> v >> c, --u, --v, --c;
  90.         edg[i] = {u, v};
  91.         comp[c].insert(i), col[i] = c;
  92.         gr[u].pb(v), gr[v].pb(u);
  93.     }
  94.     dfs(0);
  95.     bool govno = 0;
  96.     FOR(i, 0, n) govno |= !used[i];
  97.     int q; cin >> q;
  98.     if (n - 1 < k || govno) {
  99.         FOR(i, 0, q) cout << "No\n";
  100.         return 0;
  101.     }
  102.     while (q--) {
  103.         int i, c; cin >> i >> c, --i, --c;
  104.         if (col[i] != c) {
  105.             comp[col[i]].erase(i);
  106.             col[i] = c;
  107.             comp[col[i]].insert(i);
  108.         }
  109.         bool bad = 0;
  110.         sm.clear();
  111.         FOR(c, 0, k) {
  112.             if (comp[c].empty()) {
  113.                 cout << "No\n";
  114.                 bad = 1;
  115.                 break;
  116.             }
  117.             if (sz(comp[c]) <= THR) sm.pb(c);
  118.         }
  119.         if (bad) continue;
  120.         if (sz(sm) <= 1) {
  121.             cout << "Yes\n";
  122.             continue;
  123.         }
  124.         sort(all(sm), [&comp](int i, int j){ return sz(comp[i]) < sz(comp[j]); });
  125.         found = 0;
  126.         rec(0);
  127.         cout << (found ? "Yes\n" : "No\n");
  128.     }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment