Pagini recente » Cod sursa (job #1176678) | Cod sursa (job #3140408) | Cod sursa (job #3172180) | Cod sursa (job #1248245) | Cod sursa (job #3292635)
#include <fstream>
using namespace std;
ifstream fcin("disjoint.in");
ofstream fout("disjoint.out");
int n,q,c,p[1000001],x,y;
inline int locate(int x)
{
if(p[x]<0) return x;
return p[x]=locate(p[x]);
}
inline void unite(int x, int y, int caz)
{
int px=locate(x);
int py=locate(y);
if(px!=py)
{
if(caz==1)
if(p[px]<p[py])
{
p[px]+=p[py];
p[py]=px;
}
else
{
p[py]+=p[px];
p[px]=py;
}
if(caz==2)
fout<<"NU\n";
}
else
{
if(caz==2)
fout<<"DA\n";
}
}
int main()
{
fcin>>n>>q;
for(int i=0; i<=n; i++)
p[i]=-1;
while(q--)
{
fcin>>c>>x>>y;
if(c==1)
{
unite(x,y,c);
}
if(c==2)
{
unite(x,y,c);
}
}
return 0;
}