Pagini recente » Cod sursa (job #2804882) | Cod sursa (job #895214) | Cod sursa (job #529711) | Cod sursa (job #1370922) | Cod sursa (job #2551901)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100010;
int n,m;
int p[NMAX];
void initializare()
{
fin>>n>>m;
for (int i=1; i<=n; i++)
p[i]=i;
}
int findSet(int i)
{
if (p[i]==i)
return i;
return p[i]=findSet(p[i]);
}
bool isSameSet(int a,int b)
{
int x=findSet(a),y=findSet(b);
return x==y;
}
void unionSet(int a,int b)
{
int x=findSet(a),y=findSet(b);
if (x<y)
p[x]=y;
else
p[y]=x;
}
void rezolvare()
{
int a,b,c;
for(int i=1; i<=m; i++){
fin>>c>>a>>b;
switch (c){
case 1:
unionSet(a,b);
break;
case 2:
if (isSameSet(a,b))
fout<<"DA\n";
else
fout<<"NU\n";
break;
}
}
}
int main()
{
initializare();
rezolvare();
fin.close();
fout.close();
return 0;
}