Pagini recente » Cod sursa (job #1349233) | Cod sursa (job #1055773) | Cod sursa (job #2821881) | Cod sursa (job #1438908) | Cod sursa (job #1439202)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
vector <int> v[1000001];
int ok,ind[100001],tata[100001];
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int dfs(int nod1,int nod2)
{
if(tata[nod1]==nod2) {ok=1;return 1;}
else
if(nod1!=tata[nod1])
{dfs(tata[nod1],nod2);
if(ok==1) return 1;}
return 0;
}
int main()
{
int n,m;
f>>n>>m;
for(int i=1;i<=n;i++) {v[i].push_back(i);tata[i]=i;}
for(int i=1;i<=m;i++)
{
int cod,x,y;
f>>cod>>x>>y;
switch (cod)
{
case 1:
if(y<x)
tata[y]=x;
else tata[x]=y;
break;
case 2:
if(x>y) swap(x,y);
ok=0;
if(dfs(x,y)==1) g<<"DA\n";
else g<<"NU\n";
break;
}
}
return 0;
}