Cod sursa(job #1394390)
| Utilizator | Data | 20 martie 2015 11:51:47 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 40 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include <iostream>
#include<fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,i,t[100001],x,y,cod,tx,ty;
int findt(int x)
{
if(x!=t[x])
return t[x]=findt(t[x]);
else return x;
}
void unite(int x,int y)
{
tx=findt(x);
ty=findt(y);
t[tx]=ty;
}
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++)
{
f>>cod>>x>>y;
if(cod==1) unite(x,y);
else if(findt(x)==findt(y)) g<<"DA"<<endl;
else g<<"NU"<<endl;
}
return 0;
}
