Pagini recente » Cod sursa (job #2509812) | Cod sursa (job #1916534) | tema | Cod sursa (job #1894886) | Cod sursa (job #1850054)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m, viz[1001], cod, x, y;
int TT[100010], h[100010];
void citire()
{
fin >> n >> m;
for(int i=1;i<=n;i++)
{
TT[i]=i;
h[i]=0;
}
}
int root(int z)
{
int n;
for(n=z; TT[n]!=n; n=TT[n]);
return n;
}
int main()
{
citire();
while(m--)
{
fin >> cod >> x >> y;
if(cod==1)
{
if(h[root(x)]>h[root(y)])
{
TT[y]=root(x);
}
if(h[root(x)]<h[root(y)])
{
TT[x] = root(y);
}
if(h[root(x)] == h[root(y)])
{
TT[x] = root(y);
h[root(y)]++;
}
}
else if(cod==2)
{
if(root(x) == root(y))
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
return 0;
}