Source Code
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>

#include<cstring>
#include<sstream>
using namespace std;
long long  vis1[100005],vis2[100005],mod=1000000007;
vector<pair<long long ,long long >>adj1[100005],adj2[100005];
vector<pair<long long ,long long >>binary1(30),binary2(30);
void calBinary(long long n,bool tree){
    string s="";

    while(n){
        if(n%2==0)s+='0';
        else s+='1';
        n/=2;
    }
    long long  i=0;
    for(long long  c=0;c<=3;c++)
    {

        if(tree){
            if(i>=s.size())
                binary1[c].second++;
            else if(s[i]=='1')
                binary1[c].first++;
            else if(s[i]=='0')
                binary1[c].second++;
        }
        else {
            if(i>=s.size())
                binary2[c].second++;
            else if(s[i]=='1')
                binary2[c].first++;
            else if(s[i]=='0')
                binary2[c].second++;
        }
        i++;
    }

}
void dfs(long long  node,long long weight,long long  tree){
    if(tree==1) {
        vis1[node] = weight;
        for(int i=0;i<adj1[node].size();i++){
            if(vis1[adj1[node][i].first]==-1){
                dfs(adj1[node][i].first,(weight^adj1[node][i].second),tree);
            }
        }
    }
    else if(tree==2) {
        vis2[node] = weight;
        for(int i=0;i<adj2[node].size();i++){
            if(vis2[adj2[node][i].first]==-1){
                dfs(adj2[node][i].first,(weight^adj2[node][i].second),tree);
            }
        }
    }

}
long long  GetAns(long long x,long long y,long long j){
    long long c=0;

    if((x==0&&y==0)||(x==1&&y==1)){
        c+=((binary1[j].first)*(binary2[j].second));
        c+=(binary1[j].second*binary2[j].first);
    }
    else if((x==0&&y==1)||(x==1&&y==0)){
        c+=binary1[j].first*binary2[j].first;
        c+=binary1[j].second*binary2[j].second;
    }
    return c;
}
int main() {
    std::ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    long long n, u, v, w;
    cin >> n;


    for (long long  i = 0; i < n - 1; i++) {
        cin >> u >> v >> w;
        adj1[u].push_back({v, w});
        adj1[v].push_back({u, w});
    }
    for (long long  i = 0; i < n - 1; i++) {
        cin >> u >> v >> w;
        adj2[u].push_back({v, w});
        adj2[v].push_back({u, w});
    }
    memset(vis1, -1, sizeof vis1);
    memset(vis2, -1, sizeof vis2);
    dfs(1, 0, 1);
    dfs(1, 0, 2);
    for (int i = 1; i <= n; i++) {
        calBinary(vis1[i], 1);
        calBinary(vis2[i], 0);
    }


    long long ans = 0, d = 1, z;
    for (long long  i = 0; i <= 30; i++) {
        z = 0;
        for (long long  j = 1; j <= n; j++) {
            long long x, y;
            x = (vis1[j] >> i) & 1, y = (vis2[j] >> i) & 1;
            z = GetAns(x, y, i);
            ans =((ans%mod)+ ((z%mod) * (d%mod))%mod)%mod;
        }
        d *= 2;
    }
    cout << ans;

}
Copy
Trees xOr AyaHamza
GNU G++17
13 ms
6.5 MB
Wrong Answer