Codeforces · 2195E

Idiot First Search

This C++ solution uses tree dynamic programming for Codeforces 2195E Idiot First Search. Read the reasoning, inspect the code, or try your own test case below.

2195EDynamic programmingTree dynamic programmingC++58 lines
Solution113of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Tree dynamic programming

Idiot First Search tree input and required tree DP match the implementation.

Dynamic programming

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

E_Idiot_First_Search.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    const int inf = 1e9;
    const long long INF = 1e17; //❄️
    const int mod = 1e9 + 7;
    void solve() {
        int n; cin >> n;
        vector<int> lft(n + 1), rit(n + 1), par(n + 1);
        for (int i = 1; i <= n; i++) {
            cin >> lft[i] >> rit[i];
            if (lft[i] != 0) par[lft[i]] = i;
            if (rit[i] != 0) par[rit[i]] = i;
        }
        par[1] = 0;
        vector<int> order;
        stack<int> stk;
        stk.push(1);
        while (!stk.empty()) {
            int v = stk.top(); stk.pop();
            order.push_back(v);
            if (lft[v] != 0) stk.push(lft[v]);
            if (rit[v] != 0) stk.push(rit[v]);
        }
        vector<ll> par_cost(n + 1);
        for (int i = order.size() - 1; i >= 0; i--) {
           if (lft[order[i]] == 0 && rit[order[i]] == 0) {
            par_cost[order[i]] = 1;
            } else {
            par_cost[order[i]] = (par_cost[lft[order[i]]] + par_cost[rit[order[i]]] + 3) % mod;
            }
        }
           vector<ll> root_cost(n + 1);
           root_cost[1] = par_cost[1];
           stk.push(1);
           while (!stk.empty()) {
            int v = stk.top(); stk.pop();
            if (lft[v] != 0) {
                root_cost[lft[v]] = (root_cost[v] + par_cost[lft[v]]) % mod;
                stk.push(lft[v]);
            }
            if (rit[v] != 0) {
                root_cost[rit[v]] = (root_cost[v] + par_cost[rit[v]]) % mod;
                stk.push(rit[v]);
            }
        }
        for (int i = 1; i <= n; i++) {
            cout << root_cost[i] << " \n"[i == n];
        }
    }
    int main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int t; cin >> t;
        while (t--) {
            solve();
        }
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.