Pagini recente » Cod sursa (job #1025836) | Cod sursa (job #1061324) | Cod sursa (job #2217992) | Cod sursa (job #1722190) | Cod sursa (job #2696467)
#include <bits/stdc++.h>
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()
{ cin.tie(NULL);
ios::sync_with_stdio(false);
in>>N>>M;
for(int i=1; i<=M; i++)
{ int x,y,cod;
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(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;
}