Pagini recente » Cod sursa (job #710323) | Cod sursa (job #2746022) | Cod sursa (job #745561) | Cod sursa (job #1451032) | Cod sursa (job #2819249)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int inaltimi[100001];
int tati[100001];
int n,m;
int gasire_tata(int x)
{
int tata = x;
while(tata!=tati[tata])
tata = tati[tata];
while(x!=tati[x])
{
x = tati[x];
tati[x] = tata;
}
return tata;
}
void unire(int x,int y)
{
int tata_x = gasire_tata(x);
int tata_y = gasire_tata(y);
if(inaltimi[tata_x]>inaltimi[tata_y])
{
inaltimi[tata_y] = inaltimi[tata_x];
tati[tata_y] = tata_x;
}
else if(inaltimi[tata_x]<inaltimi[tata_y])
{
inaltimi[tata_x] = inaltimi[tata_y];
tati[tata_x] = tata_y;
}
else
{
inaltimi[tata_x]++;
tati[tata_y] = tata_x;
}
}
void aflare(int x,int y)
{
if(gasire_tata(x) == gasire_tata(y))
{
g << "DA\n";
return;
}
g <<"NU\n";
}
void init()
{
for(int i = 1;i<=n;i++)
{
inaltimi[i] = 1;
tati[i] = i;
}
}
int main()
{
f >> n >>m;
init();
for(int i = 1;i<=m;i++)
{
int c,x,y;
f >> c >> x >> y;
if(c == 1)
{
unire(x,y);
}
else
aflare(x,y);
}
}