Pagini recente » Cod sursa (job #1949548) | Cod sursa (job #295921) | Cod sursa (job #2938678) | Cod sursa (job #2465693) | Cod sursa (job #1884794)
#include <iostream>
#include <fstream>
#define Nmax 100001
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, p[Nmax];
int stramos(int x)
{
while(p[x]!=x)
{
x=p[x];
}
return x;
}
void reuniune(int x, int y)
{
p[stramos(x)]=stramos(y);
}
bool verif(int x, int y)
{
return stramos(x)==stramos(y);
}
int main()
{
f>>n>>m;
for(int i=1; i<=n;i++)
{
p[i]=i;
}
for(int i=1;i<=m;i++)
{
int x,y,z;
f>>x>>y>>z;
if(x==1)
{
reuniune(y, z);
}
else
{
if(verif(y, z))
g<<"DA\n";
else g<<"NU\n";
}
}
return 0;
}