博客
关于我
P2016 战略游戏(树形dp)
阅读量:414 次
发布时间:2019-03-05

本文共 2248 字,大约阅读时间需要 7 分钟。

????????????????????????????????????????????????????????????????????

????

?????????????????????????

  • ???????????????????????
  • ???????????????????DFS?????????????????????????
  • ?????????
    • f[u][0] ???? u ??????????????????
    • f[u][1] ???? u ?????????????????
  • ??????????????????????????????????????????????????????????????????????????????????
  • ???????????????????????
  • ????

    #include 
    #include
    using namespace std;int n;int adj[20000];int parent[20000];int children[20000][20000];int f[20000][2];void add_edge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u);}void dfs(int u, int p) { parent[u] = p; for (int v : adj[u]) { if (v != p) { children[u].push_back(v); dfs(v, u); } }}int main() { cin >> n; adj.resize(n); for (int i = 0; i < n; ++i) { adj[i].clear(); } for (int i = 0; i < n; ++i) { int k, x1; cin >> x1 >> k; for (int j = 0; j < k; ++j) { int y1; cin >> y1; add_edge(x1, y1); } } parent[0] = -1; children[0].clear(); dfs(0, -1); for (int u = 0; u < n; ++u) { f[u][0] = 0; f[u][1] = 0; } stack
    stack; stack.push(0); bool visited[20000]; visited[0] = true; while (!stack.empty()) { int u = stack.top(); stack.pop(); vector
    child_indices; for (size_t i = 0; i < children[u].size(); ++i) { if (!visited[children[u][i]]) { child_indices.push_back(i); } } for (size_t i = 0; i < child_indices.size(); ++i) { int v = children[u][child_indices[i]]; if (!visited[v]) { visited[v] = true; stack.push(v); } } for (size_t i = 0; i < children[u].size(); ++i) { int v = children[u][i]; if (!visited[v]) { f[u][0] = f[v][1]; f[u][1] += f[v][0]; } } } int result = min(f[0][0], f[0][1]); cout << result << endl;}

    ????

  • ??????????????????????????
  • DFS????????????????????
  • ?????????????????????
  • ????????????????????????????????????????
  • ????????????????
  • ??????????????????????????????????

    转载地址:http://egpzz.baihongyu.com/

    你可能感兴趣的文章
    python | urllib3,一个超强的 Python 库!
    查看>>
    python | webassets,一个超强的 Python 库!
    查看>>
    python | werkzeug,一个不可思议的 Python 库!
    查看>>
    python | xlsxwriter,一个实用的 Python 库!
    查看>>
    python | xlwings,一个非常实用的 Excel 相关的 Python 库!
    查看>>
    python | xmltodict,一个非常厉害的 关于XML数据 Python 库!
    查看>>
    python | xonsh,一个超酷的 Python 库!
    查看>>
    python | yagmail,一个实用的 Python 库!
    查看>>
    python | 一文掌握Python的上下文管理器和with语句
    查看>>
    python | 一文看懂Python闭包机制与变量作用域规则
    查看>>
    python读取含中文的json
    查看>>
    python | 如何用Python锁避免并发错误?
    查看>>
    python | 提升代码迭代速度的Python重载方法
    查看>>
    python | 深入理解Python并发编程中的GIL限制与解决方案
    查看>>
    Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
    查看>>
    python | 高效使用Python工具自动生成模块文档的秘诀
    查看>>
    python 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>