Pagini recente » Cod sursa (job #2611108) | Cod sursa (job #3224904) | Cod sursa (job #1721903) | Cod sursa (job #1717989) | Cod sursa (job #904421)
Cod sursa(job #904421)
#include <fstream>
using namespace std;
int n, m;
int ord[100005];
void Unite(int x, int y)
{
if(ord[y] < ord[x])
{
ord[y] += ord[x];
ord[x] = y;
}
else
{
ord[x] += ord[y];
ord[y] = x;
}
}
int Find(int x)
{
int y, r = x;
while(ord[r] > 0)
r = ord[r];
while(ord[x] > 0)
{
y = ord[x];
ord[x] = r;
x = y;
}
return r;
}
int main ()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int i,cod,x,y;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> cod >> x >> y;
if(cod == 1)
Unite(Find(x),Find(y));
else
if(Find(x) == Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
fin.close();
fout.close();
return 0;
}