Cod sursa(job #2439157)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 11:24:45
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
const int SMAX = 1000005;
int n,m;
vector<int> v[NMAX];
int vf[SMAX], father[NMAX], val[NMAX];
bool viz[NMAX];

void dfs(int nod){
    viz[nod] = 1;
    for(auto it: v[nod])
        if(!viz[it]){
            father[it] = nod;
            dfs(it);
        }
}

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

    dfs(1);

    for(i = 1 ; i <= m ; i++){
        f >> type;
        if(type == 1){
            f >> x >> y;
            val[x] += y;

            cx = x;
            while(cx != 1){
                cx = father[cx];
                val[x] += val[cx];
            }

            vf[val[x]] = x;
        }else{
            f >> x;
            if(vf[x])
                g << vf[x] << "\n";
            else
                g << -1 << "\n";
        }
    }

    return 0;
}