Cod sursa(job #3317548)

Utilizator DalvDalvGhita Vladut DalvDalv Data 24 octombrie 2025 14:21:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.31 kb
#include <algorithm>
#include <ctime>
#include <vector>
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <unordered_map>

using namespace std;

const int NMAX = 1e5;
int p[NMAX + 1], sz[NMAX + 1];

int find(int x) {
	while(x != p[x]) {
		x = p[x];
	}

	return x;
}

void unite(int x, int y) {
	x = find(x);
	y = find(y);

	if(sz[x] < sz[y]) {
		p[x] = y;
		sz[y] += sz[x];
		// sz[x] = 0;
	} else {
		p[y] = x;
		sz[x] += sz[y];
		// sz[y] = 0;
	}
}

int main() {
	ifstream cin("disjoint.in");
	ofstream cout("disjoint.out");

	int n, m;
	cin >> n >> m;

	for(int i = 1; i <= n; i++) {
		p[i] = i;
		sz[i] = 1;
	}

	for(int i = 1; i <= m; i++) {
		int op, x, y;
		cin >> op >> x >> y;

		if(op == 1) {
			unite(x, y);
		} else {
			if(find(x) == find(y)) cout << "DA\n";
			else cout << "NU\n";
		}
	}
}





















// int solution1(string s){
//     set<int> coinIndicesGotten;
//
//     int n = s.size();
//
//     for(int i=0; i<n; i++){
//         if(s[i] != 'T') continue;
//
//         for(int j = i + 3; j < n; j += 3){
//             if(s[j] == 'T') break;
//             if(s[j] != 'C') continue;
//
//             coinIndicesGotten.insert(j);
//         }
//
//         cout << s << endl;
//     }
//
//     return coinIndicesGotten.size();
// }
//
// int shareDigit(int a, int b) {
//
//
//     if(a % 10 == b % 10) return true;
//     if(a % 10 == b / 10) return true;
//
//     if(a / 10 == b % 10) return true;
//     if(a / 10 == b / 10) return true;
//
//     return -1;
// }
//
// set<int> getSet(int a) {
//     set<int> set;
//     set.insert(a % 10);
//     set.insert(a / 10);
//
//     return set;
// }
//
// int solution2(vector<int> v) {
//     int n = v.size();
//
//     int best = 0;
//     for(int i = 0; i < n; i++) {
//         int nrGrouped = 1;
//         for(int j = i + 1; j < n; j++)
//             if(v[i] % 10 == v[j] % 10 || v[i] % 10 == v[j] / 10) nrGrouped++;
//         best = max(best, nrGrouped);
//
//         nrGrouped = 1;
//         for(int j = i + 1; j < n; j++)
//             if(v[i] / 10 == v[j] % 10 || v[i] / 10 == v[j] / 10) nrGrouped++;
//         best = max(best, nrGrouped);
//     }
//
//     return best;
// }
//
//
// int main() {
//     cout << solution1("TT.T.CCCCC") << endl;
//
//     cout << solution2(vector{52,25,11,52,34,55}) << endl;
//     return 0;
// }