Cod sursa(job #2669340)

Utilizator martinmiere133Cranga Antonio martinmiere133 Data 6 noiembrie 2020 19:39:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
#include <cstring>
#include <map>
#define INF 1e9
#define NMAX 100001
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int TT[NMAX], RG[NMAX];
int N, M;
int mmax;
int find(int x)
{
    int R, y;
     for (R = x; TT[R] != R; R = TT[R]);
 
    for (; TT[x] != x;)
    {
        y = TT[x];
        TT[x] = R;
        x = y;
    }
    return R;
}
 
void unite(int x, int y)
{    if (RG[x] > RG[y])
        TT[y] = x , mmax = max(RG[x], mmax);
            else TT[x] = y , mmax = max(RG[y], mmax);
    
    if (RG[x] == RG[y]) RG[y]++ , mmax = max(RG[y], mmax);
}
 
int main()
{
    f>>N>>M;
    int i, x, y, cd;
    for (i = 1; i <= N; i++)
    {
        TT[i] = i;
        RG[i] = 1;
    }
    for (i = 1; i <= M; i++)
    {
        f>>cd;
        if (cd == 2){f>>x>>y;
            if (find(x) == find(y)) g<<"DA\n";
                else g<<"NU\n";
        }
            else if(cd == 1)
                {
                    f>>x>>y;
                    if (find(x) != find(y))
                    unite(find(x), find(y));
                }
          
    }
    return 0;
}