Pagini recente » Cod sursa (job #2686647) | Cod sursa (job #599481) | Cod sursa (job #2468315) | Cod sursa (job #2392426) | Cod sursa (job #2352835)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define NMax 100020
int N, M;
int TT[NMax], RG[NMax];
int finds(int x)
{
int R, y;
for(R = x; TT[R]!=R; R = TT[R]);
for(; TT[x]!= x ;)
{
y = TT[x];
TT[x] = R;
x = y;
}
return R;
}
void unite(int x, int y)
{
if(RG[x] > RG[y])
TT[y] = x;
else
TT[x] = y;
if(RG[x] == RG[y])
RG[y]++;
}
int main()
{
fin >> N >> M;
int i, x, y, cd;
for(int i = 1; i<=N; i++)
{
TT[i] = i;
RG[i] = 1;
}
for(int i = 1; i<=M; i++)
{
fin >> cd >> x >> y;
if(cd == 2)
{
if(finds(x) == finds(y)) fout <<"DA\n";
else fout <<"NU\n";
}
else
{
unite(finds(x), finds(y));
}
}
}