Cod sursa(job #1975955)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 2 mai 2017 15:51:53
Problema Heavy Path Decomposition Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream f("heavypath.in");
ofstream g("heavypath.out");

const int NMAX = 100005, MMAX = 100005, INF = 0x3f3f3f3f;
int n, m, value[NMAX], father[NMAX], height[NMAX];

vector<int> a[NMAX];

inline void DF(int node)
{
	//father[node] = fath;
	//height[node] = height[fath] + 1;
	for (int i = 0; i < a[node].size(); i++)
	{
		if (height[a[node][i]] != 0)
			continue;
		father[a[node][i]] = node;
		height[a[node][i]] = height[node] + 1;
		DF(a[node][i]);
	}
}

inline int max(const int & a, const int & b)
{
	if (a < b) return b;
	return a;
}

int main()
{
	f>>n>>m;
	for (int i = 1; i <= n; i++)
		f>>value[i];
	for (int x, y, i = 1; i < n; i++)
	{
		f>>x>>y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	height[1] = 1;
	DF(1);
	int p, x, y;
	while(m--)
	{
		f>>p>>x>>y;
		if (p == 0) // update
		{
			value[x] = y;
		} else  // query
		{
			int Max = max(value[x], value[y]);
			while(height[x] > height[y])
			{
				x = father[x];
				Max = max(Max, value[x]);
			}
			while(height[y] > height[x])
			{
				y = father[y];
				Max = max(Max, value[y]);
			}
			while (x != y)
			{
				x = father[x];
				y = father[y];
				int tempMax = max(value[x], value[y]);
				Max = max(Max, tempMax);
			}
			g<<Max<<'\n';
		}
	}
	f.close();
	g.close();
	return 0;
}