Pagini recente » Cod sursa (job #1645767) | Cod sursa (job #1424074) | Cod sursa (job #1325005) | Cod sursa (job #1004819) | Cod sursa (job #2947914)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m;
int inaltime[100001];
int tata[100001];
int reprez(int x)
{
if(tata[x] == x)
return x;
tata[x] = reprez(tata[x]);
return tata[x];
}
void reuniune(int reprezx,int reprezy)
{
if(inaltime[reprezx] > inaltime[reprezy])
tata[reprezy] = reprezy;
else
{
tata[reprezx] = reprezy;
if(inaltime[reprezx] == inaltime[reprezy])
inaltime[reprezy]++;
}
}
void afisare(int reprezx,int reprezy)
{
if(reprezx == reprezy)
g << "DA" << endl;
else
g << "NU" << endl;
}
int main()
{
f >> n >> m;
int i,op,x,y;
for(i=1;i<=n;i++)
{
inaltime[i] = 0;
tata[i] = i;
}
int repx,repy;
for(i=1;i<=m;i++)
{
f >> op >> x >> y;
repx = reprez(x);
repy = reprez(y);
if(op == 1)
reuniune(repx,repy);
else
afisare(repx,repy);
}
}