Cod sursa(job #2858909)

Utilizator CiuiGinjoveanu Dragos Ciui Data 28 februarie 2022 16:27:17
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#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);
        }
    }
}