output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cer...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int N, K; long long dp[500001][2]; vector<pair<int, int>> adj[500001]; void dfs(int node, int par, int costPar) { for (auto it : adj[node]) if (it.first != par) dfs(it.first, node, it.second); dp[n...
### Prompt Please provide a CPP coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int tt; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tt; while (tt--) { int n, k; cin >> n >> k; vector<vector<pair<int, int>>> g(n); for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; ...
### Prompt Please provide a CPP coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; long long t, n, k, t1, t2, t3, dp[500001][2]; vector<pair<long long, long long> > e[500001]; bool v[500001][2]; void dfs(long long g, long long second, long long p) { v[g][second] = 1; long long ch = k - second, cur = 0; vector<pair<long long, pair<long long, long lon...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> const long long MOD = 1000000007; const long long inf = 1e18; using namespace std; std::vector<long long int> fact(400001), inv_fact(400001); long long int modpow(long long int x, long long int n) { long long int res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % M...
### Prompt Construct a Cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const int MaxN = 5e5 + 10; int n, k; vector<pair<int, long long>> e[MaxN]; long long dp[MaxN][2]; void dfs(int i, int parent = -1) { long long sum = 0; vector<long long> v; for (auto p : e[i]) if (p.first != parent) { dfs(p.first, i); sum += dp[p.first...
### Prompt Develop a solution in Cpp to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> const int kInf = 0x3f3f3f3f, kMod = 1e9 + 7; const long long kInf64 = 0x3f3f3f3f3f3f3f3f; template <typename T> inline void UMin(T& x, const T& y) { if (x > y) x = y; } template <typename T> inline void UMax(T& x, const T& y) { if (x < y) x = y; } inline int Add(int a, int b) { return (a +=...
### Prompt Generate a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > v[500005]; long long k, n, dp[500005][2]; map<long long, long long> ed[500005]; void go(long long x, long long par) { vector<pair<long long, long long> > g; for (int i = 0; i < v[x].size(); i++) { if (v[x][i].first != par) { ...
### Prompt Your task is to create a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; int t, n, k, i, x, y, c, nr; long long d[500005][2], w[500005]; vector<pair<int, int> > v[500005]; int cmp(long long a, long long b) { return a > b; } void dfs(int nod, int t) { int i, vecin; d[nod][0] = d[nod][1] = 0; for (i = 0; i < v[nod].size(); i++) { if (v[n...
### Prompt Please formulate a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; using DO = double; using LL = long long; using VI = vector<LL>; const int N = 5e5 + 10; int head[N], nxt[N << 1], to[N << 1]; LL wei[N << 1]; int E; void init(int n) { for (int i = 1; i <= n; i++) { head[i] = -1; } E = 0; } void add(int u, int v, int w) { nxt[E]...
### Prompt Construct a CPP code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAX = 5e5 + 5; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; ...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; const int N = 100010; int inf = 1e9; int mod = 1e9 + 7; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) { int n, k; cin >> n >> k; vector<vector<int>> graph(n + 1), cost(n + 1); for (int i = 1; ...
### Prompt Your task is to create a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; int const N = 500050; vector<pair<int, int>> adj[N]; long long dp[N][2]; int n, k; struct aux { long long dif, z, u; }; bool comp(aux a, aux b) { return a.dif > b.dif; } void solve(int v, int p) { for (pair<int, int> filho : adj[v]) { if (filho.second != p) so...
### Prompt In Cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; int main() { int i, n, k, q; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> q; while (q--) { cin >> n >> k; vector<vector<pair<int, int> > > g(n + 1); for (i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; g[a].push_...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; int n, k; struct Edge { int to, next, w; } e[500010 * 2]; int c1, head[500010]; inline void add_edge(int u, int v, int w) { e[c1].to = v; e[c1].w = w; e[c1].next = head[u]; head[u] = c1++; } long long f[500010], g[500010]; void dfs(int u, int fa) { g[u] = 0; i...
### Prompt Your challenge is to write a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int n, k; int t; vector<pair<int, int>> adj[500005]; long long int dp[2][500005]; vector<pair<long long int, pair<int, int>>> tmp; multiset<long long int> s; void dfs(int i, int p) { for (auto j : adj[i]) if (j.first != p) dfs(j.first, i); tmp.clear(); s.clear(); ...
### Prompt Please formulate a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, int> > > graph(500005, vector<pair<int, int> >()); bool was[500005]; long long int dp[500005][2]; int n, k; void dfs(int v) { was[v] = true; vector<long long int> diffs; long long int sum = 0; for (auto ed : graph[v]) { int u = ed.first; ...
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; struct edge { int to, cost; }; vector<edge> G[N]; int n, k; long long dp[N][2]; vector<long long> vec; void dfs(int v, int p) { for (auto &e : G[v]) { int u = e.to; if (u == p) continue; dfs(u, v); } long long sum = 0; vec.clear...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; const int maxn = int(5e5) + 100; int n, m; vector<pair<int, int> > out[maxn]; int fa[maxn]; long long f[maxn], g[maxn], a[maxn]; void read() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) out[i].clear(); for (int i = 1; i < n; ++i) { int u, v, d; scanf(...
### Prompt Please formulate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const int maxn = 500011; int tot, now[maxn], pre[maxn << 1], son[maxn << 1], v[maxn << 1]; void add(int a, int b, int c) { pre[++tot] = now[a]; now[a] = tot; son[tot] = b; v[tot] = c; } void cc(int a, int b, int c) { add(a, b, c); add(b, a, c); } long long ans =...
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long T; cin >> T; while (T--) { long long N, K; cin >> N >> K; vector<vector<pair<long long, long long>>> L(N + 1); long long dp[N + 1][2]; for (long long i = 1; i <= N; i++) { ...
### Prompt Please create a solution in Cpp to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; long long dp[N][2]; vector<pair<int, int>> g[N]; int k; void dfs(int u, int p) { vector<long long> best; long long sum = 0; for (auto [v, w] : g[u]) { if (v != p) { dfs(v, u); sum += dp[v][0]; best.push_back(dp[v][1] + w - ...
### Prompt Create a solution in CPP for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; int n, k; int f[600007]; long long ans = 0; struct edge { int to; int len; edge() { to = len = 0; } edge(int _to, int _len) { to = _to; len = _len; } }; vector<struct edge> v[600007]; long long dp_all[600007]; long long dp_sub[600007]; bool cmp(pair<long l...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int N = 600000; long long MOD = 1e9 + 7; long long INF = 1e18; const int K = 51; long long dp[N][2]; vector<pair<long long, long long> > gr[...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> const double PI = acos(-1); template <class A, class B> std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) { st << "(" << p.first << ", " << p.second << ")"; return st; } using namespace std; const int MAX = 1 << 20; int n; int fr[MAX], to[MAX], W[MAX]; int cnt[MAX]; std::v...
### Prompt In cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const int MaxN = 5e5 + 55; long long dp[MaxN][2]; vector<pair<int, int> > v[MaxN]; int n, k; void clearAll() { for (int i = 1; i <= n; ++i) { v[i].clear(); dp[i][0] = dp[i][1] = 0; } } void dfs(int x, int p = 0) { vector<long long> curV; for (auto e : v[x]) ...
### Prompt Your challenge is to write a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; struct node { int v, w, next; } edges[maxn << 2]; int head[maxn], cnt; int n, k; void add_edge(int u, int v, int w) { edges[++cnt] = {v, w, head[u]}; head[u] = cnt; } long long dp[maxn][2]; void dfs(int rt, int fa) { vector<long long> vv; ...
### Prompt Develop a solution in CPP to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f3f3f3f3f; const int N = 5e5 + 10; int q, n, k; int u, v, w; vector<int> V[N], G[N]; long long dp[N][2]; void solve(int cur, int fa) { priority_queue<int> Q; dp[cur][1] = dp[cur][0] = 0; for (int i = 0; i < (int)V[cur].size(); i++) { ...
### Prompt Please create a solution in CPP to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; int q, n, m; long long f[500050][2], a[500050]; vector<pair<int, int> > edge[500050]; int cmp(long long x, long long y) { return x > y; } void dfs(int x, int y) { f[x][0] = f[x][1] = 0; for (int i = 0; i < edge[x].size(); i++) if (edge[x][i].first != y) dfs(edge[x][...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const long long inf = 2e9; const long long base = 998244353; long long mod = 1e9 + 7; long long n, k; vector<vector<long long>> g; vector<long long> dp1, dp2; vector<char> used; map<pair<long long, long long>, long long> cost; void dfs(long long v, long long from) { used[...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; int n, k; struct Edge { int v, c, nxt; } e[1000010]; int tot; int first[500010]; void build(int u, int v, int c) { e[++tot] = (Edge){v, c, first[u]}; first[u] = tot; return; } long long f[500010][2]; int tmp[500010]; void dfs(int u, int lst) { for (int i = first[u...
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; vector<vector<int>> conns; vector<vector<int>> costs; size_t n, k; pair<long long, long long> dfs(int pos, int par) { vector<pair<long long, long long>> vec; for (size_t i = 0; i < conns[pos].size(); i++) { if (conns[pos][i] == par) continue; auto p = dfs(conns[...
### Prompt Generate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } long long dp[2][500500]; vector<pair<long lo...
### Prompt Please formulate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const long long MOD = 998244353; long long dp[500005][2]; long long k; vector<vector<pair<long long, long long> > > adj; long long solve(long long idx, bool ep, long long p) { if (dp[idx][ep] != -1) { return dp[idx][ep]; } long long ...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; int Q, N, K; vector<pair<int, int>> graph[500005]; long long dp[2][500005]; void dfs(int n, int p, int val = 0) { vector<long long> v; long long tot = 0; for (auto e : graph[n]) { if (e.first != p) { dfs(e.first, n, e.second); tot += dp[0][e.first]; ...
### Prompt Your task is to create a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; const long long N = 5 * 1e5 + 100; long long t, n, k, dp[N][2]; long long tot, first[N], nxt[N * 2], point[N * 2], len[N * 2]; struct node { long long a, b, kind; }; bool cmp(node a, node b) { return (max(a.a, a.b) > max(b.a, b.b) || (max(a.a, a.b) == max(b.a,...
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + ...
### Prompt Construct a Cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> const int N = 500010; std::vector<std::pair<int, int> > e[N]; long long dp[N][2]; int k; void dfs(int u, int fa) { long long tot = 0; for (auto p : e[u]) { int v = p.first; if (v == fa) { continue; } dfs(v, u); tot += dp[v][1]; } std::vector<long long> vec; f...
### Prompt Construct a cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } const int INF = 2147483600; const int MAXN = 500010; int Q, N, K; int Nod...
### Prompt Please formulate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.se...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; int n, k; vector<vector<pair<int, long long>>> g; pair<long long, long long> dp(int v, int p) { int sz = g[v].size(); vector<pair<long long, pair<long long, long long>>> ans; for (auto t : g[v]) { if (t.first != p) { auto t1 = dp(t.first, v); ans.push_...
### Prompt In cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const long long N = 5e5 + 7; struct Edge { long long v, u, w; }; long long n, k; vector<Edge> g[N]; long long dp[N][2]; long long f(vector<pair<long long, long long>> &arr, long long x) { long long res = 0; vector<long long> d; for (auto elem : arr) { res += ele...
### Prompt Develop a solution in cpp to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ld = long double; constexpr long long mod = 1e9 + 7; constexpr long long Mod = 998244353; constexpr long long MXN = 500000 + 100; constexpr ld EPS = 1e-10; ...
### Prompt Generate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; long long n, k; vector<vector<pair<long long, long long>>> graph; bool comp(pair<long long, long long> a, pair<long long, long long> b) { return (a.first - a.second) > (b.first - b.second); } pair<long long, long long> recur(long long index, long long parent) { long lon...
### Prompt Create a solution in CPP for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; void read(int &n) { int c; for (n = 0; (c = getchar()) > 57 || c < 48;) ; for (; c > 47 && c < 58; c = getchar()) n = n * 10 + c - 48; } int n, k, fir[500100], nex[500100 + 500100], to[500100 + 500100], top, e[500100 + 500100]; long long f[500100][2], a[500100...
### Prompt Please formulate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; long long dp[1000005][2], tmp[1000005], cnt, k; vector<int> son[1000005], val[1000005]; inline bool cmp(int x, int y) { return x > y; } void dfs(int now, int fat) { for (int T, i = 0; i < son[now].size(); ++i) if ((T = son[now][i]) != fat) dfs(T, now); cnt = 0; dp...
### Prompt Please formulate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; int t, n, k; vector<pair<int, long long>> g[500005]; long long dp[500005][2]; void dfs(int v, int p) { vector<pair<long long, long long>> d; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i].first; long long w = g[v][i].second; if (to == p) continue; ...
### Prompt Construct a cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int oo = 0x3f3f3f3f; const int maxn = 500100; long long dp[maxn + 5][2]; int n, ...
### Prompt Please formulate a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 5 * 100002; int k; ll dp0[maxn], dp1[maxn]; vector<pair<int, ll>> g[maxn]; struct el { ll val; int gag; }; bool operator<(const el& a, const el& b) { return a.val > b.val; } void Solve(int v, int p) { vector<el> a; for (auto x ...
### Prompt Please formulate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = 1e9; long long add(long long x, long long y) { return (1ll * x + 1ll * y) % mod; } long long del(long long x, long long y) { return ((1ll * x - 1ll * y) % mod + mod) % mod; } long long mul(long long x, long long y) { re...
### Prompt In Cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 5; int n, q, k, p[MAXN]; vector<pair<int, int> > adj[MAXN]; vector<long long> diff[MAXN]; bool built[MAXN]; long long ans[MAXN][2]; bool found[MAXN][2]; void reset() { for (int i = 0; i <= n; ++i) { adj[i].clear(); diff[i].clear(); built...
### Prompt In CPP, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; template <typename T> void _dbg(const char *_s, T _h) { cerr << _s << " = " << _h << "\n"; } template <typename T, typename... Ts> void _dbg(const char *_s, T _h, Ts... _t) { int _b = 0; while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',') cerr << *_s++; ce...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx,tune=native") using namespace std; const int MOD = 1000000007; const int UNDEF = -1; const int INF = 1 << 30; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ?...
### Prompt In cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; long long n, k; vector<vector<pair<long long, long long>>> graph; bool comp(pair<long long, long long> a, pair<long long, long long> b) { return (a.first - a.second) > (b.first - b.second); } pair<long long, long long> recur(long long index, long long parent) { long lon...
### Prompt Construct a CPP code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; template <class T = int> T read() { T x = 0; char ch = 0; bool f = false; while (!isdigit(ch)) f |= (ch = getchar()) == '-'; for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ '0'); return f ? -x : x; } const int N = 5.1e5; struct E { int v, w; }; vector<E...
### Prompt Please create a solution in cpp to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; using LL = long long; template <class T> int size(T &&x) { return int(x.size()); } template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template <class T> auto operator<<(ostream ...
### Prompt Construct a cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; const int MN = 5e5 + 3, MOD = 1e9 + 7; vector<pii> adj[MN]; ll dp[MN][2]; int k; void dfs(int cur, int p = -1) { vector<pll> poss; for (pii i : adj[cur]) if (i.first ...
### Prompt Please provide a CPP coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const long long N = 5e5 + 10; long long n, t, dp[N][2], k; vector<pair<long long, long long> > nei[N]; bool mark[N]; void dfs(long long v) { vector<long long> p; dp[v][0] = 0; mark[v] = true; for (auto u : nei[v]) { if (mark[u.first]) continue; dfs(u.first);...
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; using pi = pair<int, int>; using ll = long long; const ll mod = 1000000007; vector<pi> v[500005]; vector<ll> t[500005]; int n, k; ll dp[2][500005]; void f(int cur, int par) { dp[0][cur] = dp[1][cur] = 0; t[cur].clear(); ll ret = 0; for (pi p : v[cur]) { int nxt ...
### Prompt Please create a solution in cpp to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; using LL = long long; int nTest, n, K; vector<vector<pair<int, int> > > gr; vector<array<LL, 2> > f; void dfs(int u, int p) { vector<LL> vec; for (auto pai : gr[u]) { int v = pai.first, w = pai.second; if (v == p) continue; dfs(v, u); f[u][0] += f[v][0];...
### Prompt Generate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,fast-math,unroll-loops") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<ll, ll>; template <class T> istream &operator>>(istream &in, vector<T> &a) { for (auto &i : a) in >> i; return in; } templ...
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const int N = 500010; const int M = 1000010; int q, n, k, cnt; int first[N], to[M], nxt[M], val[M], fa[N], son[N]; long long f[N][2], tmp[N]; void add(int a, int b, int c) { to[++cnt] = b, nxt[cnt] = first[a]; first[a] = cnt, val[cnt] = c; return; } void dfs(int x) { ...
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const int twx = 500005; const int inf = 0x3f3f3f3f; long long read() { long long sum = 0; long long flag = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') { flag = -1; } c = getchar(); } while (c >= '0' && c <= '9') { sum ...
### Prompt Please provide a CPP coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; vector<int> E[555555], W[555555]; vector<long long> G[555555]; int T, N, K, A[555555]; long long F[555555][2]; void dfs(int x, int f) { F[x][1] = 0; F[x][0] = 0; int cnt = E[x].size(); for (int i = 0; i < cnt; i++) { int nx = E[x][i]; if (nx != f) { df...
### Prompt Develop a solution in cpp to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, k; long long dp[N]; int diff[N]; vector<pair<int, int>> node[N]; void dfs(int u, int p) { vector<int> adj; int up = 0; long long sum = 0; for (auto j : node[u]) { if (j.first != p) { dfs(j.first, u); sum = sum + dp[j.fir...
### Prompt Your challenge is to write a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int n, q, k; vector<pair<int, int> > v[1000005]; long long dp[1000005][2]; void dfs(int node, int par, int val) { dp[node][0] = 0; dp[node][1] = 0; int cnt = 0; long long sum1 = 0; vector<long long> R; int del = 0; for (auto x : v[node]) { if (x.first != p...
### Prompt In Cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 8; int n, k; vector<pair<long long, long long> > G[maxn]; pair<long long, long long> dp[maxn]; bool cmp(pair<long long, long long>& A, pair<long long, long long>& B) { return A.second > B.second; return A.first - A.second < B.first - B.second; } v...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; const int N = 500005; struct ee { int v; int w; int nxt; } e[N << 1]; int h[N], et = 0; inline void Eadd(int U, int V, int W) { e[++et] = (ee){V, W, h[U]}; h[U] = et; } inline void add(int U, int V, int W) { Eadd(U, V, W); Eadd(V, U, W); } int n, K; long long ...
### Prompt Construct a CPP code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; struct edge { int fr; int to; long long val; int nex; } e[maxn << 1]; int head[maxn], cnt, n, k; long long dp[2][maxn], ans; void adde(int x, int y, long long z) { e[cnt].fr = x; e[cnt].to = y; e[cnt].val = z; e[cnt].nex = head[x]; ...
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const long long MAX = 500001; long long n, k; vector<pair<long long, long long>> adj[MAX]; vector<long long> dpk(MAX); vector<long long> dpkm1(MAX); void solve(long long now, long long prev) { vector<tuple<long long, long long, long long>> nt; for (auto [u, w] : adj[now...
### Prompt Generate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { if (b == 0) return 1LL; long long int p = power(a, b / 2); p = p * p; return (b & 1) ? p * a : p; } const int N = 5e5 + 6; vector<vector<pair<long long int, long long int> > > adj(N); long long int n, k, dp[N][...
### Prompt In cpp, your task is to solve the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors t...
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1e9; const long long LINF = INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 1 << 19; int n, k; vector<pair<long long, long long>> g[N]; long long dp[N][2]; int cmp(pair<long lo...
### Prompt Please formulate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; bool o; char chh, ss[1 << 17], *A = ss, *B = ss; inline char gc() { return A == B && (B = (A = ss) + fread(ss, 1, 1 << 17, stdin), A == B) ? EOF : *A++; } template <typename _Tp> inline void rd(_Tp &...
### Prompt Develop a solution in cpp to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> namespace OI { template <class T> void rd(T &x) { x = 0; int f = 1; char c; while (!isdigit(c = getchar())) if (c == '-') f = -1; do { x = (x << 3) + (x << 1) + (c ^ 48); } while (isdigit(c = getchar())); x *= f; } template <class T> void pt(T x) { if (x < 0) putchar('-'...
### Prompt Your task is to create a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; long long dp[500005][2], l; long long temp[500005]; vector<pair<long long, long long> > adj[500005]; void addedge(long long u, long long v, long long wt) { adj[u].push_back({v, wt}); adj[v].push_back({u, wt}); } void dfs(long long u, long long par) { vector<pair<long ...
### Prompt Create a solution in cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; int n, k; vector<pair<long long, long long> > e[500005]; long long dp[500005][2]; void dfs(int cn, int p) { long long sm = 0; vector<long long> imp; long long up = 0; for (int i = 0; i < (signed)(e[cn].size()); i...
### Prompt Construct a Cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; long long dp[N][2]; vector<int> g[N], cst[N]; int K; int n; void solve(int u, int p, int par_w) { dp[u][1] = par_w; dp[u][0] = 0; vector<long long> add; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i], w = cst[u][i]; if (v ==...
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; const int N = 5e5 + 7; const int MOD = 1e9 + 7; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = x * ...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; long long dp[N][2]; int n, k, q; struct Edge { int to, w; }; vector<Edge> g[N]; bool cmp(Edge &a, Edge &b) { return a.w - dp[a.to][0] + dp[a.to][1] > b.w - dp[b.to][0] + dp[b.to][1]; } void dfs(int u, int f) { for (auto e : g[u]) { if (e.t...
### Prompt Please provide a CPP coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int n, k; vector<pair<int, long long> > v[500005]; long long memo[500005][2]; long long rek(int node, bool used, int par) { if (node and v[node].size() == 1) return 0; if (memo[node][used] != -1) return memo[node][used]; int m = k - used; vector<long long> dif; lo...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; const int N = 500050; int n, k; vector<pair<int, int> > v[N]; long long f[N], g[N]; long long s[N]; int s_top; void dfs(int x, int fa) { for (size_t i = 0; i < (v[x]).size(); ++i) { int y = v[x][i].first; if (y == fa) continue; dfs(y, x); } s_top = 0; lo...
### Prompt Generate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; void enable_comma() {} string tostring(char c) { string s = ""; s += c; return s; } string tostring(string s) { return "\"" + s + "\""; } string tostring(const char *c) { return tostring((string)c); } string tostring(long long x) { if (x < 0) return "-" + tostring(-...
### Prompt Please provide a cpp coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int tc; long long n, k, dp[N][2]; vector<pair<int, int>> g[N]; void init() { for (int i = 1; i <= n; ++i) g[i].clear(), dp[i][0] = dp[i][1] = 0; } void dfs(int u, int p) { vector<pair<long long, pair<long long, long long>>> v; for (auto &c : g[u...
### Prompt Construct a cpp code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; long long dp[N][2]; vector<pair<int, int>> adj[N]; int n, k; void dfs(int u, int par, int parcost) { int extra = adj[u].size() - k; vector<long long> excess; long long ans = 0; for (auto pr : adj[u]) { int v = pr.first; int w = pr.seco...
### Prompt Please create a solution in Cpp to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; void debug(vector<int> &v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } template <typename T> vector<T> read_t() { int n; cin >> n; vector<T> R; for (int i = 0; i < n; i++) { T a; cin >> a; R.push_back(a); } ...
### Prompt Please formulate a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const int MAX = 1000005; const int INF = 1e9; int q, n, k; vector<pair<int, int> > ady[MAX]; long long dp[MAX][2]; void clean() { for (int i = 1; i <= n; i++) { ady[i].clear(); } } void solve(int x, int p) { long long total = 0; vector<long long> sol; for (int...
### Prompt Construct a CPP code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const int MAXN = 500005; const long long mod = 1e9 + 7; long long dp[MAXN][2]; vector<pair<int, long long>> edges[MAXN]; int indeg[MAXN]; long long dfs(int cur, int par, int take) { if (dp[cur][take] != -1) return dp[cur][take]; vector<pair<long long, long long>> res; ...
### Prompt Develop a solution in Cpp to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; const long long inf = (long long)1e16; const long double eps = 1e-12; const long long N = (long long)5e5 + 5; const long long LOGN = 19; const long double PI = 3.14159265358979323846; inline long long mul(long long a, long long b, l...
### Prompt Please formulate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") const long long INF = 0x3f3f3f3f3f3f3f3f; const long long llinf = (1LL << 62); const int inf = (1 << 30); const int nmax = 5e5 + 50; const int mod = 1e9 + 7; using namespace std; int q, i, n, x, y...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; auto start = std::chrono::system_clock::now(); inline void skj() { std::chrono::time_point<std::chrono::system_clock> end; end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::time_t end_time = std::chrono::system...
### Prompt Your task is to create a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<pair<int, int>> adj[500000]; int k; pair<ll, ll> dfs(int u, int p) { pair<ll, ll> res; vector<ll> neighbors; for (auto [c, w] : adj[u]) { if (c != p) { auto chd = dfs(c, u); neighbors.push_back(chd.second + w - chd.first); ...
### Prompt Create a solution in CPP for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; int n, q, k; vector<int> tree[1234567]; vector<long long> w[1234567]; long long dp[1234567][2]; void dfs(int v, int f) { for (int i = 0; i < tree[v].size(); i++) if (tree[v][i] != f) dfs(tree[v][i], v); vector<int> vv; long long sum = 0; for (int i = 0; i < tree...
### Prompt Generate a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > G[600025]; int n, u, to, w, k; long long p[600025], q[600025]; void link(int u, int to, int w) { G[u].push_back(pair<int, int>(to, w)); G[to].push_back(pair<int, int>(u, w)); } bool cmp(pair<int, int> a, pair<int, int> b) { return p[a.first] + ...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& x) { char c; bool f = 0; while ((c = getchar()) < '0' || '9' < c) f |= (c == '-'); for (x = (c ^ 48); '0' <= (c = getchar()) && c <= '9'; x = (x << 1) + (x << 3) + (c ^ 48)) ; if (f) x = -x; } template <class T> in...
### Prompt Please provide a Cpp coded solution to the problem described below: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { bool b = 0; char c; while (!isdigit(c = getchar()) && c != '-') ; if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48; if (b) x = -x; } template <typ...
### Prompt Generate a cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to e...
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long n, k; vector<vector<pair<long long, long long>>> graph; pair<long long, long long> DFS(long long node, long long...
### Prompt Construct a CPP code solution to the problem outlined: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors...
#include <bits/stdc++.h> using namespace std; const long long maxn = 5e5 + 5; struct Edge { long long nxt, to, w; } e[maxn << 1]; long long hd[maxn], e_cnt; void add(long long u, long long v, long long w) { e[++e_cnt] = (Edge){hd[u], v, w}; hd[u] = e_cnt; } long long n, k, f[maxn][2], deg[maxn]; void dfs(long lon...
### Prompt Create a solution in Cpp for the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k colors to...
#include <bits/stdc++.h> using namespace std; int Case, n, k, tot, Head[500000 + 5], Deg[500000 + 5]; bool Vis[500000 + 5]; long long ans, Dp[500000 + 5][2]; vector<long long> Vec[500000 + 5]; struct Edge { int next, node, w; } h[500000 + 5 << 1]; inline void addedge(int u, int v, int w) { h[++tot].next = Head[u], ...
### Prompt Your task is to create a CPP solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly...
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "( " << v.first << ", " << v.second << ")"; return o...
### Prompt Your challenge is to write a Cpp solution to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exa...
#include <bits/stdc++.h> using namespace std; int n, k; vector<pair<int, int> > nei[500005]; long long dp0[500005], dp1[500005]; void Dfs(int v, int par) { multiset<long long> mx; long long sum = 0; for (int i = 0; i < nei[v].size(); ++i) { int to = nei[v][i].first; if (to == par) continue; Dfs(to, v)...
### Prompt Please create a solution in cpp to the following problem: You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices u_i and v_i are connected by an edge with weight w_i. Let's define the k-coloring of the tree as an assignment of exactly k col...