Pagini recente » Cod sursa (job #2593947) | Cod sursa (job #1162913) | Cod sursa (job #2324480) | Istoria paginii runda/pre-oji | Cod sursa (job #1263799)
/// Craciun Catalin
/// Arbore
#include <iostream>
#include <fstream>
#include <vector>
#define NMax 100005
using namespace std;
ifstream f("arbore.in");
ofstream g("arbore.out");
long long n,m;
vector<long long> V[NMax];
long long P[NMax];
long long sume[NMax];
void firstOp(long long sef, long long suma) {
sume[sef] += suma;
}
long long sumFor(long long x) {
long long sum = sume[x];
while (P[x] != x) {
sum += sume[P[x]];
x = P[x];
}
return sum;
}
long long secondOp(long long suma) {
for (int i=1;i<=n;i++)
if (sumFor(i) == suma)
return i;
return -1;
}
void read() {
f>>n>>m;
for (long long i=1;i<=n;i++)
P[i] = i;
for (long long i=1;i<=n-1;i++) {
long long x, y;
f>>x>>y;
V[x].push_back(y);
P[y] = x;
}
for (long long j=1;j<=m;j++) {
long long type;
f>>type;
if (type == 1) {
long long sef, suma;
f>>sef>>suma;
firstOp(sef, suma);
} else if (type == 2) {
long long suma;
f>>suma;
g<<secondOp(suma)<<'\n';
}
}
f.close();
g.close();
}
int main() {
read();
return 0;
}