Cod sursa(job #2439161)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 11:30:29
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 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);

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

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

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

    return 0;
}