Pagini recente » Cod sursa (job #1218177) | Cod sursa (job #805102) | Cod sursa (job #1426004) | Cod sursa (job #411335) | Cod sursa (job #742299)
Cod sursa(job #742299)
#include<fstream>
#define nmax 100002
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int T[nmax], R[nmax];
int i, n, tip, x, y, m ;
int find(int x)
{
int R,y;
for(R = x; T[R] != R; R = T[R]);
for( ; x != T[x]; )
{
y = T[x];
T[x] = R;
x = y;
}
return R;
}
void unit(int x,int y)
{
if(R[x]> R[y])
T[y] = x;
else
T[x] = y;
if(R[x] == R[y])
R[x]++;
}
void rez()
{
int i;
fin >> n >>m ;
for(i = 1; i <= n; i++)
{
R[i] = 1;
T[i] = i;
}
for(i = 1; i <= m; i++)
{
fin >> tip >> x >> y;
if(tip == 1)
{
unit(find(x), find(y));
}
else
if(find(x) == find(y))
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
int main()
{
rez();
fin.close();
return 0;
}