Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Those who cannot remember the past are
- // condemned to repeat it (use DP -_-)
- // - George Santayana
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define ll long long
- #define ull unsigned long long
- #define MOD 1000000007
- #define PI 3.14159265
- #define ceil(a, b) (((a) / (b)) + ((a) % (b) ? 1 : 0))
- #define imin INT_MIN
- #define imax INT_MAX
- #define llmax LLONG_MAX
- #define llmin LLONG_MIN
- #define inf 2000000000
- #define nl '\n'
- #define ppcnt __builtin_popcount
- #define ppcntll __builtin_popcountll
- #define clz __builtin_clz
- #define clzll __builtin_clzll
- #define ctz __builtin_ctz
- #define ctzll __builtin_ctzll
- #define modulo(a, b, mod) ((((a) % (mod)) + ((b) % (mod))) % (mod))
- #define cnte(v, x) count(all(v), (x))
- #define mine(v) min_element(all(v))
- #define maxe(v) max_element(all(v))
- #define updmin(a, b) a = min(a, b)
- #define updmax(a, b) a = max(a, b)
- #define finmod(x, m) x = ((x) % (m) + (m)) % m
- #define debug(x) cout << "x: " << (x) << nl;
- #define debug2(x, y) cout << "x: " << (x) << " y: " << y << nl;
- #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
- #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
- //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
- //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
- template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
- for (auto& [x, y] : v) in >> x >> y;
- return in;
- }
- template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
- for (T& i : v) in >> i;
- return in;
- }
- template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
- for (const T& x : v)
- out << x << ' ';
- return out;
- }
- template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
- for(auto& [x, y] : v){
- out << x << ' ' << y << nl;
- }
- return out;
- }
- void Start_Crushing() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- vector<vector<pair<int, int>>> tree_1, tree_2;
- vector<int> one_1, one_2, zero_1, zero_2;
- vector<bool> vis;
- void dfs(int src, int st, int curr, bool one, vector<vector<pair<int, int>>>& tree){
- for(int bit = 0; bit < 31; bit++){
- if(curr & (1 << bit)){
- (one ? one_1 : one_2)[bit]++;
- }else{
- (one ? zero_1 : zero_2)[bit]++;
- }
- }
- vis[src] = true;
- for(auto& [nxt, wgt] : tree[src]){
- if(not vis[nxt]){
- dfs(nxt, st, curr ^ wgt, one, tree);
- }
- }
- }
- void solve(){
- int n; cin >> n;
- tree_1.assign(n + 5, vector<pair<int, int>>());
- tree_2.assign(n + 5, vector<pair<int, int>>());
- one_1.resize(32), one_2.resize(32), zero_1.resize(32), zero_2.resize(32);
- for(int i = 0; i < 2; i++){
- for(int j = 0; j < n - 1; j++){
- int u, v, w; cin >> u >> v >> w;
- (i & 1 ? tree_2 : tree_1)[u].emplace_back(v, w);
- (i & 1 ? tree_2 : tree_1)[v].emplace_back(u, w);
- }
- }
- // we should count the number of ways in which we can set this bit to one in both trees
- // and then, I think we can multiply the two frequencies by each others
- for(int i = 1; i <= n; i++){
- vis.assign(n + 5, false);
- dfs(i, i, 0, true, tree_1);
- vis.assign(n + 5, false);
- dfs(i, i, 0, false, tree_2);
- }
- int sum = 0;
- debug(one_1)
- debug(zero_1)
- debug(one_2)
- debug(zero_2)
- for(int i = 1; i <= n; i++){
- for(int j = 1; j <= n; j++){
- for(int k = 1; k <= n; k++){
- if(i == j and j == k)
- continue;
- cout << i << ' ' << j << ' ' << k << nl;
- }
- }
- }
- }
- int main(){
- Start_Crushing();
- int t = 1;
- // /*Multiple test cases?*/ cin >> t;
- while (t--) {
- solve();
- if(!t)
- break;
- cout << nl;
- }
- // for(int tc = 1; tc <= t; tc++){
- // cout << "Case #" << tc << ": ";
- // solve();
- // if(tc != t)
- // cout << nl;
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement