博客
关于我
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读excel
    查看>>
    Python 中读取 CSV 文件-ChatGPT4o作答
    查看>>
    Python 之 filecmp
    查看>>
    python请求html_使用Python请求获取HTML?
    查看>>
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>
    python语音播放
    查看>>
    python语言:装饰器原理
    查看>>
    Python 交互式数据可视化详解
    查看>>
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>
    Python 使用pandas 进行查询和统计详解
    查看>>