Pagini recente » Arhiva de probleme | Rating infoarena | Cod sursa (job #3294032) | Cod sursa (job #2927367) | Cod sursa (job #3294350)
//https://infoarena.ro/problema/disjoint
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("fast-math")
//#pragma GCC optimize ("unroll-loops")
//#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <vector>
//#include <cstring>
//#include <cmath>
//#include <bitset>
//#include <queue>
//#include <utility>
//#include <algorithm>
//#include <string>
//#include <map>
//#include <climits>
//#include <iomanip>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int a[100005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, i, c, x, y;
fin >> n >> m;
for(i = 1; i <= m; ++i)
{
fin >> c >> x >> y;
if (c == 1)
{
int xx = x, yy = y;
while (a[xx])
{
xx = a[xx];
}
while (a[yy])
{
yy = a[yy];
}
a[xx] = yy;
}
else
{
int xx = x, yy = y;
while (a[xx])
{
xx = a[xx];
}
while (a[yy])
{
yy = a[yy];
}
if (xx == yy)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}