Pagini recente » Cod sursa (job #981858) | Cod sursa (job #2631071) | Cod sursa (job #1137475) | Cod sursa (job #1748771) | Cod sursa (job #2439161)
#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;
}