Cod sursa(job #1122985)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 25 februarie 2014 21:44:04
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.98 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
#include <cassert>

using namespace std;

const char infile[] = "disjoint.in";
const char outfile[] = "disjoint.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

template <class T>
class DisjointSet {
public:
    DisjointSet(const int &N) {
        this -> N = N;
        this -> father.resize(N);
        for(int i = 0 ; i < N ; ++ i)
            this -> father[i] = i;
    }
    void Join(const int &x, const int &y) {
        int Tx = Find(x);
        int Ty = Find(y);
        assert(Tx != Ty);
        father[Tx] = Ty;
    }
    int Find(const int &Node) {
        if(father[Node] != Node)
            father[Node] = Find(father[Node]);
        return father[Node];
    }
    string Query(const int &x, const int &y) {
        int Tx = Find(x);
        int Ty = Find(y);
        if(Tx == Ty)
            return "DA";
        return "NU";
    }
private:
    vector <int> father;
    int N;
};

int main() {
    int N, M;
    fin >> N >> M;
    DisjointSet <int> dSet(N);
    for(int i = 1 ; i <= M ; ++ i) {
        int tip, x, y;
        fin >> tip >> x >> y;
        if(tip == 1)
            dSet.Join(--x, --y);
        else fout << dSet.Query(--x, --y) << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}