Pagini recente » Cod sursa (job #1413456) | Cod sursa (job #2560394) | Cod sursa (job #1205654) | Cod sursa (job #1438904) | Cod sursa (job #1438756)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
vector <int> v[1000001];
int ok,ind[100001];
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int dfs(int nod1,int nod2)
{
if(nod1==nod2)
{
ok=1; return 1;
}
ind[nod1]=1;
for(int j=0;j<v[nod1].size();j++)
if(ind[v[nod1][j]]==0)
{
if(dfs(v[nod1][j],nod2)==1) return 1;
ind[v[nod1][j]]=1;
}
return 0;
}
int main()
{
int n,m;
f>>n>>m;
for(int i=1;i<=n;i++) v[i].push_back(i);
for(int i=1;i<=m;i++)
{
int cod,x,y;
f>>cod>>x>>y;
switch (cod)
{
case 1:
v[x].push_back(y);
v[y].push_back(x);
break;
case 2:
ok=0;
for(int k=1;k<=n;k++) ind[k]=0;
if(dfs(x,y)==1) g<<"DA\n";
// {
// ok=0;
// for(int k=1;k<=n;k++) ind[k]=0;
// if(dfs(y,x)==1) g<<"DA\n";
// else g<<"NU\n";
// }
else g<<"NU\n";
break;
}
}
return 0;
}