Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define fore(i, l, r) for(int i = int(l); i < int(r); i++)
- #define forn(i, n) fore(i, 0, n)
- using namespace std;
- #define mp make_pair
- #define pb push_back
- #define sz(a) int((a).size())
- #define all(a) (a).begin(), (a).end()
- #define x first
- #define y second
- typedef long long li;
- typedef long double ld;
- typedef pair<int, int> pt;
- mt19937 rnd(time(NULL));
- template<class A, class B> ostream& operator <<(ostream &out, const pair<A, B> &p) {
- return out << "(" << p.x << ", " << p.y << ")";
- }
- template<class A> ostream& operator <<(ostream &out, const vector<A> &v) {
- out << "[";
- forn(i, sz(v)) {
- if(i) out << ", ";
- out << v[i];
- }
- return out << "]";
- }
- const int INF = int(1e9);
- const li INF64 = li(1e18);
- const ld EPS = 1e-9;
- const int N = 100043;
- int color[N];
- set<int> g[N];
- int n;
- const int C1 = -1;
- const int C2 = -2;
- bool step(queue<pair<int, set<int>::iterator> >& a, vector<int>& cur, int c)
- {
- if(a.empty())
- return false;
- auto k = a.front();
- a.pop();
- if(k.y == g[k.x].end())
- return true;
- int to = *k.y;
- k.y++;
- a.push(mp(k.x, k.y));
- if(color[to] != c)
- {
- color[to] = c;
- cur.pb(to);
- a.push(mp(to, g[to].begin()));
- }
- return true;
- }
- inline bool read() {
- scanf("%d", &n);
- return true;
- }
- char buf[5];
- void ask(int x, int y)
- {
- if(color[x] == color[y])
- puts("YES");
- else
- puts("NO");
- fflush(stdout);
- }
- int last_color = 100043;
- void jfs(int x, int y, bool m)
- {
- queue<pair<int, set<int>::iterator> > q1, q2;
- vector<int> cur1, cur2;
- q1.push(mp(x, g[x].begin()));
- int c1 = color[x];
- color[x] = C1;
- q2.push(mp(y, g[y].begin()));
- int c2 = color[y];
- color[y] = C2;
- cur1.pb(x);
- cur2.pb(y);
- bool ft = true;
- while(true)
- {
- if(!step(q1, cur1, C1))
- {
- ft = true;
- break;
- }
- if(!step(q2, cur2, C2))
- {
- ft = false;
- break;
- }
- }
- if(ft)
- {
- if(m)
- {
- for(auto x : cur2) color[x] = c2;
- for(auto x : cur1) color[x] = c2;
- }
- else
- {
- int cc = last_color++;
- for(auto x : cur2) color[x] = c2;
- for(auto x : cur1) color[x] = cc;
- }
- }
- else
- {
- if(m)
- {
- for(auto x : cur2) color[x] = c1;
- for(auto x : cur1) color[x] = c1;
- }
- else
- {
- int cc = last_color++;
- for(auto x : cur2) color[x] = cc;
- for(auto x : cur1) color[x] = c1;
- }
- }
- }
- void merge(int x, int y)
- {
- jfs(x, y, true);
- g[x].insert(y);
- g[y].insert(x);
- }
- void split(int x, int y)
- {
- g[x].erase(y);
- g[y].erase(x);
- jfs(x, y, false);
- }
- inline void solve() {
- forn(i, n) color[i] = i;
- while(true)
- {
- int x, y;
- scanf("%s", buf);
- string s = buf;
- if(s == "C")
- {
- scanf("%d %d", &x, &y);
- --x;
- --y;
- merge(x, y);
- }
- if(s == "D")
- {
- scanf("%d %d", &x, &y);
- --x;
- --y;
- split(x, y);
- }
- if(s == "E")
- {
- return;
- }
- if(s == "T")
- {
- scanf("%d %d", &x, &y);
- --x;
- --y;
- ask(x, y);
- }
- }
- }
- int main() {
- #ifdef _DEBUG
- // freopen("input.txt", "r", stdin);
- int tt = clock();
- #endif
- cout << fixed << setprecision(12);
- int tc;
- tc = 1;
- while(tc--){
- read();
- solve();
- #ifdef _DEBUG
- cerr << "TIME = " << clock() - tt << endl;
- tt = clock();
- #endif
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement