Cod sursa(job #3326678)

Utilizator GliggyGligor Andrei Gliggy Data 29 noiembrie 2025 22:05:51
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
// RULE: There shouldnt be any spaces in the code, except after commas.
// RULE: All variables in main should be declared globally, and can be used before they are defined.
// RULE: All open curly braces should be placed on the same line as the function or control structure they belong to, not on the line after.
#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");   //strudel
ofstream fout("disjoint.out"); //disjoint
int n,m,i,c,x,y;
int t[100010];
int getp(int x){
    if(t[x]>0) t[x]=getp(t[x]), x=t[x];
    return x;
}
void join(int x, int y){
    int parx, pary;
    parx = getp(x);
    pary = getp(y);
    if(t[parx]>t[pary]) t[pary]+=t[parx], t[parx]=y;
    else t[parx]+=t[pary], t[pary]=x;
}
bool find(int x, int y){
    return getp(x)==getp(y);
}
void print(){
    for(int i=1;i<=n;i++) fout<<t[i]<<" ";
    fout<<'\n';
}
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++){
        fin>>c>>x>>y;
        if(c==1) join(x,y);
        else fout<<(find(x,y)==1?"DA\n":"NU\n");
    }
    return 0;
}