Pagini recente » Cod sursa (job #2328617) | Istoria paginii runda/tt/clasament | Istoria paginii runda/simulare-cartita-13/clasament | Cod sursa (job #1077988) | Cod sursa (job #1888431)
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <fstream>
#define nmax 100001
#include <cstring>
using namespace std;
int tata[nmax];
int h[nmax];
void unire (int x, int y)
{
if (h[x]>h[y]) tata[y]=x;
else {tata[x]=y;
if (h[x]==h[y]) h[y]++;}
}
int gasire (int x)
{
int r=x;
while (tata[r]) r=tata[r];
int y=x, t;
while (y!=r)
{
t=tata[y];
tata[y]=r;
y=t;
}
return r;
}
int main()
{
memset(h,1,sizeof(h));
int n, m, i, q, a, b;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f >> n >> m;
for (i=1; i<=m; ++i)
{
f >> q >> a >> b;
if (q==1)
{ if (gasire(a)!=gasire(b)) unire(gasire(a),gasire(b));}
else {
if (gasire(a)==gasire(b)) g <<"DA\n";
else g << "NU\n";}
}
return 0;
}