Pagini recente » Cod sursa (job #353769) | Cod sursa (job #1982148) | Cod sursa (job #2638175) | Cod sursa (job #1314644) | Cod sursa (job #657406)
Cod sursa(job #657406)
#include <fstream>
#define NMAX 100200
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,i,x,y,o,PR[NMAX],V[NMAX];
int rad(int x) {
int p,y;
for (p=x;p!=PR[p];p=PR[p]);
for (;x!=PR[x];) {
y=PR[x];
PR[x]=p;
x=y;
}
return p;
}
void unite(int x,int y) {
if (V[x]>=V[y]) PR[y]=x;
else PR[x]=y;
if (V[x]==V[y]) V[x]++;
}
int main () {
f >> n >> m;
for (i=1;i<=n;i++) {
PR[i]=i;
V[i]=1;
}
for (i=1;i<=m;i++) {
f >> o >> x >> y;
if (o==1) unite(rad(x),rad(y));
if (o==2) {
if (rad(x)==rad(y)) g << "DA" << '\n';
else g << "NU" << '\n';
}
}
f.close();g.close();
return 0;
}