Cod sursa(job #2439166)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 11:41:36
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 100005;
vector <int> v[NMAX];
int n,m,father[NMAX],val[NMAX];
bool ok;

void dfs(int nod){
    for(auto it: v[nod])
        if(it != father[nod]){
            father[it] = nod;
            dfs(it);
        }
}

void DFS(int nod, int value){
    val[nod] += value;
    for(auto it: v[nod])
        if(father[nod] != it){
            DFS(it,value);
        }
}

void Dfs(int nod, int value){
    if(val[nod] == value){
        g << nod << "\n";
        ok = 1;
    }else{
        for(auto it: v[nod])
            if(it != father[nod]){
                if(!ok)
                    Dfs(it,value);
                else
                    break;
            }
    }
}

int main(){
    int i,j,x,y,type;
    f >> n >> m;
    for(i = 1 ; i < n; i++){
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);

    for(i = 1 ; i <= m ; i++){
        f >> type;
        if(type == 1){
            f >> x >> y;
            DFS(x,y);
        }else{
            ok = 0;
            f >> x;
            Dfs(1,x);
        }
    }

    return 0;
}