Pagini recente » Borderou de evaluare (job #1517309) | Cod sursa (job #2013401) | Cod sursa (job #116198) | Borderou de evaluare (job #1541123) | Cod sursa (job #3328811)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100005;
int n, m;
int t[NMAX];
vector <int> comp[NMAX];
int root(int nod)
{
return t[nod];
}
void add(int x, int y)
{
for(int nod : comp[x])
{
comp[y].push_back(nod);
t[nod]=y;
}
comp[x].clear();
}
bool check(int x, int y)
{
return root(x)==root(y);
}
void unite(int x, int y)
{
x = root(x);
y = root(y);
if(x!=y)
{
if(comp[x].size() > comp[y].size())
swap (x,y);
add(x, y);
}
}
int main()
{
int task,x,y;
fin>>n>>m;
for(int i = 1; i <= n; i++)
{
t[i]=i;
comp[i].push_back(i);
}
for(int i = 1; i <= m; i++)
{
fin >> task >> x >> y;
if (task == 1)
{
unite(x,y);
}
else
{
fout << (check(x,y) ? "DA" : "NU") <<"\n";
}
}
return 0;
}