Pagini recente » Cei mai harnici utilizatori info-arena | Cod sursa (job #2376096) | Cod sursa (job #3259234) | Cod sursa (job #2536179) | Cod sursa (job #775307)
Cod sursa(job #775307)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
//vector<int> v[200001];
int f[200001];
int find_root(int x)
{
int y = x, z;
while(y!=f[y])
{
y = f[y];
}
// root is y
while(x!=f[x])
{
z = f[x];
f[x] = y;
x = z;
}
return y;
}
int uniune(int x, int y)
{
f[x]=y;
}
int main()
{
int n,m,op,x,y;
fin>>n>>m;
for(int i = 1;i<=n;i++)
f[i] = i;
for(int i=1;i<=m;i++)
{
fin>>op>>x>>y;
if(op == 1)
{
//v[x].push_back(y);
//v[y].push_back(x);
uniune(x,y);
}
else
{
if(find_root(x) == find_root(y))
fout<<"DA";
else fout<<"NU";
fout<<'\n';
}
}
fin.close();
fout.close();
return 0;
}