Pagini recente » Cod sursa (job #1390591) | Cod sursa (job #2445502) | Cod sursa (job #1934400) | Cod sursa (job #887097) | Cod sursa (job #2328680)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in") ;
ofstream g ("disjoint.out") ;
int N , x , y , M , c , cer , t[100005] , h[100005];
int Find (int x)
{
int r = x ;
while (t[r]) r = t[r];
int y = x;
int t1 ;
while (y != x)
{
swap(t[y] , r) ;
}
return r;
}
void Union (int x , int y)
{
x = Find(x) ;
y = Find(y) ;
if (h[x] < h[y])
{
t[x] = y;
c = y;
}
else
{
t[y] = x;
c = x;
}
if (h[x] == h[y]) h[c] ++ ;
}
int main()
{
f >> N >> M ;
for (int i = 1 ; i <= M ; ++i)
{
f >> cer;
f >> x >> y;
if (cer == 1)
{
Union(x,y) ;
}
else
{
if (Find(x) == Find(y)) g << "DA" ;
else g << "NU";
g << '\n';
}
}
}