Cod sursa(job #1139868)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 11 martie 2014 16:06:31
Problema Heavy Path Decomposition Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.46 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "heavypath.in";
const char outfile[] = "heavypath.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, M, value[MAXN], heavy[MAXN], depth[MAXN];
int pathCount;
int pathWhere[MAXN], pathPosition[MAXN], pathFather[MAXN];
vector <int> arb[MAXN], path[MAXN];
Graph G;

void DFs(int Node, int Father, int actLevel) {
    depth[Node] = actLevel;
    heavy[Node] = 1;
    int heaviest = -1;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(*it == Father)
            continue;
        DFs(*it, Node, actLevel + 1);
        heavy[Node] += heavy[*it];
        if(heaviest == -1)
            heaviest = *it;
        else if(depth[heaviest] < depth[*it])
            heaviest = *it;
    }
    if(heaviest == -1) {
        pathWhere[Node] = ++ pathCount;
        pathFather[pathWhere[Node]] = Father;
        path[pathWhere[Node]].push_back(Node);
        return;
    }
    pathWhere[Node] = pathWhere[heaviest];
    pathFather[pathWhere[Node]] = Father;
    path[pathWhere[Node]].push_back(Node);

}

void Build(int Node, int st, int dr, int whichPath) {
    if(st == dr) {
        arb[whichPath][Node] = value[path[whichPath][st]];
        return;
    }
    int mid = ((st + dr) >> 1);
    Build((Node << 1), st, mid, whichPath);
    Build(((Node << 1)|1), mid+1, dr, whichPath);
    arb[whichPath][Node] = max(arb[whichPath][(Node << 1)], arb[whichPath][((Node << 1)|1)]);
}

void Update(int Node, int st, int dr, int pos, int whichPath) {
    if(st == dr) {
        arb[whichPath][Node] = value[path[whichPath][pos]];
        return;
    }
    int mid = ((st + dr) >> 1);
    if(pos <= mid)
        Update(Node << 1, st, mid, pos, whichPath);
    else Update(((Node << 1) | 1), mid+1, dr, pos, whichPath);
    arb[whichPath][Node] = max(arb[whichPath][(Node << 1)], arb[whichPath][((Node << 1)|1)]);
}

int Query(int Node, int st, int dr, int a, int b, int whichPath) {
    if(a <= st && dr <= b)
        return arb[whichPath][Node];
    int mid = ((st + dr) >> 1);
    int actMax = -oo;
    if(a <= mid)
        actMax = max(actMax, Query((Node << 1), st, mid, a, b, whichPath));
    if(mid < b)
        actMax = max(actMax, Query(((Node << 1) | 1), mid+1, dr, a, b, whichPath));
    return actMax;
}

int QueryHeavyPath(int x, int y) {
    if(pathWhere[x] == pathWhere[y]) {
        if(pathPosition[x] > pathPosition[y])
            swap(x, y);
        return Query(1, 0, path[pathWhere[x]].size() - 1, pathPosition[x], pathPosition[y], pathWhere[x]);
    }
    if(depth[pathFather[pathWhere[x]]] < depth[pathFather[pathWhere[y]]])
        swap(x, y);
    int actMax = Query(1, 0, path[pathWhere[x]].size() - 1, 0, pathPosition[x], pathWhere[x]);
    return max(actMax, QueryHeavyPath(pathFather[pathWhere[x]], y));
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i <= N ; ++ i)
        fin >> value[i];
    for(int i = 1 ; i != N ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFs(1, 1, 1);
    for(int i = 1 ; i <= pathCount ; ++ i) {
        reverse(path[i].begin(), path[i].end());
        for(int j = 0 ; j < int(path[i].size()) ; ++ j)
            pathPosition[path[i][j]] = j;
        arb[i].resize(4 * path[i].size());
        Build(1, 0, path[i].size() - 1, i);
    }
    for(int i = 1 ; i <= M ; ++ i) {
        int op, x, y;
        fin >> op >> x >> y;
        if(op == 0) {
            value[x] = y;
            Update(1, 0, path[pathWhere[x]].size() - 1, pathPosition[x], pathWhere[x]);
        }
        else if(op == 1)
            fout << QueryHeavyPath(x, y) << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}