Pagini recente » Cod sursa (job #1078149) | Cod sursa (job #1379601) | Cod sursa (job #2677655) | Cod sursa (job #427509) | Cod sursa (job #1136165)
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <random>
#include <assert.h>
using namespace std;
const string file = "disjoint";
const string infile = file + ".in";
const string outfile = file + ".out";
const int INF = 0x3f3f3f3f;
//#define ONLINE_JUDGE
vector<int> tata;
vector<int> rang;
int Find(int x)
{
int t = x;
for(;tata[t] != 0; t = tata[t]);
int tprec;
for(int i = x; tata[i] != 0; i = tprec)
{
tprec = tata[i];
tata[i] = t;
}
return t;
}
void Union(int x, int y)
{
if(x == y)
return;
if(rang[x] < rang[y])
swap(x, y);
rang[x] += rang[y];
tata[y] = x;
}
int main()
{
#ifdef ONLINE_JUDGE
ostream &fout = cout;
istream &fin = cin;
#else
fstream fin(infile.c_str(), ios::in);
fstream fout(outfile.c_str(), ios::out);
#endif
int N, M;
fin >> N >> M;
tata.resize(N + 1, 0);
rang.resize(N + 1, 1);
for(int i = 0; i < M; i++)
{
int op, x, y;
fin >> op >> x >> y;
if(op == 1)
{
int a = Find(x);
int b = Find(y);
Union(a, b);
}
else if(op == 2)
{
int a = Find(x);
int b = Find(y);
fout << (a == b ? "DA" : "NU") << "\n";
}
}
#ifdef ONLINE_JUDGE
#else
fout.close();
fin.close();
#endif
}