Cod sursa(job #1164441)

Utilizator UVS_Elfus_Deneo_KiraUVS-Elfus-Dutzul-Kira UVS_Elfus_Deneo_Kira Data 2 aprilie 2014 08:38:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<fstream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#include<cstring>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<queue>
#define f cin
#define g cout
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll unsigned long long
#define bit 20
#define inf 500000000
#define base 256
#define ba 255
#define N 100100
#define mod  1999999973
#define inu "disjoint.in"
#define outu "disjoint.out"
using namespace std;
ifstream f(inu);
ofstream g(outu);
//int dx[]={0,0,0,1,-1};
//int dy[]={0,1,-1,0,0};
int T[N],R[N],x,y,n,t,m;
int find(int x)
{
    int aux=x;
    while(x!=T[x])
    x=T[x];
    int taso=x;
    x=aux;
    while(x!=taso)
    {
        aux=T[x];
        T[x]=taso;
        x=aux;
    }
    return taso;
}
void unite(int x,int y)
{
    if(R[x]>=R[y])
    {
        T[y]=x;
        if(R[x]==R[y])
        R[x]++;
    }
    else
    T[x]=y;
}
int main ()
{
    f>>n>>m;
    FOR(i,1,n)
    T[i]=i;
    FOR(i,1,m)
    {
        f>>t>>x>>y;
        x=find(x);
        y=find(y);
        if(t==1)
        if(x!=y)
        unite(x,y);
        if(t==2)
        {
        if(x==y)
        g<<"DA\n";
        else
        g<<"NU\n";
        }
    }
    return 0;
}