Pagini recente » Diferente pentru utilizator/funnystocky intre reviziile 196 si 113 | Monitorul de evaluare | Cod sursa (job #726731)
Cod sursa(job #726731)
// http://infoarena.ro/problema/arbore
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXSIZE = 100001;
ifstream in("arbore.in");
ofstream out("arbore.out");
int nodes,ops,sum;
int money[MAXSIZE];
vector<int> graph[MAXSIZE];
vector<int> leafs;
void depthFirst(int node);
int main() {
in >> nodes >> ops;
int from,to;
for(int i=1;i<nodes;i++) {
in >> from >> to;
graph[from].push_back(to);
}
for(int i=1;i<nodes;i++)
if(graph[i].empty())
leafs.push_back(i);
int type = 0,node = 0;
for(int i=1;i<=ops;i++) {
in >> type;
if(type == 1) {
in >> node >> sum;
depthFirst(node);
}
else {
in >> node;
for(int k=1;k<=nodes;k++)
if(money[k] == node) {
out << k << "\n";
break;
}
}
}
return (0);
}
void depthFirst(int node) {
money[node] += sum;
for(int i=0;i<graph[node].size();i++)
depthFirst(graph[node][i]);
}