Cod sursa(job #1154369)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 26 martie 2014 09:49:04
Problema Heavy Path Decomposition Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.06 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; }

const int DIM = 10000000;
char parse[DIM];
int pos;
Graph G, path, arb;
int V[MAXN], depth[MAXN], heavy[MAXN], N, M;
int numPaths, pathWhere[MAXN], pathPosition[MAXN], pathFather[MAXN];

inline int get() {
    int number = 0;
    char sgn = '+';
    while(!('0' <= parse[pos] && parse[pos] <= '9')) {
        sgn = parse[pos];
        if(++ pos == DIM) {
            fin.getline(parse, DIM, '\0');
            pos = 0;
        }
    }
    while('0' <= parse[pos] && parse[pos] <= '9') {
        number = number * 10 + parse[pos] - '0';
        if(++ pos == DIM) {
            fin.getline(parse, DIM, '\0');
            pos = 0;
        }
    }
    if(sgn == '-')
        number *= -1;
    return number;
}

inline 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 || heavy[*it] > heavy[heaviest])
            heaviest = *it;
    }
    if(heaviest == -1) {
        pathWhere[Node] = ++ numPaths;
        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);
}

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

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

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

inline int QueryHeavyPath(int x, int y) {
    //cerr << x << ' ' << y << '\n';
    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() {
    N = get();
    M = get();
    fin >> N >> M;
    for(int i = 1 ; i <= N ; ++ i)
        V[i] = get();
    for(int i = 1 ; i != N ; ++ i) {
        int x, y;
        //fin >> x >> y;
        x = get();
        y = get();
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFs(1, 0, 1);
    for(int i = 1 ; i <= numPaths ; ++ i) {
        reverse(path[i].begin(), path[i].end());
        for(int j = 0 ; j < path[i].size() ; ++ j)
            pathPosition[path[i][j]] = j;
        arb[i].resize(4 * path[i].size() + 10);
        Build(1, 0, path[i].size() - 1, i);
    }
    for(int i = 1 ; i <= M ; ++ i) {
        int op, x, y, z;
        op = get();
        x = get();
        y = get();
        ///fin >> op >> x >> y;
        if(op == 0) {
            V[x] = y;
            Update(1, 0, path[pathWhere[x]].size() - 1, pathPosition[x], pathWhere[x]);
        }
        if(op == 1)
            fout << QueryHeavyPath(x, y) << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}