Pagini recente » Monitorul de evaluare | cenzura | Clasament rating | Cod sursa (job #781656) | Cod sursa (job #532092)
Cod sursa(job #532092)
#include <fstream>
#define nmax 100002
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[nmax],h[nmax];
int find (int x)
{
int r=x,aux,t;
while (tata[r]) r=tata[r];
aux=x;
while (aux!=r) //avem mai multi auxiliari t,aux
{
t=tata[aux];
tata[aux]=r;
aux=t;
}
return r;
}
void unite (int x,int y)
{
if (h[x]>h[y])
tata[y]=x;
else tata[x]=y;
if (h[x]==h[y]) h[y]++; //fiind egale tata[x]=y;
}
int main ()
{
int n,t,cod,x,y;
f>>n>>t;
for (int i=1; i<=n; i++)
h[i]=1; //inaltimea multimii cu tatal in i
for (int i=1; i<=t; i++)
{
f>>cod>>x>>y;
if (cod==1)
unite(find(x),find(y));
else
if (find(x)==find(y))
g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
f.close (); g.close ();
return 0;
}