Cod sursa(job #3266390)

Utilizator Commander_XDunel Stefan-Octavian Commander_X Data 8 ianuarie 2025 12:48:10
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.22 kb
//#include <fstream>
//#include <vector>
//#include <queue>
//
//std::ifstream fin("dijkstra.in");
//std::ofstream fout("dijkstra.out");
//
//using namespace std;
//
//const int INF = 0x3f3f3f3f;
//
//vector<vector<pair<int,int>>> G;
//vector<int> dist;
//struct crit
//{
//	bool operator()(pair<int, int> a, pair<int, int> b)
//	{
//		return a.second < b.second;
//	}
//};
//priority_queue<pair<int, int>, vector<pair<int, int>>, crit> pq;
//int n,p;
//
//void read()
//{
//	int x, y, c;
//	fin >> n >> p;
//	G.resize(n + 1);
//	dist.resize(n + 1, INF);
//
//	while(fin >> x >> y >> c)
//		G[x].push_back({ y,c });
//}
//void Dijkstra(int start)
//{
//	int nod;
//	pq.push({ start,0 });
//	dist[start] = 0;
//	while (!pq.empty())
//	{
//		nod = pq.top().first;
//		pq.pop();
//		for (auto vecin : G[nod])
//			if (dist[vecin.first] > dist[nod] + vecin.second)
//			{
//				dist[vecin.first] = dist[nod] + vecin.second;
//				pq.push({ vecin.first,dist[vecin.first] });
//			}
//	}
//}
//int main()
//{
//	read();
//	Dijkstra(p);
//	for (int i = 1; i <= n; ++i)
//		if (dist[i] == INF)
//			fout << -1 << ' ';
//		else
//			fout << dist[i] << ' ';
//}

//https://www.pbinfo.ro/probleme/1652/rf

//#include <iostream>
//#include <vector>
//#include <algorithm>
//#include <queue>
//
//using namespace std;
//
//const int oo = 0x3f3f3f3f;
//
//struct muchie
//{
//	int x, y, c;
//};
//struct ordine
//{
//	bool operator()(pair<int, int> a, pair<int, int> b)
//	{
//		return a.second > b.second;
//	}
//};
//bool comp(muchie a, muchie b)
//{
//	return a.x < b.x;
//}
//
//vector<vector<pair<int, int>>>G;
//vector<int> dist;
//vector<muchie> v;
//priority_queue<pair<int, int>, vector<pair<int, int>>, ordine> pq;
//int n, m;
//
//void read()
//{
//	cin >> n >> m;
//	G.resize(n + 1);
//	dist.resize(n + 1, oo);
//	v.resize(m);
//	for (int i = 0; i < m; ++i)
//	{
//		cin >> v[i].x >> v[i].y >> v[i].c;
//		G[v[i].x].push_back({ v[i].y,v[i].c });
//	}
//}
//void Dijkstra(int start)
//{
//	dist.clear();
//	dist.resize(n + 1, oo);
//	pq.push({ start,0 });
//	dist[start] = 0;
//	int nod;
//	while (!pq.empty())
//	{
//		nod = pq.top().first;
//		pq.pop();
//		for(auto vecin : G[nod])
//			if (dist[vecin.first] > dist[nod] + vecin.second)
//			{
//				dist[vecin.first] = dist[nod] + vecin.second;
//				pq.push({ vecin.first,dist[vecin.first] });
//			}
//	}
//}
//int main() 
//{
//	int start, cnt = 0;
//	read();
//	sort(v.begin(), v.end(), comp);
//	for (int i = 0; i < m; i++)
//	{
//		start = v[i].x;
//		Dijkstra(v[i].x);
//		for (i; i < m && v[i].x == start;++i)
//			if (dist[v[i].y] == v[i].c)
//				++cnt;
//		--i;
//	}
//	cout << cnt;
//}

//https://www.pbinfo.ro/probleme/593/parc

//#include<fstream>   
//#include<vector>
//#include<queue>
//
//std::ifstream fin("parc.in");
//std::ofstream fout("parc.out");
//
//using namespace std;
//
//const long long int oo = 0x3f3f3f3f;
//
//struct crit
//{
//    bool operator()(pair<long long int, long long int> a, pair<long long int, long long int> b)
//    {
//        return a.second > b.second;
//    }
//};
//vector<vector<pair<long long int, long long int>>>G;
//vector<long long int> dist;
//priority_queue<pair<long long int, long long int>, vector<pair<long long int, long long int>>, crit> pq;
//long long int n, m,C;
//
//void read()
//{
//    long long int x, y, c;
//    fin >> n >> m >> C;
//    G.resize(n + 1);
//    dist.resize(n + 1, oo);
//
//    for (int i = 0; i < m; ++i)
//    {
//        fin >> x >> y >> c;
//        G[x].push_back({ y,c });
//        G[y].push_back({ x,c });
//    }
//}
//void Dijkstra(long long int start)
//{
//    long long int nod;
//    pq.push({ start,0 });
//    dist[start] = 0;
//    while (!pq.empty())
//    {
//        nod = pq.top().first;
//        pq.pop();
//        for (auto vecin : G[nod])
//            if (dist[vecin.first] > dist[nod] + vecin.second)
//            {
//                dist[vecin.first] = dist[nod] + vecin.second;
//                pq.push({ vecin.first,dist[vecin.first] });
//            }
//    }
//}
//int main()
//{
//    long long int l, x, minim = oo, minim_poz;
//    read();
//    fin >> l;
//    Dijkstra(C);
//    while (fin >> x)
//    {
//        if (dist[x] < minim)
//        {
//            minim = dist[x];
//            minim_poz = x;
//        }
//    }
//    fout << minim_poz;
//}

///https://infoarena.ro/problema/disjoint

#include <fstream>
#include <vector>

std::ifstream fin("disjoint.in");
std::ofstream fout("disjoint.out");

using namespace std;

vector<int> T, Card;
int N, M;

int findT(int x)
{
	if (T[x] == x)
		return x;
	T[x] = findT(T[x]);
	return T[x];
}
void Unite(int x, int y)
{
	if (Card[x] < Card[y])
		swap(x, y);
	T[y] = x;
	Card[x] += Card[y];
}
int main()
{
	int c, x, y;
	fin >> N >> M;
	Card.resize(N + 10, 0);
	T.resize(N + 10);
	for (int i = 1; i <= N; ++i)
	{
		T[i] = i;
		Card[i] = 1;
	}
	for (int i = 1; i <= M; ++i)
	{
		fin >> c >> x >> y;
		if (c == 1 && findT(x) != findT(y))
			Unite(x, y);
		else
			fout << (findT(x) == findT(y) ? "DA\n" : "NU\n");
	}
}

//#include <fstream>
//#include <vector>
//
//using namespace std;
//
//constexpr int NMAX = 100002;
//
//int n, m;
//
//vector<int> rad(NMAX), card(NMAX);
//
//int Find(int x) {
//    if (rad[x] == x) {
//        return x;
//    }
//    rad[x] = Find(rad[x]);
//    return rad[x];
//}
//
//void Union(int a, int b) {
//    if (card[a] < card[b]) {
//        swap(a, b);
//    }
//    rad[b] = a;
//    card[a] += card[b];
//}
//
//int main() {
//    ifstream fin("disjoint.in");
//    ofstream fout("disjoint.out");
//
//    fin >> n >> m;
//
//    for (int i = 1; i <= n; ++i) {
//        rad[i] = i;
//        card[i] = 1;
//    }
//
//    for (int i = 1; i <= m; ++i) {
//        int cod, x, y;
//        fin >> cod >> x >> y;
//
//        if (cod == 1 && Find(x) != Find(y)) {
//            Union(Find(x), Find(y));
//        }
//        else {
//            fout << ((Find(x) == Find(y)) ? "DA\n" : "NU\n");
//        }
//    }
//
//    fin.close();
//    fout.close();
//    return 0;
//}