Pagini recente » Cod sursa (job #336277) | Cod sursa (job #3188033) | Cod sursa (job #3241398)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5+2;
using pii = pair<int, int>;
int n,q,k,ord[NMAX],val[NMAX];
vector<int> v[NMAX];
pii ranges[NMAX];
ifstream fin("arbore.in");
ofstream fout("arbore.out");
void dfs(int nod, int tata = -1){
ord[++k] = nod;
ranges[nod] = {k, k};
for(int fiu: v[nod]){
if(fiu == tata){
continue;
}
dfs(fiu, nod);
}
ranges[nod].second = k;
}
int main()
{
fin >> n >> q;
for(int i = 1; i < n; i++){
int x, y;
fin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
while(q--){
int t, p, s;
fin >> t;
if(t == 1){
fin >> p >> s;
// update(p, s);
auto [l, r] = ranges[p];
for(int j = l; j <= r; j++){
val[ord[j]] += s;
}
}else{
fin >> s;
// find(s);
bool found = false;
for(int i = 1; i <= n; i++){
if(val[i] == s){
fout << i << "\n";
found = true;
break;
}
}
if(!found){
fout << "-1\n";
}
}
}
return 0;
}