树上染色(树形dp)

简介: 树上染色(树形dp)


/*********************************************************************
    程序名:
    版权: Joecai
    作者: Joecai
    日期: 2022-03-27 17:35
    说明:
*********************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
# define rep(i,be,en) for(int i=be;i<=en;i++)
# define pre(i,be,en) for(int i=be;i>=en;i--)
typedef pair<int, int> PII;
#define ll long long
#define endl "\n"
#define LOCAL
#define pb push_back
const int N = 2500 + 10, INF = 0x3f3f3f3f;
ll qpow(int a, int b) {
  ll ans = 1;
  while (b) {
    if (b & 1) ans = ans * a;
    a = (ll)a * a;
    b >>= 1;
  }
  return ans;
}
inline ll read()
{
  ll X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}
inline void write(ll X)
{
  if (X < 0) {
    X = ~(X - 1);
    putchar('-');
  }
  if (X > 9) write(X / 10);
  putchar(X % 10 + '0');
}
struct dsu {
  private:
    vector<int> par, ran, cnt;
  public:
    dsu(int n) {
      par.resize(n, 0);
      iota(par.begin(), par.end(), 0);
      ran.resize(n, 0);
      cnt.resize(n, 1);
    }
    int find(int x) {
      if (par[x] == x)return x;
      else return par[x] = find(par[x]);
    }
    bool unite(int x, int y) {
      x = find(x), y = find(y);
      if (x == y) return false;
      if (ran[x] < ran[y]) {
        par[x] = y;
        cnt[y] += cnt[x];
      }
      else {
        par[y] = x;
        cnt[x] += cnt[y];
        if (ran[x] == ran[y])ran[x]++;
      }
      return true;
    }
    bool same(int x, int y) {
      return find(x) == find(y);
    }
    int getcnt(int x) {
      return cnt[find(x)];
    }
};
struct node
{
  int to;
  int w;
};
vector<node>g[N];
int n, k;
ll f[N][N];// i 点i子树选j个点的贡献
int dfs(int u, int fa)
{
  int cnt = 1;
  f[u][0] = f[u][1] = 0;
  for (auto x : g[u])
  {
    int w = x.w;
    int j = x.to;
    if (j == fa) continue;
    int son = dfs(j, u);
    cnt += son;
    for (int i = min(k, cnt); i >= 0; i--)
      for (int r = 0; r <= min(i, son); r++)
      {
        //取r个
        if (f[u][i - r] != -1)
        {
          ll tot = (ll)r * (k - r) + (ll)(son - r) * (n - son - k + r);
          f[u][i] = max(f[u][i], f[j][r] + f[u][i - r] + 1ll * w * tot);
        }
      }
  }
  return cnt;
}
void solve()
{
  n = read();
  k = read();
  if(n-k<k)k=n-k;
  memset(f, -1, sizeof(f));
  for (int i = 1; i <= n - 1; i++)
  {
    int a, b, c;
    a = read();
    b = read();
    c = read();
    g[a].pb({b, c});
    g[b].pb({a, c});
  }
  dfs(1, -1);
  write(f[1][k]);
}
int main() {
  //#ifdef LOCAL
  //freopen("data.in.txt","r",stdin);
  //freopen("data.out.txt","w",stdout);
  //#endif
  int __ = 1;
  //cin>>__;
  while (__--)
  {
    solve();
  }
  return 0;
}

时间复杂度卡的死,不开O2优化100分,开O2accpeted.

目录
相关文章
|
11天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23461 10
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
15天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
5008 17
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
16天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
6011 14
|
5天前
|
人工智能 缓存 Shell
Claude Code 全攻略:命令大全 + 实战工作流(完整版)
Claude Code 是一款运行在终端环境下的 AI 编码助手,能够直接在项目目录中理解代码结构、编辑文件、执行命令、执行开发计划,并支持持久化记忆、上下文压缩、后台任务、多模型切换等专业能力。对于日常开发、项目维护、快速重构、代码审查等场景,它可以大幅减少手动操作、提升编码效率。本文从常用命令、界面模式、核心指令、记忆机制、图片处理、进阶工作流等维度完整说明,帮助开发者快速上手并稳定使用。
1028 2
|
4天前
|
前端开发 API 内存技术
对比claude code等编程cli工具与deepseek v4的适配情况
DeepSeek V4发布后,多家编程工具因未适配其强制要求的`reasoning_content`字段而报错。本文对比Claude Code、GitHub Copilot、Langcli、OpenCode及DeepSeek-TUI等主流工具的兼容性:Claude Code需按官方方式配置;Langcli表现最佳,开箱即用且无报错;Copilot与OpenCode暂未修复问题;DeepSeek-TUI尚处早期阶段。
878 2
对比claude code等编程cli工具与deepseek v4的适配情况
|
1月前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
25430 65
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)