Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. #define FAST_ALLOCATOR_MEMORY 210000000
  10. #define y1 yy11
  11.  
  12. #include "optimization.h"
  13.  
  14. using namespace std;
  15.  
  16. typedef long long ll;
  17. typedef long long llu;
  18. const ll maxn = 163456;
  19.  
  20. ll p[maxn];
  21.  
  22. ll getp(ll i) {
  23.     return (p[i] == i) ? i : (p[i] = getp(p[i]));
  24. }
  25.  
  26. void join(ll a, ll b) {
  27.     a = getp(a), b = getp(b);
  28.     p[a] = b;
  29. }
  30.  
  31. inline int solve() {
  32.     for (int i = 0; i < maxn; ++i) {
  33.         p[i] = i;
  34.     }
  35.     ll n = readInt(), m = readInt(), k = readInt();
  36.     for (int i = 0; i < n; ++i) {
  37.         readInt();
  38.         readInt();
  39.     }
  40.     vector<pair<string, pair<ll, ll>>> qs;
  41.     for (int i = 0; i < k; ++i) {
  42.         char buf[5];
  43.         readWord(buf);
  44.         string a = buf;
  45.         ll b = readInt() - 1, c = readInt() - 1;
  46.         qs.push_back({a, {b, c}});
  47.     }
  48.     vector<string> ans;
  49.     reverse(qs.begin(), qs.end());
  50.     for (auto i : qs) {
  51.         if (i.first == "ask") {
  52.             if (getp(i.second.first) == getp(i.second.second)) {
  53.                 ans.push_back("YES");
  54.             } else {
  55.                 ans.push_back("NO");
  56.             }
  57.         } else {
  58.             join(i.second.first, i.second.second);
  59.         }
  60.     }
  61.     for (auto i = ans.rbegin(); i != ans.rend(); i++) {
  62.         writeWord(i->c_str());
  63.         writeChar('\n');
  64.     }
  65. }
  66.  
  67. int main() {
  68. #ifdef DEBUG
  69.     freopen("input.txt", "r", stdin);
  70. #endif
  71.     ios_base::sync_with_stdio(false);
  72.     cin.tie(0);
  73.     solve();
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement