Cod sursa(job #1350410)
| Utilizator | Data | 20 februarie 2015 19:47:01 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 70 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <fstream>
#define NMax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M,TT[NMax];
void Unite(int x, int y)
{
TT[x] = y;
}
int Father(int x)
{
while(TT[x]!=x)
x=TT[x];
return x;
}
int main()
{
fin>>N>>M;
for(int i=1;i<=N;i++)
TT[i] = i;
while(M--)
{
int cod,x,y;
fin>>cod>>x>>y;
if(cod==1)
Unite(Father(x),Father(y));
if(cod==2)
if(Father(x)==Father(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}
