output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int INF = 1000 * 1000 * 1000 + 7; const long long LINF = INF * (long long)INF; const int MAX = 5 * 1000 * 100 + 47; struct T { T() {} T(pair<long long, long long> val1, long long w1) { val = val1; w = w1; } pair<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; bool chkmin(int &x, int y) { return x > y ? x = y, 1 : 0; } bool chkmax(int &x, int y) { return x < y ? x = y, 1 : 0; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch...
### 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, int> > vec[500005]; long long dp[500005][2]; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return a.first - a.second > b.first - b.second; } void dfs(int s, int par = 0) { vector<pair<long long, long long> > tmp; for...
### 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; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } const int maxn = 510000; long long dp[maxn][2]; vector<pair<int, int> > e[maxn]; int n, k; void dfs(int v, 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; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; } template <class T> inline voi...
### 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 dp[500005][4], n, k; vector<pair<long long, long long> > v[500005]; void solve(int ver, int par) { vector<pair<long long, long long> > cur; long long tok = 0, ans = 0, l = -1; for (int i = 0; i < v[ver].size(); i++) { if (v[ver][i].first == par) continue...
### 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 long long INF = 1e9 + 9; long long n, k; vector<vector<pair<long long, long long> > > g; vector<long long> used; vector<vector<long long> > dp; void dfs(long long v) { used[v] = 1; long long cur = 0; vector<long long> can; for (auto ix : g[v]) { long long ...
### 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 maxi = 1e6 + 10; int a[maxi]; string s; int n, k; vector<pair<int, int>> v[maxi]; long long dp[2][maxi]; void dfs(int x, int pred) { vector<pair<long long, pair<int, int>>> values; dp[0][x] = 0; dp[1][x] = 0; for (auto i : v[x]) if (i.first != pred) { ...
### 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, K; vector<pair<int, int> > adj[MAXN]; long long dp[MAXN][2]; void load() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; i++) adj[i].clear(); for (int i = 1; i < N; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); adj[u].p...
### 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; struct Edge { int val, to, nxt; } edge[2 * 500005]; int head[500005]; int tot = 0; int n, k; void init() { for (int i = 1; i <= n; i++) { head[i] = -1; } tot = 0; } void addedge(int u, int v, int value) { edge[tot].nxt = head[u]; head[u] = tot; edge[tot].t...
### 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") using namespace std; const int mod0 = 1e9 + 7; const long long mod1 = 998244353; const long long mod2 = 1e9 + 9; const long long mod3 = 2147483647; const int sz = 256 * 1024; const long long inf = 2 * 1024 * 1024 * 1023 - 1; const long long INF = inf * inf; const d...
### 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 = 5e5 + 10; int T, n, k; int final[N], nex[N * 2], to[N * 2], tot; int w[N * 2]; long long f[N][2]; void link(int x, int y, int ww) { to[++tot] = y, nex[tot] = final[x], final[x] = tot; w[tot] = ww; } vector<long long> a; void dfs(int x, int fa) { for (int...
### 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, K; vector<pair<int, int> > G[5 << 17]; pair<long long, long long> dfs(int u, int p) { vector<long long> A; long long ret = 0; for (const pair<int, int>& q : G[u]) { if (q.first == p) continue; pair<long long, long long> r = dfs(q.first, u); ret +...
### 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 int N = 5e5 + 5; int n, k, T; const long long INF = 1e18; std::vector<std::pair<int, int> > g[N]; long long dp[N][2]; void dfs(int u, int fa) { dp[u][0] = dp[u][1] = 0; std::vector<long long> f; for (std::pair<int, int> c : g[u]) if (c.first != fa) { int v = c.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 long double PI = acos(-1.0); const long long N = 5e5 + 4; vector<pair<long long, long long> > adjlist[N]; long long dp[N][2]; long long n, k; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return a.first - a.second > b.first - b.second; } voi...
### 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 maxn = 500009; long long t, n, k; vector<pair<long long, long long> > edges[maxn]; long long dp[maxn][3]; void dfs(long long pos, long long par, long long nia) { if (dp[pos][nia] != -1) return; dp[pos][nia] = 0; vector<pair<long long, long long> > vec;...
### 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 oo = 1e18; const int maxn = 5e5 + 5; int T, n, k; vector<pair<int, int>> adj[maxn]; long long dp[maxn][2]; void dfs(int u, int p) { vector<pair<long long, int>> st; for (int i = 0; i < ((int)adj[u].size()); i++) { auto v = adj[u][i]; if (v.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> using namespace std; using ll = long long; using ii = pair<int, int>; const int maxn = 5e5 + 5; int n, k; vector<ii> g[maxn]; ll dp[maxn][2]; void solve(int u, int par) { for (ii e : g[u]) { int v = e.first; ll w = e.second; if (v - par) { solve(v, u); } } ll tot0 = ...
### 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 INF = 1e18 + 1; const long long MOD = 1e9 + 7; const long long MAXN = 5e5 + 5; const long double EPS = 1e-12; const long double PI = acos(-1); const long long XX[] = {0, -1, 0, 1, 0, -1}; const long long YY[] = {-1, 0, 1, 0, -1, 0}; const long long ALPH = 31...
### 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; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) ret...
### 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; long long mod = 1000000007LL; long long large = 2000000000000000000LL; int n, k; vector<vector<pair<int, int> > > adj; vector<vector<long long> > dp; vector<bool> vis; long long dfs(int u, int pa, int t) { if (dp[u][t] != -1) return dp[u][t]; vector<long long> ch; 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; const long long inf = (long long)1e18; const long long mod = (long long)1e9 + 7; const double eps = (double)1e-9; const double pi = acos(-1.0); const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; const int N = 500500; int n, k; vector<pair<int, int> > g[N]...
### 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; int n, k, t; vector<pair<int, int> > v[500002]; long long dp[3][500002]; int cc[500002]; void dfs(int dad, int nod) { vector<pair<int, int> > c; vector<long long> sp, sp2, sp3; for (int i = 0; i < v[nod].size(); ++i) { int vecin = v[nod]...
### 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; using edgevec = vector<vector<pair<int, long long>>>; pair<long long, long long> dfs(int curr, int prev, int k, edgevec &e) { vector<long long> v; long long all = 0; for (auto &iter : e[curr]) { auto to = iter.first; if (prev == to) continue; auto w = iter...
### 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 mxi = 5e5 + 666; const long long mod = 1e9 + 7; long long n, k, te, dp[mxi][2]; vector<pair<long long, long long> > t[mxi]; void dfs(int v, int par = -1) { dp[v][0] = dp[v][1] = 0; long long sum = 0; vector<long long> c; for (auto x : t[v]) { long long...
### 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 long long N = 500010; inline long long read() { long long s = 0, w = 1; register char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch =...
### 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 T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return 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; int n, k; struct edge { int next, to, len; edge(int next, int to, int len) : next(next), to(to), len(len) {} edge() {} } e[600019 << 1]; int cnt = 1, h[600019]; void Add_Edge(int x, int y, int z) { e[++cnt] = edge(h[x], y, z); h[x] = cnt; e[++cnt] = edge(h[y], x...
### 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 = 500500; int n, k; long long dp[N][2]; vector<pair<int, int> > adj[N]; long long get(int v, int p, int par) { long long& ans = dp[v][p]; if (ans != -1) { return ans; } ans = 0; long long sum = 0; vector<long long> ot; for (pair<int, int> e : a...
### 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 mod = 1e9 + 7; const int maxn = 5e5 + 10; int k; long long dp[maxn][2]; vector<pair<int, int> > t[maxn]; void dfs(int v, int par = -1) { dp[v][0] = dp[v][1] = 0; long long sum = 0; vector<long long> c; for (auto edge : t[v]) { int u = edge.first, w = e...
### 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 q; vector<pair<int, int>> edges[500005]; long long dp[500005][2]; void dfs(int u, int p) { dp[u][0] = 0; vector<long long> ws; ws.clear(); for (int i = 0; i < edges[u].size(); ++i) { int v = edges[u][i].first; int w = edges[u][i].second; if...
### 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 long long N = 1e6 + 100; vector<pair<long long, long long> > a[N]; long long dpu[N]; long long dp[N]; long long n, k; vector<long long> u[N]; void dfs(long long v, long long p = -1) { dp[v] = dpu[v] = 0; for (auto P : a[v]) { long long u2 = P.first; long l...
### 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; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } const long long int INF = 1e10; vector<vector<pair<int, int> > > ad(50000...
### 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; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if< !is_same<T_container, s...
### 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; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int cg(long long int n) { return n ^ (n >> 1); } long long int SUM(long long int a) { return (a * (a + 1) / 2); } bool CAN(int x, int y, int n, int m) { if (x...
### 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; long long dp[500500][2]; vector<pair<int, int> > adj[500500]; void dfs(int u, int pa = 0) { vector<long long> vec; dp[u][0] = 0; for (auto p : adj[u]) { int v = p.first, w = p.second; if (v == pa) continue; dfs(v, u); vec.push_back(dp[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> using namespace std; const int N = 5e5 + 20; int q, n, k; long long dp[N][2]; vector<pair<int, int> > g[N]; void dfs(int v, int par = -1) { long long sum = 0; vector<long long> vec; for (pair<int, int> ed : g[v]) { int u = ed.first, w = ed.second; if (u == par) { continue; ...
### 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<pair<int, long long> > m[500050]; long long dp[500050], dp1[500050], a, b, c, d, e, licztes; void dfs(int poz, int oj) { for (int i = 0; i < m[poz].size(); i++) { if (m[poz][i].first != oj) { dfs(m[poz][i].first, poz); } } vector<long long> q; f...
### 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; vector<pair<long long, long long> > ed[500001]; int n, k; pair<long long, long long> dfs(int u, int p) { vector<pair<long long, long long> > var; long long tot = 0, cnt = 0; for (auto v : ed[u]) { if (v.first == p) continue; pair<long long, long long> temp = d...
### 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 = (long long)1e6 + 6, mod = (long long)0; long long k; long long dp[N][2]; vector<pair<long long, long long>> adj[N]; void dfs(long long v, long long p = -1) { dp[v][0] = dp[v][1] = 0; vector<pair<long long, pair<long long, long long>>> all; long lon...
### 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; struct ed { int to; int w; int prev; }; int n, k; vector<ed> e; int last[500500]; void addEdge(int from, int to, int w) { e.push_back({to, w, last[from]}); last[from] = e.size() - 1; return; } pair<long long, int> answer(int v, int prev) { long long ans = 0; ...
### 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 MAXN = 500100; int Q; int N, K; vector<pair<int, int> > edge[MAXN]; long long dpg[MAXN], dpn[MAXN]; void flood(int cloc, int last) { vector<long long> inc; long long tot = 0; long long pscore = 0; for (auto p : edge[cloc]) { int neigh = p.first; if...
### 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 + 5; int degree[N]; vector<int> v[N]; int n, k; long long dp[3][N]; long long val[N]; void dfs(int now, int pre) { for (int i = 0; i < v[now].size(); i += 2) { if (v[now][i] == pre) continue; dfs(v[now][i], now); } int size = v[now].size(); ...
### 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; ifstream in("boysgirls.in"); ofstream out("boysgirls.out"); struct Edge { int to, w; }; long long dp[511111][2]; int n, k; struct E { long long a, b, c; }; bool cmp(const E& a, const E& b) { if (a.a != b.a) return a.a > b.a; if (a.b != b.b) return a.b > b.b; retur...
### 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; vector<vector<pair<long long, long long>>> a; vector<pair<long long, long long>> ans; vector<multiset<long long>> st; long long k; long long get(long long u, long long v, long long w) { if (a[v].size() <= k) return ans[v].first + w; else return max(ans[v].first,...
### 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; long long n, k; vector<pair<long long, long long>> adj[505050]; map<pair<long long, long long>, long long> M; long long dfs(long long i, long long p, long long w) { if (M.count({i, w})) return M[{i, w}]; vector<pair<long long, long long>> 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; const int N = 5e5 + 10, M = 1e6 + 10; struct Edge { int v, Next; long long w; } G[M]; int head[N], tot, n, k; void add(int x, int y, long long z) { G[++tot].v = y; G[tot].w = z; G[tot].Next = head[x]; head[x] = tot; } bool cmp(int x, int y) { return x > y; } lon...
### 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<pair<long long, long long>>> graph; long long k; pair<long long, long long> dfs(long long v, long long prev) { vector<pair<long long, pair<long long, long long>>> childs; for (auto [u, w] : graph[v]) { if (u == prev) continue; auto [val1, val2] = d...
### 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; long long n, k; struct Node; vector<Node> tree; map<int, vector<int>> hs; struct Node { int id; int parent; vector<pair<int, long long>> adj; long long hcost = 0, lcost = 0; void treeify(int p, int h) { hs[-h].push_back(id); for (int i = adj.size() - 1; 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; const int N = 500000; int n, k; vector<pair<int, int>> g[N]; long long dp[N][2]; void dfs(int v, int p) { dp[v][0] = dp[v][1] = 0; vector<pair<int, int>> join; long long res = 0; for (const auto &pp : g[v]) { int to = pp.first; int cost = pp.second; if (...
### 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; using ll = long long; using pii = pair<int, int>; mt19937 gen(chrono::steady_clock::now().time_since_epoch().count()); template <typename... T> void rd(T&... args) { ((cin >> args), ...); } template <typename... T> void wr(T... args) { ((cout << args << " "), ...); co...
### 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> #pragma GCC optimize("Ofast") using namespace std; const int S = 1000 * 1000 + 5; int n, k; vector<pair<int, int>> g[S]; pair<long long int, long long int> dfs(int v, int p, int iw) { vector<long long int> cds; long long int res = 0; for (auto e : g[v]) { int to = e.first; int w =...
### 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 int64 = long long; constexpr int DEBUG = 0; struct WeightedEdge { int from, to; int64 weight; WeightedEdge(int from, int to, int64 weight) : from(from), to(to), weight(weight) {} }; vector<vector<WeightedEdge>> ReadUndirectedWeightedGraph( int n, int 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 N = 5e5 + 5; const int M = 1e5 + 5; long long dp1[N], dp2[N]; int n, k; vector<pair<int, long long> > v[N]; bool vis[N]; void dfs(int s) { bool ok = true; bool is; vis[s] = true; vector<long long> f; long long tot = 0; for (auto x : v[s]) { if (!vi...
### 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 MAX_N = 5e5 + 5; vector<pair<int, long long>> adj[MAX_N]; long long dpw[MAX_N]; long long dpwo[MAX_N]; void dfs(int vertex, int parent, long long parw, int deglim) { for (pair<int, long long> nxt : adj[vertex]) { if (nxt.first != parent) { dfs(nxt.firs...
### 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; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } long long g[1000100...
### 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; vector<vector<pair<long long, long long>>> g; vector<long long> dp[2]; long long n, k; void dfs(long long v, long long pr) { vector<long long> a; long long sm = 0; for (auto p : g[v]) { long long u = p.first; long long w = p.second; if (u == pr) continue; ...
### 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 double PI = 3.14159265358979323846; const double eps = (1e-5); int isGreater(long double x, long double y) { if (abs(x - y) < eps) return 0; if (x > y) return 1; return -1; } const int MAX_N = 5e5 + 5; vector<pair<int, int> > adj[MAX_N]; vector<vector<long long>...
### 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; int q, n, k; vector<pair<int, long long> > adj[500005]; pair<long long, long long> dfs(int a, int par, long long w) { vector<pair<long long, long long> > v; for (auto i : adj[a]) { if (i.first == par) continue; v.push_back(dfs(i.first, a, i.second)); } sort(...
### 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 <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...); } template ...
### 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 mod = 1000000007; const int inf = 1000000009; const long long INF = 1000000000000000009; const long long big = 1000000000000000; const long double eps = 0.0000000001; using namespace std; bool cmp(int a, int b) { return a > b; } int k; long long int DP[500005][3]; vector<pair<int, int...
### 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 = 500000; int n, k; vector<pair<int, int>> g[N]; long long dp[N][2]; void dfs(int v, int p) { dp[v][0] = dp[v][1] = 0; vector<pair<int, int>> join; long long res = 0; for (const auto &pp : g[v]) { int to = pp.first; int cost = pp.second; if (...
### 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; void solve() { long long n, k; cin >> n >> k; vector<vector<pair<long long, long long>>> g(n + 2); vector<vector<long long>> f(n + 2, vector<long long>(2)); for (long long i = 1; i < n; i++) { long long u, v, w; cin >> u >> v >> w; g[u].push_back({v, w...
### 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<pair<int, int>> graf[500005]; long long dp[500005][2]; int n, k; void dfs(int v, int parent) { long long sum = 0; vector<long long> vec; for (auto c : graf[v]) { if (c.first == parent) continue; dfs(c.first, v); vec.push_back(dp[c.first][1] + c.seco...
### 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 int kN = 500000 + 5; int n, k; std::vector<std::pair<int, int>> edges[kN]; using LL = long long; LL f[kN][2]; void dfs(int u, int fa, int wa) { std::vector<LL> vec; LL s = 0; for (auto t : edges[u]) { int v = t.first; int w = t.second; if (v != fa) { dfs(v, u, w); ...
### 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 + 10; const int inf = 0x3f3f3f3f; int kase, n, k; struct Edge { int to, nex; long long val; } edge[maxn << 1]; int head[maxn], ecnt; void add_edge(int u, int v, long long val) { edge[++ecnt] = {v, head[u], val}; head[u] = ecnt; } long long dp[ma...
### 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; using namespace std; long long dp[500005][2]; int head[500005], cnt, n, k, u, v, w; struct edge { int to, nxt, w; } e[1000005]; inline void add(int u, int v, int w) { e[++cnt].to = v; e[cnt].nxt = head[u]; e[cnt].w = w; head[u] = cnt; } void dfs(int x, int fa) { ...
### 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; long long INF = (long long)1e20; long long mod = (long long)1e9 + 7; pair<long long, long long> solve( long long v, vector<vector<pair<long long, long long>>> &nodes, long long p, long long k) { vector<pair<pair<long long, long long>, long long>> a; vector<pair<...
### 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; using ii = pair<int, int>; using vi = vector<int>; using vii = vector<ii>; using ll = long long; using vll = vector<ll>; int q, n, k; vii g[500005]; ll dp1[500005], dp2[500005]; void dfs(int u, int p) { ll sum = 0; vector<ll> vals; for (auto e : g[u]) { int v = e....
### 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 maxn = 6e5 + 10; const long long MAX = 1000; const long long inf = 0x3f3f3f3f; const long long mod = 1e9 + 7; long long n, m; vector<pair<long long, long long> > a[maxn]; long long f[maxn][2]; void dfs(long long now, long long fa) { f[now][0] = f[now][1] =...
### 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 ll = long long; const int N = 5 * (int)1e5; vector<pair<int, ll>> conns[N]; ll dp1[N]; ll dp2[N]; void recSolve(int i, int p, int k) { ll sum = 0; vector<ll> offs; for (auto ed : conns[i]) { int t = ed.first; if (t == p) continue; recSolve(t, i, k); ...
### 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, k; vector<pair<int, int> > G[MAXN]; vector<int> val[MAXN]; long long f[MAXN][2]; void dfs(int u, int fa) { f[u][1] = f[u][0] = 0; val[u].clear(); for (pair<int, int> e : G[u]) if (e.first != fa) { dfs(e.first, u); val[u...
### 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 MOD = 1e9 + 7; int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) { if (c == '-') f = -1; c = getchar(); } while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); return v * f; } long long readll() { ...
### 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; vector<long long> D0, D1; vector<vector<pair<long long, long long> > > G; bool comp(pair<long long, long long> a, pair<long long, long long> b) { return a.second - a.first > b.second - b.first; } void dfs(int x, int k, int p = -1) { vector<pair<long long, long long> > F...
### 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; using ll = long long; const int maxn = 5e5 + 5; int q, n, k; struct Edge { int to, w; }; vector<Edge> adj[maxn]; ll dp[maxn][2]; ll f(int i, int p, int m) { ll& res = dp[i][m]; if (res >= 0) return res; res = 0; for (Edge e : adj[i]) { if (e.to == p) continue;...
### 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 k; vector<pair<int, int> > g[N]; long long dp[N][2]; long long weight[N]; long long calc(int v) { return max(0LL, dp[v][0] + weight[v] - dp[v][1]); } bool cmp(int i, int j) { return calc(i) > calc(j); } void dfs(int v, int p) { dp[v][0] = dp[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; template <class T> inline void chkmax(T &x, const T &y) { if (x < y) x = y; } template <class T> inline void chkmin(T &x, const T &y) { if (y < x) x = y; } inline int read() { int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') flag...
### 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; vector<pair<int, int>> adj[500001]; int parent[500001]; int parent_weight[500001]; vector<pair<long long, long long>> child_result[500001]; int main() { int T; scanf("%d", &T); for (int tt = 1; tt <= T; ++tt) { int n, k; scanf("%d%d", &n, &k); for (int i =...
### 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; const int maxn = 500005; long long f[maxn][2], n, k; vector<pair<int, int> > g[maxn]; void adde(int u, int v, int w) { g[u].push_back({v, w}); } void dfs(int u, int fa) { vector<long long> d; d.clear(); f[u][0] = f[u][1] = 0; for (int i = 0; i < g[u].size(); i++) { ...
### 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 = 5 * (1e5 + 1); int N, n, k, mask[MAXN]; vector<pair<int, long long>> E[MAXN]; pair<long long, long long> dfs(int v) { mask[v] = 1; vector< pair<long long, pair<int, pair<long long, pair<long long, long long>>>>> q; for (auto item : E[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; int const Nmax = 5e5 + 5; int64_t DP0[Nmax], DP1[Nmax]; vector<pair<int, int> > Adj[Nmax]; int64_t val[Nmax]; int n, k; void DFS(int u, int par) { int64_t res = 0; vector<int64_t> val; for (int i = 0; i < (int)Adj[u].size(); ++i) { int v = Adj[u][i].first; int...
### 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 n, k; vector<pair<int, int>> a[500500]; long long f[500500][2]; void dfs(int x, int par) { f[x][0] = f[x][1] = 0; vector<long long> children; for (auto u : a[x]) { int y = u.first, w = u.second; if (y == par) continue; dfs(y, x); f[x][0] += max(f[y...
### 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 N = 1e6 + 7; const int MX = 1e9 + 7; const long long int INF = 1LL * MX * MX; int n, k; long long int dp[N][2]; vector<pair<int, int> > G[N]; void dfs(int u, int p) { long long int allSum = 0; vector<long long int> diffs; for (auto v : G[u]) if (v.first ...
### 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; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } using ll = long long; using ld = long double; const string FILENAME = "input"; const int M...
### 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 optimize("O3") using namespace std; const int N = 500031; int tests, n, k; vector<pair<int, int> > g[N]; int used[N]; long long dp[N][2]; void dfs(int v) { used[v] = 1; long long guaranteed = 0; vector<long long> benefits; for (int i = 0; i < g[v].size(); i++) { int to =...
### 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 long long N = 5e5 + 5; long long n, k; long long dp[N][2]; vector<pair<long long, long long> > g[N]; void dfs(long long u, long long par) { dp[u][0] = 0, dp[u][1] = 0; long long sum = 0; vector<long long> v; for (auto &it : g[u]) { if (it.first == par) con...
### 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; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } int TC; int N, K; vector<pair<int, int> > edge[500013]; long long dp[500013][2]; void dfs(int u, int p) { for (pair<int, int...
### 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 + 100; vector<pair<int, long long>> g[N]; vector<pair<int, long long>> t[N]; void build_tree(int curr, int prev) { for (auto conn : g[curr]) { if (conn.first != prev) { t[curr].emplace_back(conn); build_tree(conn.first, curr); } } }...
### 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 = (int)5e5 + 5; const int inf = (int)1e9 + 7; int q; int n, k; long long dp[N][2]; vector<pair<int, int> > g[N]; void dfs(int v, int p) { dp[v][0] = dp[v][1] = 0; vector<pair<long long, pair<int, int> > > vals; long long sumrest = 0; for (pair<int, int> ...
### 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; constexpr int N = 5e5 + 5; int n, k; vector<pair<int, int>> E[N]; LL memo[N][2]; LL dfs(int at, int f, int par) { if (memo[at][f] != -1) return memo[at][f]; int t = k - f; vector<LL> ans1, ans2; vector<int> weight; vector<int> ord; for (aut...
### 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<int> v[500009]; vector<int> l[500009]; int n, k; int f[500009]; long long dp[500009], dp1[500009]; void dfs(int x) { f[x] = 1; vector<long long> V; for (int i = 0; i < v[x].size(); i++) { int y = v[x][i], L = l[x][i]; if (f[y]) continue; dfs(y); ...
### 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 = (int)(5e5 + 1); int n, k; vector<pair<int, int> > g[N]; long long dp[N][2]; void dfs(int v, int p = -1) { vector<long long> dobav; long long ans1 = 0; for (int i = 0; i < g[v].size(); ++i) { int to = g[v][i].first; int w = g[v][i].second; if ...
### 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 inf = (long long)1e18; const long long mod = (long long)1e9 + 7; const double eps = (double)1e-9; const double pi = acos(-1.0); const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; const int N = 500500; int q, n, k; long long dp[N][2]; vecto...
### 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; const int maxn = 5e5 + 5; int q, n, k; struct Edge { int to, w; }; vector<Edge> adj[maxn]; ll dp[maxn][2]; ll f(int i, int p, int m) { if (dp[i][m] != -1) return dp[i][m]; dp[i][m] = 0; vector<ll> deltas; for (Edge e : adj[i]) { if (e.to ...
### 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; const int MAXN = 500010; int n, k; vector<pair<int, LL>> x[MAXN]; LL f[MAXN][2]; void DP(int a, int fa) { LL sum = 0; vector<LL> z; LL pv = 0; for (auto [b, c] : x[a]) { if (b == fa) { pv = c; continue; } DP(b, a); s...
### 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 inline read() { int num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } const int maxn = 500...
### 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 big = 2e9 + 5; const long long md = 1e9 + 7; const long long steps[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; void solve(); int main() { ios_base ::sync_with_stdio(0); cin.tie(); cout.tie(); long long q; cin >> q; for (; q > 0; q--) { solve(...
### 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; void dfs(int v, vector<bool> &used, vector<long long> &k0, vector<long long> &k1, vector<vector<pair<int, long long>>> &g) { used[v] = true; vector<pair<long long, int>> tmp; for (auto now : g[v]) { int u = now.first; long long w = now.secon...
### 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 mxN = 5e5; int n, k; vector<array<int, 2>> adj[mxN]; long long dp[mxN][2]; void dfs(int u = 0, int p = -1) { long long base = 0; vector<long long> a; for (array<int, 2> v : adj[u]) { if (v[0] ^ p) { dfs(v[0], u); dp[v[0]][1] += v[1]; ba...
### 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 double INF = 1e9 - 7; int k; vector<vector<pair<long long, long long>>> g; vector<vector<long long>> dp; void dfs(int u, int p = -1) { vector<pair<long long, long long>> temp_dp; long long res = 0; for (auto el : g[u]) { int v = el.first; int w = el...
### 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...