Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Make CSP great again
- #include <bits/stdc++.h>
- #define TASK "TESTCODE"
- using namespace std;
- const int N = 1e5;
- int lab[N + 1], p[N + 1], n, m;
- vector<int> Query[2];
- int FindSet(int u)
- {
- if (lab[u] < 0)
- {
- return u;
- }
- return lab[u] = FindSet(lab[u]);
- }
- void read()
- {
- memset(lab, -1, sizeof(lab));
- cin >> n >> m;
- Query[0].push_back(0);
- Query[1].push_back(0);
- }
- bool check(int a, int num)
- {
- int u = Query[0][num], v = Query[1][num];
- while (true)
- {
- if (u == a)
- {
- return true;
- }
- if (u == v)
- {
- return false;
- }
- u = p[u];
- }
- }
- void solve()
- {
- for (int i = 1; i <= m; ++ i)
- {
- int type;
- cin >> type;
- if (type == 1)
- {
- int u, v;
- cin >> u >> v;
- lab[u] = v;
- p[u] = v;
- }
- if (type == 2)
- {
- int u; cin >> u;
- Query[0].push_back(u);
- Query[1].push_back(FindSet(u));
- }
- if (type == 3)
- {
- int a, num;
- cin >> a >> num;
- if (check(a, num))
- {
- cout << "YES";
- }
- else
- {
- cout << "NO";
- }
- 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; ++ __)
- {
- //cout << "Case " << __ << ": ";
- read();
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement