Pagini recente » Cod sursa (job #2152598) | Cod sursa (job #1147461) | Cod sursa (job #1808758) | Cod sursa (job #2334649) | Cod sursa (job #1814931)
#include <iostream>
#include <fstream>
#define MAX 100001
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[MAX],rang[MAX],n,m,z;
void citire();
bool unire(int x,int y);
int findT(int x);
int main()
{
citire();
return 0;
}
void citire()
{
int x,y;
in>>n>>m;
for (int i=1;i<=n;i++) tata[i]=i;
for (int i=1;i<=m;i++)
{
in>>z>>x>>y;
if (z==1)
{
bool t=unire(x,y);
}
else
{
if (unire(x,y)) out<<"DA \n";
else out<<"NU \n";
}
}
}
int findT(int x)
{
if (x == tata[x]) return x;
else
{
int tati = findT(tata[x]);
tata[x] = tati;
return tati;
}
}
bool unire(int x,int y)
{
int tataX = findT(x);
int tataY = findT(y);
if (tataX != tataY)
{
if (z==1)
{
if (rang[tataX] > rang[tataY])
{
tata[tataY] = tataX;
}
else
{
if (rang[tataX] < rang[tataY])
{
tata[tataX] = tataY;
}
else
{
tata[tataX] = tataY;
rang[tataY]++;
}
}
return false;
}
else
{
return false;
}
}
return true;
}