Cod sursa(job #946791)

Utilizator Robert_MarksonTiberiu Popa Robert_Markson Data 5 mai 2013 21:48:13
Problema Heavy Path Decomposition Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.99 kb
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>

#define MAX_N 100000

using namespace std;

vector<int> neighbours[MAX_N];
int value[MAX_N];
int level[MAX_N];
int subtree[MAX_N];
int parent[MAX_N];
bool visited[MAX_N];

vector<int> chains[MAX_N];
int chain_index[MAX_N];
size_t chain_position[MAX_N];
int num_chains = 0;

void insert_node(int chain, int node)
{
	chain_position[node] = chains[chain].size();
	chains[chain].push_back(node);
	chain_index[node] = chain;
}

void dfs(int root)
{
	int heavy_node = -1;
	bool leaf = true;

	visited[root] = true;
	subtree[root] = 1;

	//printf("%d\n", root);
	for (vector<int>::iterator it = neighbours[root].begin(); it != neighbours[root].end(); ++it) {
		int node = *it;
		if (!visited[node]) {
			leaf = false;
			level[node] = level[root] + 1;
			parent[node] = root;
			dfs(node);
			subtree[root] += subtree[node];
			if (heavy_node == -1 || subtree[node] > subtree[heavy_node]) {
				heavy_node = node;
			}
		}
	}

	if (leaf) {
		insert_node(num_chains, root);
		++num_chains;
	} else {
		int heavy_chain = chain_index[heavy_node];
		insert_node(heavy_chain, root);
	}

	//printf("%d: st=%d hn=%2d lvl=%d chain=%d\n", root, subtree[root], heavy_node, level[root], chain_index[root]);
}

int solve_problem()
{
	int n, m;

	if (scanf("%d %d", &n, &m) != 2)
		return 1;

	for (int i = 0; i < n; i++)
		if (scanf("%d", &value[i]) != 1)
			return 1;

	for (int i = 1; i < n; i++) {
		int x, y;
		if (scanf("%d %d", &x, &y) != 2)
			return 1;
		--x;
		--y;
		neighbours[x].push_back(y);
		neighbours[y].push_back(x);
	}

	level[0] = 0;
	parent[0] = -1;
	dfs(0);

	for (int i = 0; i < m; i++) {
		int t, x, y;
		if (scanf("%d %d %d", &t, &x, &y) != 3)
			return 1;
		if (t == 0) {
			--x;
			value[x] = y;
		} else {
			--x;
			--y;
			int max_elem = 0;
			while (y >= 0) {
				int x_chain = chain_index[x];
				int y_chain = chain_index[y];
				int px = chains[x_chain].back();
				int py = chains[y_chain].back();
				//printf("(%d,%d) => (%d,%d)\n", x, y, px, py);
				if (px == py) {
					size_t left = chain_position[x];
					size_t right = chain_position[y];
					if (left > right)
						swap(left, right);
					for (size_t j = left; j <= right; j++) {
						int node = chains[y_chain][j];
						max_elem = max(max_elem, value[node]);
					}
					break;
				} else {
					if (level[px] > level[py]) {
						swap(x, y);
						swap(x_chain, y_chain);
						swap(px, py);
					}
					for (size_t j = chain_position[y]; j < chains[y_chain].size(); j++) {
						int node = chains[y_chain][j];
						max_elem = max(max_elem, value[node]);
					}
					y = parent[py];
				}
			}
			printf("%d\n", max_elem);
		}
	}

	return 0;
}

int main()
{
	const char *filenames[] = {
		"heavypath.in",
		"heavypath.out",
	};

	if (freopen(filenames[0], "rt", stdin) == NULL) {
		fprintf(stderr, "Could not reopen stdin\n");
		return 1;
	}
	if (freopen(filenames[1], "wt", stdout) == NULL) {
		fprintf(stderr, "Could not reopen stdout\n");
		return 1;
	}

	solve_problem();

	return 0;
}