Cod sursa(job #1811769)

Utilizator MickeyTurcu Gabriel Mickey Data 21 noiembrie 2016 16:19:15
Problema Radiatie Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.19 kb
#include<fstream>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include<map>
#include<unordered_map>
#include<array>
#include<deque>
#include<queue>
#include<math.h>
#include<functional>
#include<unordered_set>
#include<set>
#include<iostream>
#include<iomanip>
#include<bitset>
using namespace std;
int nr,c, n, m, x, y, i, level[20200], curlv, power, j, putere, curcost, viz[20200],papa,number;
char chr;
vector<pair<int, int>>v[20200];
pair<int, int>up[20200];
//ifstream f("file.in");
//ofstream g("file.out");
//ifstream f("radiatie.in");
ofstream g("radiatie.out");
int k, point[20200], rang[20200];
struct edge
{
	int x;
	int y;
	int c;
}ed;
vector<edge>vec;
vector<edge>::iterator it;
int rez;
bool comp(edge a, edge b)
{
	return a.c < b.c;
}
int find(int x, int y)
{
	int compx = x, aux, compy = y;
	while (point[x] != x)
		x = point[x];
	while (point[y] != y)
		y = point[y];
	while (point[compx] != compx)
	{
		aux = point[compx];
		point[compx] = x;
		compx = aux;
	}
	while (point[compy] != compy)
	{
		aux = point[compy];
		point[compy] = y;
		compy = aux;
	}
	return x == y;
}
void tie(int x, int y)
{
	while (point[x] != x)
		x = point[x];
	while (point[y] != y)
		y = point[y];
	if (rang[x] > rang[y])
		point[y] = x;
	else
		point[x] = y;
	if (rang[x] == rang[y])
		rang[y]++;
}
void dfs(int nod, int lev)
{
	level[nod] = lev; 
	viz[nod] = 1;
	if (v[nod].size() != 0)
	{
		for (auto it = v[nod].begin(); it != v[nod].end(); it++)
			if (viz[it->first] == 0)
			{
				up[it->first].first = nod;
				up[it->first].second = it->second;
				dfs(it->first, lev + 1);
			}
	}
}
void lca(int x, int y)
{
	int max1,lx,ly;
	lx = level[x];
	ly = level[y];
	max1 = 0;
	while (lx != ly)
	{
		if (lx > ly)
		{
			max1 = max(up[x].second, max1);
			x = up[x].first;
			lx--;
		}
		else
		{
			max1 = max(up[y].second, max1);
			y = up[y].first;
			ly--;
		}
	}
	while (x != y)
	{
		max1 = max(up[x].second, max1);
		x = up[x].first;
		max1 = max(up[y].second, max1);
		y = up[y].first;
	}
	g << max1 << '\n';
}
inline int get_nr()
{
	number = 0;
	chr = getc(stdin);
	while (!isdigit(chr))
	{
		chr = getc(stdin);
	}
	while (isdigit(chr))
	{
		number = number * 10 + chr - '0';
		chr = getc(stdin);
	}
	return number;
}
int main()
{
	freopen("radiatie.in", "r", stdin);
	//freopen("file.in", "r", stdin);
	//f >> n >> m >> k;
	n = get_nr();
	m = get_nr();
	k = get_nr();
	for (i = 1; i <= m; i++)
	{
		//f >> x >> y >> c;
		x = get_nr();
		y = get_nr();
		c = get_nr();
		ed.x = x;
		ed.y = y;
		ed.c = c;
		vec.push_back(ed);
	}
	sort(vec.begin(), vec.end(), comp);
	it = vec.begin();
	for (i = 1; i <= n; i++)
	{
		point[i] = i;
		rang[i] = 1;
	}
	nr = 0;
	while (nr != n - 1)
	{
		if (!(find(it->x, it->y)))
		{
			nr++;
			tie(it->x, it->y);
			v[it->x].push_back(make_pair(it->y, it->c));
			v[it->y].push_back(make_pair(it->x, it->c));
		}
		if (it != vec.end())
			it++;
	}
	nr = 0;
	curcost = 0;
	dfs(1,0);
	nr--;
	for (j = 1; j <= k; j++)
	{
		//f >> x >> y;
		x = get_nr();
		y = get_nr();
		if (x == y)
			g << "0\n";
		else
			lca(x, y);
	}
	return 0;
}