Pagini recente » Monitorul de evaluare | christmas-balls | Cod sursa (job #756069) | Autentificare | Cod sursa (job #3150643)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
struct DSU
{
vector<int>father;
vector<int>size;
DSU(int n)
{
father.resize(n + 1);
size.resize(n + 1);
for(int i = 1; i <= n; ++ i)
{
father[i] = i;
size[i] = 1;
}
}
int FindFather(int x)
{
if(father[x] == x)
return x;
return father[x] = FindFather(father[x]);
}
void Join(int x, int y)
{
int father_unu = FindFather(x);
int father_doi = FindFather(y);
if(size[father_unu] > size[father_doi])
swap(father_unu, father_doi);
father[father_unu] = father_doi;
size[father_doi] += size[father_doi];
}
bool SameSet(int x, int y)
{
return FindFather(x) == FindFather(y);
}
};
const int NMAX = 1e5+5;
int n, m;
DSU pdm(NMAX);
int main()
{
ios::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n >> m;
while(m--)
{
int op, x, y;
fin >> op >> x >> y;
if(op == 1)
{
pdm.Join(x, y);
}
else
{
if(pdm.SameSet(x, y))
fout << "DA\n";
else
fout << "NU\n";
}
}
}