Cod sursa(job #2816466)

Utilizator alexxxxxxhalex alx alexxxxxxh Data 11 decembrie 2021 13:53:29
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb

#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector <vector<int>>v;
int f(int x,int y)
{
    vector <int> viz;
    viz.resize(100001);
    queue<int> Q;
    Q.push(x);
    viz[x]=1;
    while (!Q.empty())
    {
        int i=Q.front();
        Q.pop();
        for (int j=0;j<v[i].size();j++)
        {
            if (v[i][j]==y) return 1;
            if (!viz[v[i][j]])
            {
                viz[v[i][j]]=1;
                Q.push(v[i][j]);
            }
        }
    }
    return 0;
}
int main()
{
int  n,m;
fin >>n>>m;
v.resize(1000001);
for (int i=1;i<=m;i++)
{
    int intr;
    fin >>intr;
    int x,y;
    fin >>x>>y;
    if (intr==1)
    {
        v[x].push_back(y);
        v[y].push_back(x);
    }
    else
    {
        if (f(x,y)==1) fout << "DA";
        else fout << "NU";
        fout <<"\n";
    }
}
}