#include "infection.h"
//#include "grader.cpp"
#include <vector>
#include <bits/stdc++.h>
#include <utility>
using namespace std;


int n, m;
vector<vector<int>> g;
vector<vector<int>> path;
bool dfs(int x, int p, int k, int t)
{
    if(x==t)
    {
        path[k].push_back(x);
        //cout << x << endl;
        return 1;
    }
    for(int i=0; i<g[x].size(); i++)
    {
        if(g[x][i]==p)
        {
            continue;
        }
        if(dfs(g[x][i], x, k, t))
        {
            path[k].push_back(x);
            return 1;
        }
    }
    return 0;
}

std::vector<int> solve(
    std::vector<std::pair<int, int>> r,
    std::vector<long long> d,
    std::vector<int> s,
    std::vector<int> t,
    std::vector<bool> b
)
{
    n=r.size()+1;
    m=s.size();
    //cout << n << endl << m << endl;
    path.assign(m, {});
    g.assign(n, {});
    for(int i=0; i<n-1; i++)
    {
        int x, y;
        x=r[i].first;
        y=r[i].second;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    //cout << 67 << endl;
    for(int i=0; i<m; i++)
    {
        dfs(s[i], -1, i, t[i]);
        //cout << 67 << endl;
    }
    for(int i=0; i<m; i++)
    {
        reverse(path[i].begin(), path[i].end());
    }
    /*
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<path[i].size(); j++)
        {
            cout << path[i][j] << " ";
        }
        cout << endl;
    }
    */

    vector<int> ans(m, 0);
    bool o[n][2];
    for(int i=0; i<n; i++)
    {
        o[i][0]=0;
        o[i][1]=0;
    }
    for(int i=0; i<m; i++)
    {
        if(b[i])
        {
            ans[i]=1;
            o[s[i]][0]=1;
        }
        //cout << b[i] << " ";
    }
    for(int i=1; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(i>=path[j].size())
            {
                continue;
            }
            if(o[path[j][i-1]][(i-1)%2] && !b[j])
            {
                b[j]=1;
                ans[j]=1;
            }
            if(b[j])
            {
                ans[j]++;
                o[path[j][i]][i%2]=1;
            }
            //cout << b[j] << " ";
        }
        //cout << endl;
    }
    return ans;
}
