Cod sursa(job #1376949)

Utilizator corul_barbatescUNIBUC Kira96 lockmihai corul_barbatesc Data 5 martie 2015 19:28:32
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
/*
    Those without power, seek us!
    Those with power, fear us!
    We are the Order of the Black Knights!
*/

#include<fstream>
#include<cstdio>
#include<map>
#include<set>
#define FIT(a,b) for(vector<int >::iterator a=b.begin();a!=b.end();a++)
#define FITP(a,b) for(vector<pair<int,int> >::iterator a=b.begin();a!=b.end();a++)
#define RIT(a,b) for(vector<int>::reverse_iterator a=b.end();a!=b.begin();++a)
#include<stack>
#define ROF(a,b,c) for(int a=b;a>=c;--a)
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#define REP(a,b) for(register int a=0;a<b;++a)
#include<cstring>
#include<ctime>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<set>
#define f cin
#define g cout
#include<queue>
#define debug cerr<<"OK";
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned int
#define mod 1000000009LL
#define SQR 350
#define inf 1<<30
#define div fdasfasd
#define hash dsafdsfds
#define od 100003
#define mod  1999999973LL
#define DIM 60010000
#define base 256
#define bas 255
#define N 100100
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
/*
    int dx[]={0,0,0,1,-1};
    int dy[]={0,1,-1,0,0};
*/
int n,m,T[N],t,x,y,R[N];
int find(int x)
{
    int aux=x;
    while(x!=T[x])
        x=T[x];
    int tata=x;
    x=aux;
    while(x!=tata)
    {
        aux=T[x];
        T[x]=tata;
        x=aux;
    }
    return tata;
}
void unite(int x,int y)
{
    if(R[x]>R[y])
        swap(x,y);
    if(R[x]==R[y])
        R[x]++;
    T[y]=x;
}
int main ()
{
    f>>n>>m;
    FOR(i,1,n)
    T[i]=i;
    FOR(i,1,m)
    {
        f>>t;
        if(t==1)
        {
            f>>x>>y;
            x=find(x);
            y=find(y);
            if(x!=y)
                unite(x,y);
        }
        else
        {
            f>>x>>y;
            x=find(x);
            y=find(y);
            if(x==y)
                g<<"DA\n";
            else
                g<<"NU\n";
        }
    }
    return 0;
}