Pagini recente » Monitorul de evaluare | Cod sursa (job #2342510) | Cod sursa (job #2607112) | Cei mai harnici utilizatori infoarena | Cod sursa (job #1926946)
#include <bits/stdc++.h>
#define N 100010
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int root[N];
int totalNodes, totalRelations;
int getRoot(int node){
if ( root[node] != node ){
root[node] = getRoot(root[node]);
}
return root[node];
}
inline void connectThings(int first, int second){
first = getRoot(first);
second = getRoot(second);
root[first] = root[second];
}
inline void doesItExist(int first, int second){
first = getRoot(first);
second = getRoot(second);
if ( first == second )
fout << "DA" << "\n";
else
fout << "NU" << "\n";
}
int main(){
fin >> totalNodes >> totalRelations;
for ( int index = 1; index <= totalNodes; index++ )
root[index] = index;
int first, second, question;
for ( int index = 1; index <= totalRelations; index++ ){
fin >> question >> first >> second;
switch (question){
case 1:
connectThings(first, second);
break;
case 2:
doesItExist(first, second);
break;
}
}
}