Pagini recente » Istoria paginii utilizator/margarita_si_retelele_de_socializare | Cod sursa (job #3170226) | Cod sursa (job #1121259) | Istoria paginii runda/oni2014_ziua_viii | Cod sursa (job #1231681)
#include <iostream>
#include <fstream>
#define nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int t, a, b;
int i, p1, p2;
int P[nmax];
int root(int x)
{
if (P[x] == x)
return x;
return root(P[x]);
}
void init()
{
for (i=1; i<=n; i++)
P[i] = i;
}
void citire()
{
fin >> n >> m;
init();
for (i=1; i<=m; i++)
{
fin >> t >> a >> b;
if (t == 1)
{
P[a] = b;
}
else
{
p1 = root(a);
p2 = root(b);
if (p1 == p2)
fout << "YES\n";
else
fout << "NO\n";
}
}
}
int main()
{
citire();
fin.close();
fout.close();
return 0;
}