Cod sursa(job #2931980)

Utilizator razvan1403razvan razvan1403 Data 1 noiembrie 2022 13:36:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define d double
#define in int
#define si(x) scanf('%d', &x)
#define sl(x) scanf('%lld', &x)
#define ss(s) scanf('%s', s) 
#define pi(x) printf('%d', x)
#define pl(x) printf('%lld', x)
#define ps(s) printf('%s', s) 
#define pb push_back 
#define mp make_pair 
#define F first 
#define S second 
#define all(x) x.begin(), x.end() 
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x)) 
typedef pair<int, int> pii; 
typedef pair<ll, ll> pl; 
typedef vector<int> vi; 
typedef vector<ll> vl; 
#define modulo 1000000007
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef vector<pii> vpii;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

vector<int>father;
vector<int> height;
int n,k, operatie, x, y;

class Union_Find{
public:

  int Find(int x){
    if(father[x] == 0)
      return x;
    father[x] = Find(father[x]);
    return father[x];
  }

  void Union(int x, int y){
    int rx = Find(x);
    int ry = Find(y);

    if(height[rx] < height[ry]){
      father[rx] = ry;
    }
    else if(height[rx] > height[ry])
        father[ry] = rx;
    else{
        father[ry] = rx;
        height[rx]+=1;
      }
  }

  void menu(){

    fin>>n>>k;
    father = vector<int> (n+1, 0);
    height = vector<int> (n+1, 0);
    for(int i=1;i<=k;++i){
      fin>>operatie>>x>>y;
      if(operatie == 1){
        Union(x,y);
      }
      else{
        if(Find(x) == Find(y))
          fout<<"DA\n";
        else
          fout<<"NU\n";
      }
    }
  }
};

int main() 
{
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  Union_Find x;
  x.menu();
  fin.close();
  fout.close();
  return 0;
}