Cod sursa(job #2423040)

Utilizator Mickai69Mihai Voica Mickai69 Data 20 mai 2019 17:56:37
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void DFS (int node, vector<int> &viz, vector<vector<int>> &adj)
{
    viz[node] = 1;
    for (auto vecin : adj[node])
        if (viz[vecin] == 0)
            DFS(vecin, viz, adj);
}

bool verif (int node1, int node2, int n, vector<vector<int>> &adj)
{
    vector<int> viz(n + 1, 0);
    DFS(node1, viz, adj);
    return !viz[node2];
}

int main()
{
    ifstream fin ("disjoint.in");
    ofstream fout ("disjoint.out");
    int n, m, k, a, b;
    fin >> n >> m;
    vector<vector<int>> adj(n + 1);
    for (int i = 0; i < m; i++)
    {
        fin >> k >> a >> b;
        if (k == 1){
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        else
            if(verif(a, b, n, adj))
                fout << "NU" << "\n";
            else
                fout << "DA" << "\n";

    }


    return 0;
}