Cod sursa(job #1214196)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 29 iulie 2014 19:40:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.1 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>

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; }

class DisjointSet {
private:
    int N;
    vector <int> Father;
public:
    DisjointSet() {

    }
    DisjointSet(int N) {
        this->N = N;
        init();
    }
    void init() {
        Father = vector <int> (N + 1);
        for(int i = 1 ; i <= N ; ++ i)
            Father[i] = i;
    }
    int Find(int x) {
        if(Father[x] != x)
            Father[x] = Find(Father[x]);
        return Father[x];
    }
    bool Check(int x, int y) {
        return Find(x) == Find(y);
    }
    void Merge(int x, int y) {
        Father[Find(x)] = Find(y);
    }
} mDisjointSet;

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    int n, m;
    fin >> n >> m;
    mDisjointSet = DisjointSet(n);
    for(int i = 1 ; i <= m ; ++ i) {
        int op, x, y;
        fin >> op >> x >> y;
        switch(op) {
        case 1:
            mDisjointSet.Merge(x, y);
            break;
        case 2:
            if(mDisjointSet.Check(x, y))
                fout << "DA" << '\n';
            else fout << "NU\n";
            break;
        }

    }
    fin.close();
    fout.close();
    return 0;
}