Pagini recente » Cod sursa (job #872565) | Cod sursa (job #1593980) | Cod sursa (job #885358) | Cod sursa (job #1574861) | Cod sursa (job #3199808)
#include <bits/stdc++.h>
#define FastIo() ios_base::sync_with_stdio(false), fin.tie(nullptr),fout.tie(nullptr);
#define ll long long
#define pii pair<int,int>
#define pipii pair<int,pair<int,int>>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
//#define fin cin
//#define fout cout
struct DSU{
int n;
vector<int> t;
DSU(int _n){
n=_n;
t.resize(n);
for(int i=0;i <n;i++)
t[i]=i;
}
int get_root(int x){
if(t[x] != x) return t[x] = get_root(t[x]);
return x;
}
void join(int x, int y){
int root_x = get_root(x);
int root_y = get_root(y);
t[root_x] = root_y;
}
bool same_union(int x, int y){
return (get_root(x) == get_root(y));
}
void afisare(){
for(int i=0;i<n;i++)
fout<<t[i]<<' ';
fout<<'\n';
}
};
int n, m;
int op, x, y;
int main(){
fin>>n>>m;
DSU dsu = DSU(n);
for(int i=1;i<=m;i++){
cin>>op>>x>>y;
x--,y--;
if(op==1){
dsu.join(x,y);
}else{
if(dsu.same_union(x,y)) fout<<"DA\n";
else fout<<"NU\n";
}
dsu.afisare();
}
}