Pagini recente » Cod sursa (job #2728891) | Cod sursa (job #405678) | Cod sursa (job #675539) | Cod sursa (job #135070) | Cod sursa (job #2858909)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <fstream>
#include <cassert>
#include <vector>
#include <set>
#include <queue>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAX_SIZE = 100005;
vector<int> graph[MAX_SIZE];
int goneThrough[MAX_SIZE];
void findWay(int start, int end) { // bfs
for (int i = 1; i <= MAX_SIZE; ++i) {
goneThrough[i] = 0;
}
goneThrough[start] = 1;
int ok = 0;
queue<int> positions;
positions.push(start);
while (!positions.empty()) {
int currentPeak = positions.front();
positions.pop();
for (int i = 0; i < graph[currentPeak].size(); ++i) {
int nextPeak = graph[currentPeak][i];
if (nextPeak == end) {
ok = 1;
break;
}
if (goneThrough[nextPeak] == 0) {
goneThrough[nextPeak] = 1;
positions.push(nextPeak);
}
}
}
if (ok) {
fout << "DA";
} else {
fout << "NU";
}
fout << "\n";
}
int main() {
int peaks, operations;
fin >> peaks >> operations;
for (int i = 1; i <= operations; ++i) {
int operation, start, end;
fin >> operation >> start >> end;
if (operation == 1) {
graph[start].push_back(end);
graph[end].push_back(start);
} else {
findWay(start, end);
}
}
}