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

    你可能感兴趣的文章
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OS2ATC2017:阿里研究员林昊畅谈操作系统创新与挑战
    查看>>