Pagini recente » Cod sursa (job #3126535) | Cod sursa (job #1340592) | Cod sursa (job #761267) | Cod sursa (job #642086) | Cod sursa (job #2696463)
#include <fstream>
#include <unordered_map>
using namespace std;
int N,M;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
unordered_map< int ,pair <int,int>>Map;
int stramos(int x)
{ unordered_map<int ,pair <int,int>>::iterator it;
it=Map.find(x);
if(it==Map.end())
return 0;
while(x!=Map[x].first)
x=Map[x].first;
return x;
}
int main()
{ in>>N>>M;
int x,y,cod;
for(int i=1;i<=M;i++)
{ in>>cod>>x>>y;
if(cod==1)
{ /// comanda de unificare
int sx=stramos(x);
int sy=stramos(y);
if(sx==0)
{ Map[x]={x,1};
sx=x;
}
if(sy==0)
{ Map[y]={y,1};
sy=y;
}
if(sx!=sy)
if(Map[sx].second<=Map[sy].second)
{
Map[sx].first=sy;
Map[sy].second+=Map[sx].second;
}
else
{
Map[sy].first=sx;
Map[sx].second+=Map[sy].second;
}
}
else
{ int sx,sy;
sx=stramos(x);
sy=stramos(y);
if(sy==sx&&sy!=0)
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
in.close();
out.close();
return 0;
}