博客
关于我
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/

    你可能感兴趣的文章
    OpenStack项目管理实战
    查看>>
    OpenStreetMap初探(一)——了解OpenStreetMap
    查看>>
    openSUSE 13.1 Milestone 2 发布
    查看>>
    openSUSE推出独立 GUI 包管理工具:YQPkg,简化了整个软件包管理流程
    查看>>
    OpenVP共用账号 一个账号多台电脑登录
    查看>>
    OpenVSwtich(OVS)Vlan间路由实战 附实验环境
    查看>>
    Openwrt LuCI模块练习详细步骤
    查看>>
    openwrt_git_pull命令提示merger冲突时如何解决?
    查看>>
    OpenWrt包管理软件opkg的使用(极路由)
    查看>>
    OpenWrt固件编译刷机完全总结
    查看>>
    Open××× for Linux搭建之二
    查看>>
    Open×××有线网络时使用正常,无线网络时使用报错的解决方案
    查看>>
    Opera Mobile Classic Emulator
    查看>>
    Operation not supported on read-only collection 的解决方法 - [Windows Phone开发技巧系列1]
    查看>>
    OperationResult
    查看>>
    Operations Manager 2007 R2系列之仪表板(多)视图
    查看>>
    operator new and delete
    查看>>
    operator new 与 operator delete
    查看>>
    operator() error
    查看>>
    OPPO K3在哪里打开USB调试模式的完美方法
    查看>>