We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: tt12
Now with the condition on consecutive moves, the optimal way of moving would be to move equal number of 'L' and 'U' moves in an alternating manner. We would then be left with only one type of move to perform, say 'L' (therefore, k1 > k2 and hence cost till now = k2*l + k2*u). We would have now reached the 'y' coordinate of the destination.
Finally, to move to the 'x' coordinate of the destination, we have to perform the remaining k1 — k2 'L' moves. Since these moves are not allowed to be consecutive, we must perform either a 'U' move or a 'D' move after every 'L' move such that the 'y' coordinate remains unchanged at the end. Hence during these set of moves, |U| = |D|. Hence, (k1 — k2) / 2 moves both of the type 'U' and 'D' must be performed along with k1 — k2 'L' moves.
Final cost = k2*l + k2*u + (k1 — k2)*l + (k1 — k2)/2 * (u + d).
To reduce the complexity of solving multiple types of cases, one can rotate the graph by swapping values of {x,y}, {l,r}, {u,d} accordingly so as to always solve for a specific type of solution.
Author: vrintle
Let's consider current bit count to be $$$x$$$, so let's mark nodes with bit count $$$x$$$ in red color, and the other nodes in black color. So, this problem boils down to finding maximum distance between any two red colored nodes (a similar codeforces problem). The answer to this sub problem is the diameter of this red-black tree. Note that, in this version, the diameter ends will always be red nodes.
Time complexity: $$$\mathcal{O}(n \cdot \log{n})$$$
PS: I have thought about this partial diameter problem before the actual div3 contest linked above.
include <bits/stdc++.h>
using namespace std;
define int int64_t
define endl '\n'
define sz(v) ((int) v.size())
define all(v) v.begin(), v.end()
define vi vector
define pii pair<int, int>
define inf 2e18
int max(int x, int y) { return x > y ? x : y; } int min(int x, int y) { return x < y ? x : y; }
const int N = 2e5 + 7; vi g[N];
void solve() { int n; cin >> n; vi a(n); for(int i = 0; i < n; i++) { cin >> a[i]; a[i] = __builtin_popcount(a[i]); g[i].clear(); } for(int _ = 0; _ < n — 1; _++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } int ans = 0; for(int b = 1; b <= 20; b++) { vi dist(n, inf); function<void(int, int)> dfs = [&](int u, int p) { for(auto& v: g[u]) { if(v == p) continue; dist[v] = dist[u] + 1; dfs(v, u); } }; for(int i = 0; i < n; i++) { if(a[i] == b) { dist[i] = 0; dfs(i, -1); break; } } int mx = 0; for(int i = 0; i < n; i++) { if(a[i] == b) mx = max(mx, dist[i]); } for(int i = 0; i < n; i++) { if(a[i] == b && dist[i] == mx) { dist.assign(n, inf); dist[i] = 0; dfs(i, -1); break; } } for(int i = 0; i < n; i++) { if(a[i] == b) ans = max(ans, dist[i]); } } cout << ans; }
int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef VRINTLE freopen("./tests/tree_27.txt", "r", stdin); freopen("./out.txt", "w", stdout); freopen("./err.txt", "w", stderr); #endif int t = 1; cin >> t; while(t--) { solve(); cout << endl; } return 0; } ```



