Pagini recente » Cod sursa (job #919156) | Cod sursa (job #2967193) | Istoria paginii runda/baraj_spiru | Cod sursa (job #2113795) | Cod sursa (job #2108765)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int MAX = 100005;
vector < int > myVector[MAX];
queue < int > myQueue;
int N ,M ,tip, x, y;
bool beenThere[MAX];
void Reset()
{
for ( int i = 1; i <= N ; ++i)
beenThere[i] = false;
}
void Tip1(int nod1, int nod2)
{
myVector[nod1].push_back(nod2);
myVector[nod2].push_back(nod1);
}
void Tip2(int nod1,int nod2)
{
bool OK = false;
myQueue.push(nod1);
while(!myQueue.empty())
{
int nodCurent = myQueue.front();
myQueue.pop();
if(nodCurent == nod2) OK = true;
beenThere[nodCurent] = true;
for ( size_t k = 0 ; k < myVector[nodCurent].size() ; ++k)
{
int Vecin = myVector[nodCurent][k];
if(beenThere[Vecin] == false)
{
beenThere[Vecin] = true;
myQueue.push(Vecin);
}
}
}
if( OK == true) out << "DA" <<"\n";
else out << "NU" <<"\n";
}
int main()
{
in >> N >> M;
for ( int i = 1; i <= M ;++i)
{
in >> tip >> x >> y;
if( tip == 1) Tip1(x,y);
if( tip == 2)
{ Tip2(x,y);
Reset();
}
}
return 0;
}