1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #include <bits/stdc++.h> using namespace std; const int MAXN = 10100; const int MAXM = 200100; typedef pair<int, int> pii; struct UFS { int f[MAXN]; stack<pii> s; int finds(int x) { while (x ^ f[x]) x = f[x]; return x; } void unite(int x, int y) { x = finds(x); y = finds(y); if (x != y) { s.push({x, f[x]}); f[x] = y; } } void init(int b, int e) { for (int i = b; i < e; i++) f[i] = i; } void undo() { f[s.top().first] = s.top().second; s.pop(); } }; struct SegTree { vector<pii> data[MAXM << 2]; static inline int lson(int k) { return k << 1; } static inline int rson(int k) { return (k << 1) | 1; } static inline int fat(int l, int r) { return (l + r) >> 1; } void add(int k, int l, int r, int x, int y, const pii &value) { if (l == x && r == y) { data[k].push_back(value); return; } int mid = fat(l, r); if (y <= mid) { add(lson(k), l, mid, x, y, value); } else if (x > mid) { add(rson(k), mid + 1, r, x, y, value); } else { add(lson(k), l, mid, x, mid, value); add(rson(k), mid + 1, r, mid + 1, y, value); } } }; UFS ufs; SegTree segTree; vector<pair<pii, int> > que; int tar;
bool dfs(int k, int l, int r) { rejudge: int target = que[tar].second; if (target == r && l == r) { if (ufs.finds(que[tar].first.first) == ufs.finds(que[tar].first.second)) cout << "Yes" << endl; else cout << "No" << endl; tar++; return tar == que.size(); } int mid = SegTree::fat(l, r); if (target <= mid) {
for (int i = 0; i < segTree.data[SegTree::lson(k)].size(); ++i) ufs.unite(segTree.data[SegTree::lson(k)][i].first, segTree.data[SegTree::lson(k)][i].second); if (dfs(SegTree::lson(k), l, mid)) return true;
for (int i = 0; i < segTree.data[SegTree::lson(k)].size(); ++i) ufs.undo(); if (que[tar].second > r) return false; goto rejudge; } else {
for (int i = 0; i < segTree.data[SegTree::rson(k)].size(); ++i) ufs.unite(segTree.data[SegTree::rson(k)][i].first, segTree.data[SegTree::rson(k)][i].second); if (dfs(SegTree::rson(k), mid + 1, r)) return true;
for (int i = 0; i < segTree.data[SegTree::rson(k)].size(); ++i) ufs.undo(); if (que[tar].second > r) return false; goto rejudge; } } void solve() { int n, m; cin >> n >> m; map<pii, int> mp; for (int i = 0; i < m; ++i) { string s; int u, v; cin >> s >> u >> v; if (u > v) swap(u, v); switch (s[0]) { case 'Q':
que.push_back(make_pair(make_pair(u, v), i + 1)); break; case 'C':
mp.insert(make_pair(make_pair(u, v), i + 1)); break; case 'D': {
map<pii, int>::iterator iter = mp.find(make_pair(u, v));
segTree.add(1, 1, m, iter->second, i, make_pair(u, v)); mp.erase(iter); break; } } } map<pii, int>::iterator iter = mp.begin(); while (iter != mp.end()) { segTree.add(1, 1, m, iter->second, m, iter->first); iter++; } ufs.init(0, n + 1); tar = 0;
for (int i = 0; i < segTree.data[1].size(); ++i) ufs.unite(segTree.data[1][i].first, segTree.data[1][i].second);
dfs(1, 1, m); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef ACM_LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long long test_index_for_debug = 1; char acm_local_for_debug; while (cin >> acm_local_for_debug) { if (acm_local_for_debug == '$') exit(0); cin.putback(acm_local_for_debug); if (test_index_for_debug > 20) { throw runtime_error("Check the stdin!!!"); } auto start_clock_for_debug = clock(); solve(); auto end_clock_for_debug = clock(); cout << "Test " << test_index_for_debug << " successful" << endl; cerr << "Test " << test_index_for_debug++ << " Run Time: " << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl; cout << "--------------------------------------------------" << endl; } #else solve(); #endif return 0; }
|