Pagini recente » Cod sursa (job #2788520) | Cod sursa (job #2106113) | Cod sursa (job #1931329) | Cod sursa (job #3153265) | Cod sursa (job #2454052)
//============================================================================
// Name : test.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX=100005;
int d[NMAX],h[NMAX];
int stramos(int x){
if(d[x]==0)
return x;
int r=stramos(d[x]);
d[x]=r;
return r;
}
bool aceasi_componenta(int x,int y){
return stramos(x)==stramos(y);
}
void unire(int x,int y){
x=stramos(x);
y=stramos(y);
if(x==y)
return;
if(h[x]==h[y]){
h[x]++;
d[y]=x;
}
else if(h[x]>h[y]){
d[y]=x;
}
else
d[x]=y;
}
int main() {
int x,y,cod,m,n;
in>>n>>m;
for(int i=1;i<=m;i++){
in>>cod>>x>>y;
if(cod==1)
{
unire(x,y);
}
if(cod==2)
{
if(aceasi_componenta(x,y))
out<<"DA\n";
else
out<<"NU\n";
}
}
//out<<"DA\n";
return 0;
}