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

    你可能感兴趣的文章
    Nodejs简介以及Windows上安装Nodejs
    查看>>
    nodejs配置express服务器,运行自动打开浏览器
    查看>>
    Node入门之创建第一个HelloNode
    查看>>
    Node出错导致运行崩溃的解决方案
    查看>>
    node安装及配置之windows版
    查看>>
    Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
    查看>>
    NOIp2005 过河
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>