Pagini recente » Cod sursa (job #3199644) | Cod sursa (job #275820) | Cod sursa (job #1709598) | Cod sursa (job #2657018) | Cod sursa (job #2858911)
#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 || start == end) {
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);
}
}
}
/*
1 1
2 1 1 -> DA
5 6
1 2 1
2 2 1
2 1 2
2 1 3
2 2 3
2 3 5 -> DA DA NU NU NU, bun
6 14
1 1 6
1 4 5
1 4 2
1 6 2
1 2 3
2 1 2
2 1 3
2 1 4
2 1 5
2 1 6
2 2 6
2 2 1
2 3 5
2 3 4 -> 9X DA -> bun
4 6
1 1 2
1 3 4
2 1 3
2 1 2
1 1 3
2 1 4 -> NU, DA, DA -> bun
*/