Pagini recente » Cod sursa (job #1039547) | Cod sursa (job #2158661) | Monitorul de evaluare | Cod sursa (job #1774881) | Cod sursa (job #946616)
Cod sursa(job #946616)
#include <fstream>
#define NMAX 100004
using namespace std;
const char iname[] = "disjoint.in";
const char oname[] = "disjoint.out";
ifstream fin(iname);
ofstream fout(oname);
int M, c, a, b;
int t[NMAX];
inline int find(int x)
{
if (t[x] != x) t[x] = find(t[x]);
return t[x];
}
inline int uneste(int x, int y)
{
x = find(x); y = find(y);
if (x == y) return 0;
t[x] = y;
return 1;
}
int main()
{
fin >> M;
for (a = 1; a <= M; ++a) t[a] = a;
fin >> M;
while (M--)
{
fin >> c >> a >> b;
if (c & 1)
uneste(a, b);
else{
if (find(a) != find(b)) fout << "NU" << '\n';
else fout << "DA" << '\n';
}
}
return 0;
}