Pagini recente » Cod sursa (job #510847) | Cod sursa (job #1994809) | Cod sursa (job #1545936) | Monitorul de evaluare | Cod sursa (job #1744079)
///ponderare - O(n log2 n)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n, m, s[100002], h[100002];
int find3(int x)
{
int r=x, j;
while (s[r]!=r)
r=s[r];
int i = x;
while (i!=r)
{
j=s[i];
s[i]=r;
i=j;
}
return i;
}
void reuniune3(int a, int b)
{
if(h[a]==h[b])
{
++h[a];
s[b]=a;
}
else if (h[a]>h[b])
s[b]=a;
else
s[a]=b;
}
int main()
{
int cod, x, y;
ifstream g ("disjoint.in");
ofstream h ("disjoint.out");
g>>n>>m;
for(int i=1;i<=n;++i)
s[i]=i;
for(int i=1;i<=m;++i)
{
g>>cod>>x>>y;
if(cod==1)
reuniune3(find2(x), find2(y));
else
{
if(find2(x)==find2(y))
h<<"DA\n";
else
h<<"NU\n";
}
}
g.close();
h.close();
return 0;
}