Pagini recente » Cod sursa (job #2106580) | Clasament testf | Cod sursa (job #1998163) | Agm 2018 Runda 2 | Cod sursa (job #668700)
Cod sursa(job #668700)
// http://infoarena.ro/problema/disjoint
#include <fstream>
using namespace std;
const int MAXSIZE = 100001;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int length,operations;
int v[MAXSIZE];
void join(int node,int father);
bool isJoined(int first,int second);
int main()
{
in >> length >> operations;
int type,first,second;
for(int i=1;i<=operations;i++)
{
in >> type >> first >> second;
if(type == 1)
join(first,second);
else
{
//for(int i=1;i<=length;i++)
// out << v[i] << " ";
if(isJoined(first,second))
out << "DA\n";
else
out << "NU\n";
//out << "\n";
}
}
return (0);
}
void join(int node,int father)
{
if(!v[node])
v[node] = father;
else
join(v[node],father);
}
bool isJoined(int first,int second)
{
if(v[first])
return isJoined(v[first],second);
else
return (first == second);
}