#include "infection.h"
#include <vector>
#include <utility>
#include <bits/stdc++.h>
using namespace std;
vector<int>gr[150016];
inline int dfs(int v, int prr, int g, int cnt){
	if(v == g){
		return cnt;
	}
	for(auto x : gr[v]){
		if(x != prr){
			dfs(x, v, g, cnt + 1);
		}
	}
	return 0;
}
vector<int> solve(
    vector<pair<int, int>> R,
    vector<long long> D,
    vector<int> S,
    vector<int> T,
    vector<bool> I
    
) {
	vector<int>res((int)D.size(), 0);
	for(auto [x, y] : R){
		gr[x].push_back(y);
		gr[y].push_back(x);
	}
	int check = 0;
	for(int i = 0; i < (int)D.size(); ++i){
		if(D[i] != (long long)1000000 * (long long)i){
			check = 1;
			break;
		}
	}
	if(check == 0){
		for(int i = 0; i < (int)D.size(); ++i){
			if(I[i] != 1){
				res[i] = 0;
				continue;
			}
			int lol = dfs(S[i], -1, T[i], 1);
			res[i] = lol;
		}
	}
	return res;
}
