Pagini recente » Cod sursa (job #1579397) | Cod sursa (job #2753443) | Cod sursa (job #1176942) | Cod sursa (job #341668) | Cod sursa (job #2979568)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int a[100010], b[100010];
int n, m;
void init()
{
for (int i=1; i<=n; i++)
{
a[i]=i;
b[i]=1;
}
}
int caut(int x)
{
int r,aux;
for (r=x; a[r]!=r; r=a[r]);
while (a[x]!=x)
{
aux=a[x];
a[x]=r;
x=aux;
}
return r;
}
void reun(int x, int y)
{
if (b[x]>b[y])
a[y]=x;
else
a[x]=y;
if (b[x]==b[y])
b[y]++;
}
void verif(int cod, int x, int y, int i)
{
if (cod==2)
{
if (caut(x)==caut(y))
g<<"DA\n";
else
g<<"NU\n";
}
else
{
if (caut(x)==caut(y))
{
g<<i;
exit(0);
}
reun(caut(x), caut(y));
}
}
void cit()
{
int cod, x, y;
for (int i=1; i<=m; i++)
{
f>>cod>>x>>y;
verif(cod, x, y, i);
}
}
int main()
{
f>>n>>m;
init();
cit();
return 0;
}