Pagini recente » Cod sursa (job #2035309) | Cod sursa (job #1699421) | Cod sursa (job #298080) | Cod sursa (job #2057819) | Cod sursa (job #2924739)
#include <iostream>
#include <fstream>
#include <algorithm>
///#include <tryhardmode>
///#include <GODMODE::ON>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NMAX=1e5+5;
int t[NMAX];
int p[NMAX];
int root(int x)
{
if(t[x]==x)
return x;
return t[x]=root(t[x]);
}
void solve(int x,int y)
{
x=root(x);
y=root(y);
if(p[x]>p[y])
swap(x,y);
t[y]=x;
p[x]=p[x]+p[y];
}
void get_edges(int x,int y)
{
if(root(x)!=root(y))
solve(x,y);
}
int main()
{
int n,m,i,j,tip,x,y;
fin>>n>>m;
for(i=1;i<=n;i++)
{
t[i]=i;
p[i]=1;
}
for(i=1;i<=m;i++)
{
fin>>tip>>x>>y;
if(tip==1)
get_edges(x,y);
else
if(root(x)==root(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}