#include <iostream>
#include<vector>
#include<utility>
#include<algorithm>


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> I
    ){
    int n=R.size()+1,m=D.size();
    std::vector<long long>R1[n];
    for(int i=0;i<n;i++){
        R1[i].clear();
    }
    for(int i=0;i<R.size();i++){
        R1[R[i].first].push_back(R[i].second);
        R1[R[i].second].push_back(R[i].first);
    }
    std::vector<int>put[m];
    for(int i=0;i<m;i++){
        put[i].clear();
    }
    long long cities[n];
    std::vector<int>path;
    int place;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cities[j]=0;
        }
        cities[S[i]]=1;
        for(int j=1;j<n;j++){
            for(int k=0;k<n;k++){
                if(cities[k]==j){
                    for(int q=0;q<R1[k].size();q++){
                        if(cities[R1[k][q]]==0){
                            cities[R1[k][q]]=j+1;
                        }
                    }
                }
            }
            if(cities[T[i]]>0){
                break;
            }
        }
        path.clear();
        place=T[i];
        for(int j=cities[T[i]];j>=1;j--){
            path.push_back(place);
            for(int k=0;k<R1[place].size();k++){
                if(cities[R1[place][k]]==j-1){
                    place=R1[place][k];
                    break;
                }
            }
        }
        reverse(path.begin(),path.end());
        put[i]=path;
    }
    int whichmove[m];
    for(int i=0;i<m;i++){
        whichmove[i]=0;
    }
    std::vector<int> players[n];
    for(int i=0;i<n;i++){
        players[i].clear();
    }
    std::vector<int>broiach(m,0);
    for(int i=0;i<m;i++){
        broiach[i]=0;
    }
    int minday=D[0];
    for(int i=1;i<m;i++){
        if(D[i]<minday)minday=D[i];
    }
    int longestput=0;
    for(int i=0;i<m;i++){
        if(longestput<D[i]+put[i].size())longestput=D[i]+put[i].size();
    }
    int position[m];
    int stupka[m];
    for(int i=0;i<m;i++){
        position[i]=0;
        stupka[i]=0;
    }
    int rest,closest;
    for(int i=minday;i<longestput;i++){
        rest=0;
        for(int j=0;j<m;j++){
            if(D[j]==i){
                position[j]=put[j][0];
                stupka[j]++;
            }else if(D[j]<i&&put[j].size()>stupka[j]){
                position[j]=put[j][stupka[j]];
                stupka[j]++;
            }else if(put[j].size()==stupka[j]){
                stupka[j]++;
            }else{
                rest++;
            }
        }
        if(rest==m){
            closest=0;
            for(int j=0;j<m;j++){
                if(D[j]>i&&(closest==0||closest>D[j])){
                    closest=D[j];
                }
            }
            i=closest-1;
        }else{
            for(int j=0;j<m;j++){
                if(put[j].size()>=stupka[j]&&D[j]<=i&&I[j]){
                    for(int k=0;k<m;k++){
                        if(put[k].size()>=stupka[k]&&D[k]<=i){
                            if(position[j]==position[k])I[k]=true;
                        }
                    }
                }
            }
            for(int j=0;j<m;j++){
                if(I[j]&&put[j].size()>=stupka[j]&&D[j]<=i)broiach[j]++;
            }
        }
    }
    return broiach;
}