Cod sursa(job #1263929)

Utilizator catalincraciunCraciun Catalin catalincraciun Data 15 noiembrie 2014 11:50:55
Problema Arbore Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
/// Craciun Catalin
///  Arbore
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

const int NMax = 100005;

using namespace std;

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

long long n,m;
long long P[NMax];
long long sume[NMax];

void firstOp(long long sef, long long suma) {
    sume[sef] += suma;
}

long long secondOp(long long suma) {

    for (long long i=1;i<=n;i++) {
        long long sum = 0; int pos;
        for (pos = i; P[pos] != pos; pos = P[pos]) sum+=sume[pos]; sum+=sume[pos];

        if (sum == 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;i++) {
        long long x, y; f>>x>>y;
        if (x > y) {
            long long aux = x;
            x = y;
            y = aux;
        }

        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;
}