#include <bits/stdc++.h>
#include "happy.h"
//using namespace std;
bool sel[2005] , ok[2005] ;

std :: vector < std :: pair < int , int > > g[2005] ;

int df[2005] ;

void dfs ( int node , int dist )
{
    ok[node] = 1 ;
    if ( dist != -1 )
        df[node] = dist ;
    else
        df[node] = 0 ;
    for ( auto it : g[node] )
        if ( ok[it.first] == 0 && dist != -1 )
            dfs ( it.first , dist ^ it.second ) ;
        else if ( ok[it.first] == 0 )
            dfs ( it.first , it.second ) ;
}
long long dist[300005] ;

std::vector<long long int> happy (int N, int Q,std::vector<int> u, std::vector<int> v, std::vector<int> h,std::vector<int> t, std::vector<int> l, std::vector<int> r)
{
    std :: vector < long long > answer ;
    for ( int i = 0 ; i < N - 1 ; i ++ )
    {
        g[u[i]].push_back({v[i],h[i]}) ;
        g[v[i]].push_back({u[i],h[i]}) ;
    }
    for ( int i = 0 ; i < Q ; i ++ )
    {
        if ( t[i] == 1 )
        {
            dfs ( u[i] , - 1 ) ;
            for ( int j = 0 ; j < N ; j ++ )
            {
                if ( sel[j] == 1 )
                {
                    dist[j] += df[j] ;
                    dist[u[i]] += df[j] ;
                }
                df[j] = 0 ;
            }
            sel[u[i]] = 0 ;
        }
        else
        {
            dfs ( u[i] , -1 ) ;
            for ( int j = 0 ; j < N ; j ++ )
            {
                if ( sel[j] == 1 )
                    dist[j] -= df[j] ;
                df[j] = 0 ;
            }
            sel[u[i]] = 0 ;
            dist[u[i]] = 0 ;
        }
        long long sum = 0 ;
        for ( int j = 0 ; j < N ; j ++ )
            sum += dist[j] ;
    }
}