Cod sursa(job #2439174)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 12:20:54
Problema Arbore Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("arbore.in");
ofstream g("arbore.out");

const int NMAX = 100005;
vector <int> v[NMAX];
int n,m, lo[NMAX], hi[NMAX], father[NMAX], ans[NMAX];
vector <tuple <int, int, int> > q;
vector <int> ord;
int mars[NMAX], sum[NMAX];

void dfs(int nod){
    ord.push_back(nod);
    lo[nod] = ord.size();
    for(auto it: v[nod])
        if(it != father[nod]){
            father[it] = nod;
            dfs(it);
        }
    hi[nod] = ord.size();
}

int main(){
    int i,j,x,y,type;
    f >> n >> m;
    for(i = 1 ; i < n; i++){
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for(i = 1 ; i <= m ; i++){
        f >> type >> x;
        if(type == 1){
            f >> y;
            q.push_back(make_tuple(type,x,y));
        }else
            q.push_back(make_tuple(type, x, 0));
        ans[i] = -1;
    }

    dfs(1);
    ord.push_back(0);

    for(auto it: q){
        type = get<0>(it);
        x = get<1>(it);
        y = get<2>(it);

        if(type == 1){
            mars[lo[x]] += y;
            mars[hi[x] + 1] -= y;
        }else{
            for(i = 1 ; i <= n ; i++){
                sum[i] = sum[i - 1] + mars[i];
                if(sum[i] == x){
                    g << ord[i - 1] << "\n";
                    i = n + 4;
                }
            }
            if(n == i - 1)
                g << -1 << "\n";
        }
    }

    return 0;
}