Pagini recente » Cod sursa (job #2464806) | Cod sursa (job #3188251) | Cod sursa (job #2870186) | Cod sursa (job #2612603) | Cod sursa (job #901750)
Cod sursa(job #901750)
#include <stdio.h>
const int sz = (int)1e5+1;
int getRoot(int node);
FILE *in,*out;
int numbers, questions;
int father[sz];
int main()
{
in=fopen("disjoint.in","rt");
out=fopen("disjoint.out","wt");
fscanf(in,"%d%d",&numbers,&questions);
while(questions--)
{
int type, node1, node2;
fscanf(in,"%d%d%d",&type, &node1, &node2);
if(type==1)
father[getRoot(node2)] = getRoot(node1);
else
fprintf(out,"%s\n", (getRoot(node1)==getRoot(node2)) ? "DA" : "NU");
}
fclose(in);
fclose(out);
return 0;
}
int getRoot(int node)
{ return (father[node]) ? father[node] = getRoot(father[node]) : node; }