#include "happy.h"
#include <bits/stdc++.h>
using namespace std;

void dfs(int cur, int prev, vector<int> &color, vector<vector<pair<int, int> > > &g)
{
    for(pair<int, int> &state : g[cur])
    {
        int next = state.first;

        if(next == prev)continue;
        color[next] = (color[cur] + 1) % 2;
        dfs(next, cur, color, g);
    }
}

vector<long long int> happy (int N, int Q,
vector<int> u, vector<int> v, vector<int> h,
vector<int> t, vector<int> l, vector<int> r)
{
    int n = N;
    vector<int> open(n, 0);
    vector<long long> rez;

    vector<vector<pair<int, int> > > g(n);

    for(int i=0; i<n-1; i++)
    {
        int U = u[i];
        int V = v[i];
        int H = h[i];

        g[U].push_back({V, H});
        g[V].push_back({U, H});
    }

    vector<int> color(n, 0);
    dfs(0, -1, color, g);

    int color1 = 0, color0 = 0;
    for(int &num : color)
    {
        if(num == 1)color1++;
        else color0++;
    }

    int curSum = 0;

    int on0 = 0, on1 = 0;

    for(int q = 0; q < Q; q++)
    {
        int point = l[q];
        int newState = t[q] % 2;

        if(newState == open[point]){rez.push_back(curSum); continue;}

        int curColor = color[point];

        if(curColor == 0 && t[q]%2 == 0)on0--;
        if(curColor == 0 && t[q]%2 == 1)on0++;
        if(curColor == 1 && t[q]%2 == 0)on1--;
        if(curColor == 1 && t[q]%2 == 1)on1++;

        curColor = (curColor + 1) % 2;

        if(curColor == 1)curSum -= on1;
        else curSum -= on0;

        if(curColor == 0)curColor = color0;
        else curColor = color1;

        if(t[q] % 2 == 0)curSum -= curColor;
        else curSum += curColor;

        rez.push_back(curSum);
    }

    return rez;
}