Cod sursa(job #3316587)

Utilizator PetreIonutPetre Ionut PetreIonut Data 19 octombrie 2025 13:01:37
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda cex_01 Marime 0.87 kb
#include <bits/stdc++.h>
#define NMAX 100005
#define mod 666013
#define ll long long

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

using namespace std;

int n, q, p, T;
int a[NMAX];
vector<int> muchii[NMAX];
bool ramai;

void update(){
    int x, y;
    f >> x >> y;
    a[x]+=y;
}

void dfs(int nod, int sum){
    if(ramai==1) return;
    sum+=a[nod];
    if(sum==T){g << nod << '\n';ramai=1;return;} 
    for(auto i:muchii[nod]){
        dfs(i, sum);
    }
    sum-=a[nod];
}

void solve(){
    f >> T;
    ramai=0;
    dfs(1, 0); 
    if(ramai==0) g << -1 << '\n';
}

int main(){
    f >> n >> q;
    for(int i=1;i<n;i++){
        int x, y;
        f >> x >> y;
        muchii[x].push_back(y);
    }
    for(int i=1;i<=n;i++) sort(muchii[i].begin(), muchii[i].end());
    for(int i=1;i<=q;i++){
        int cer;
        f >> cer;
        (cer==1)?update():solve();
    }
}