Cod sursa(job #2609814)

Utilizator Gliumarin negai Gliu Data 3 mai 2020 16:25:23
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

const int nmax=100000;
queue <int> coada;
vector <int> muchi[nmax];
int n,m,k=0;
int viz[nmax][3000];

ifstream in("disjoint.in");
ofstream out("disjoint.out");

void dfs(int nod){
 viz[nod][k]=1;
 for(unsigned int i=0;i<muchi[nod].size();i++){
 	int vecin =muchi[nod][i];
 	if(!viz[vecin][k]){
 		dfs(vecin);
	 }
 }
}

int main(){
in >>n>>m;

for(int i=1;i<=m;i++){
	int tip,x,y;
	in >>tip>>x>>y;
	
	if(tip==1){
		muchi[x].push_back(y);
		muchi[y].push_back(x);
	}
	if(tip==2){
		k++;
		dfs(x);
		if(viz[y][k]){
			out <<"DA"<<"\n";
		}else out <<"NU"<<"\n";
	
	}
	
	}

return 0;
}