Cod sursa(job #2658863)

Utilizator Alin_StanciuStanciu Alin Alin_Stanciu Data 15 octombrie 2020 12:40:14
Problema Heavy Path Decomposition Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("heavypath.in");
ofstream fout("heavypath.out");

int n, m, V[100001];
vector<int> G[100001];
bool F[100001];

int Dfs(int x, int t)
{
    if (x == t)
        return V[x];

    F[x] = true;
    for (int y : G[x])
    {
        if (!F[y])
        {
            int ma = Dfs(y, t);
            if (ma >= 0)
            {
                F[x] = false;
                return max(ma, V[x]);
            }
        }
    }
    F[x] = false;
    return -1;
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; ++i)
        fin >> V[i];
    for (int i = 1; i < n; ++i)
    {
        int a, b;
        fin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    for (int i = 1; i <= m; ++i)
    {
        int t, x, y;
        fin >> t >> x >> y;
        if (t == 0)
            V[x] = y;
        else if (t == 1)
            cout << Dfs(x, y) << '\n';
    }

    return 0;
}