Pagini recente » Cod sursa (job #2795672) | Cod sursa (job #270393) | Cod sursa (job #254667) | Cod sursa (job #518595) | Cod sursa (job #360231)
Cod sursa(job #360231)
#include <iostream>
#include <fstream>
#include <stack>
#include <algorithm>
using namespace std;
int vect[100001];
stack<int > S;
int father(int i)
{
int father=i;
while ( vect[father] != 0)
{
S.push(father);
father=vect[father];
}
while (!S.empty())
{
vect[S.top()]=father;
S.pop();
}
return father;
}
void join(int i, int j)
{
if (vect[i] == 0)
vect[i]=j;
else
if (vect[j] == 0)
vect[j]=i;
else
vect[father(i)]=j;
}
void solve(const char * buff_file, const char * out_file)
{
fstream f(buff_file, ios::in);
fstream g(out_file, ios::out);
int nodes;
int m;
f>>nodes>>m;
int dec, left, right;
for (int i=1; i<=m; ++i)
{
f>>dec>>left>>right;
if ( dec == 1 )
join(left, right);
if ( dec == 2 )
{
if (father(left)==father(right))
g<<"DA\n";
else
g<<"NU\n";
}
}
}
int main()
{
solve("disjoint.in", "disjoint.out");
return 0;
}