Pagini recente » Cod sursa (job #1638484) | Cod sursa (job #79984) | Cod sursa (job #2988924) | Cod sursa (job #1165178) | Cod sursa (job #639386)
Cod sursa(job #639386)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAXN = 100005;
int TT[MAXN] , N , M;
int find(int x)
{
int aux , R;
for(R = x;TT[R] != R;R = TT[R]);
for(;x !=TT[x];)
{
aux = TT[x];
TT[x] = R;
x = aux;
}
return R;
}
void unite(int x,int y)
{
TT[y] = x;
}
int main()
{
fin>>N>>M;
for(int i=1;i<=N;++i)
TT[i] = i;
for(;M;M--)
{
int x , y , tip;
fin>>tip>>x>>y;
if(tip==1) unite(x,y);
else
if(find(x)==find(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}