Pagini recente » Cod sursa (job #244420) | Cod sursa (job #404853) | Cod sursa (job #1720487) | Istoria paginii runda/rf__1 | Cod sursa (job #668701)
Cod sursa(job #668701)
// 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) || isJoined(second,first))
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);
}