Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define TASK "LABYRINTH"
- using namespace std;
- const int N = 1e5;
- bool unlocked[N + 1];
- vector<int> Adj[N+1];
- int n, m, k, x, y, Trace[N + 1];
- char type;
- void read()
- {
- cin >> n >> m >> k;
- fill(unlocked, unlocked + n + 1, false);
- fill(Trace, Trace + n + 1, -1);
- for (int i = 1; i <= m; ++ i)
- {
- cin >> x >> y;
- Adj[x].push_back(y);
- Adj[y].push_back(x);
- }
- }
- int FindSet(int u)
- {
- if (Trace[u] < 0)
- {
- return u;
- }
- return Trace[u] = FindSet(Trace[u]);
- }
- void Unite(int r, int s)
- {
- if (Trace[s] < Trace[r])
- {
- swap(r, s);
- }
- Trace[r] += Trace[s];
- Trace[s] = r;
- }
- void solve()
- {
- while (k--)
- {
- cin >> type;
- if (type == 'X')
- {
- cin >> x;
- unlocked[x] = true;
- for (int v : Adj[x])
- {
- if (unlocked[v])
- {
- int r = FindSet(x), s = FindSet(v);
- if (r != s)
- {
- Unite(r, s);
- }
- }
- }
- }
- else
- {
- cin >> x >> y;
- int u = FindSet(x), v = FindSet(y);
- if (u == v)
- {
- cout << "Y";
- }
- else
- {
- cout << "N";
- }
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- freopen(TASK".INP", "r", stdin);
- freopen(TASK".OUT", "w", stdout);
- int t = 1;
- bool typetest = false;
- if (typetest)
- {
- cin >> t;
- }
- for (int __ = 1; __ <= t; ++ __)
- {
- read();
- solve();
- }
- }
Add Comment
Please, Sign In to add comment