Cod sursa(job #2439182)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 12:52:19
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 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 <int> ord;
int 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);
    }
    dfs(1);
    ord.push_back(0);
    for(i = 1 ; i <= m ; i++){
        f >> type >> x;
        if(type == 1){
            f >> y;
            for(j = lo[x] ; j <= hi[x] ; j++)
                sum[j] += y;
        }else{
            for(j = 1 ; j <= n ; j++)
                if(sum[j] == x){
                    g << j << "\n";
                    j = n + 4;
                }
                if(j == n + 1)
                    g << -1 << "\n";
        }
    }

    return 0;
}