Pagini recente » Cod sursa (job #1491730) | Cod sursa (job #611394) | Cod sursa (job #2419693) | Cod sursa (job #1168467) | Cod sursa (job #544721)
Cod sursa(job #544721)
#include<fstream>
#define dmax 100010
using namespace std;
int n,m;
int tata[dmax],rang[dmax];
int find(int x)
{
if (x != tata[x])
tata[x] = find(tata[x]);
return tata[x];
}
void uneste(int x, int y)
{
if (rang[x] > rang[y])
{
tata[y] = x;
rang[y]++;
} else
{
tata[x] = y;
rang[x]++;
}
}
void reuniune(int x, int y)
{
uneste (find(x), find(y));
}
void citire()
{
int i,op,x,y;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin>>n>>m;
for (i=1; i<=n; i++)
tata[i] = i;
for (i=1; i<=m; i++)
{
fin>>op>>x>>y;
if (op == 1)
reuniune(x, y); else
if (find(x) == find(y))
fout<<"DA"<<'\n'; else
fout<<"NU"<<'\n';
}
fin.close();
fout.close();
}
int main()
{
citire();
return 0;
}