Cod sursa(job #3277022)

Utilizator leelcheeseCiovnicu Denis leelcheese Data 15 februarie 2025 11:21:17
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda vs11_12_vine_oji_2025 Marime 2.1 kb
#include <bits/stdc++.h>
#include <unordered_map>
#define nmax 100006
#define MOD 666013
#define INF 2012345678
#define ll long long
#define ull unsigned long long
using namespace std;
//#define fin cin
//#define fout cout

ifstream fin("radiatie.in");
ofstream fout("radiatie.out");

struct Patru {
	int x, y, cost;
	bool operator < (const Patru e) const
	{
		return cost < e.cost;
	}
}a[nmax];

int n, m, K;
int t[nmax];
int price[nmax]; // price[i] - pretul cel mai mic sa plec din i

int DFS(int x, int y)
{
	int mx1, mx2, i, j;
	bool ok1, ok2;
	mx1 = mx2 = INF; i = x; j = y; ok1 = ok2 = 0;

	if (t[x] != 0)
		mx1 = 0;

	if (t[y] != 0)
		mx2 = 0;

	while (t[i] != 0 && i != y)
	{
		if (t[i] == y)
			ok1 = 1;

		mx1 = max(mx1, price[i]);
		i = t[i];
	}

	while (t[j] != 0 && j != x)
	{
		if (t[j] == x)
			ok2 = 1;
		mx2 = max(mx2, price[j]);
		j = t[j];
	}

	if (ok2)
		return mx2;
	return mx1;
}

int FindRoot(int x)
{
	int rad, y;
	rad = x;
	while (t[rad] != 0)
		rad = t[rad];

	while (x != rad)
	{
		y = t[x];
		t[x] = rad;
		x = y;
	}

	return rad;
}

void Union(int x, int y)
{
	t[y] = x;
}

void Kruskal()
{
	int i, j, x, y, costTotal, nrc;
	costTotal = 0; nrc = n;
	sort(a + 1, a + m + 1);
	for (i = 1; nrc > 1; i++)
	{
		x = FindRoot(a[i].x);
		y = FindRoot(a[i].y);
		if (x != y)
		{
			costTotal += a[i].cost;
			Union(x, y);
			nrc--;
		}
	}
}

void afis()
{
	for (int i = 1; i <= n; i++)
		fout << i << " ";
	fout << "\n";

	for (int i = 1; i <= n; i++)
		fout << t[i] << " ";
	fout << "\n";

	for (int i = 1; i <= n; i++)
		if (price[i] != INF)
			fout << price[i] << " ";
		else
			fout << "x ";
	fout << "\n";
	fout << "\n";
}

int main()
{
	int i, x, y, cost;
	fin >> n >> m >> K;

	for (i = 1; i <= n; i++)
		price[i] = INF;

	for (i = 1; i <= m; i++)
	{
		fin >> x >> y >> cost;
		a[i].x = x;
		a[i].y = y;
		a[i].cost = cost;
		price[y] = min(price[y], cost);
	}

	Kruskal();

	afis();

	for (i = 1; i <= K; i++)
	{
		fin >> x >> y;
		fout << DFS(x, y) << "\n";
	}
	return 0;
}