Pagini recente » Cod sursa (job #698806) | Cod sursa (job #2124417) | Cod sursa (job #1486335) | Cod sursa (job #1638003) | Cod sursa (job #1395896)
#include <fstream>
using namespace std;
const int NMax = 100002;
int rankit[NMax];
int tata[NMax];
//Find Rank and link low to top nodes
int Find(int x)
{
if(tata[x] != x)
{
tata[x] = Find(tata[x]);
}
return tata[x];
}
void Union(int x, int y)
{
if(rankit[x] < rankit[y])
{
tata[x] = y;
}
else if(rankit[x] > rankit[y])
{
tata[y] = x;
}
else
{
rankit[y]++;
tata[x] = y;
}
}
void MakeSet(int x)
{
rankit[x] = 1;
tata[x] = x;
}
int main()
{
int n,m,x,y,i,j;
int op;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for(i = 1; i <= n; i++)
{
MakeSet(i);
}
for(i = 1; i <= m; i++)
{
f>>op>>x>>y;
if(op == 1)
{
Union(Find(x),Find(y));
}
else if(Find(x) == Find(y))
{
g<<"DA\n";
}
else g<<"NU\n";
}
}