zoukankan      html  css  js  c++  java
  • [luoguP2016] 战略游戏(DP)

    传送门

    f[i][0]表示不选当前节点,当前节点的所有儿子节点都选
    f[i][1]表示选当前节点,儿子节点可选可不选

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 1501
    #define min(x, y) ((x) < (y) ? (x) : (y))
    
    int n, cnt;
    int head[N], to[N << 1], next[N << 1], f[N][2];
    bool vis[N];
    
    //f[i][0]表示不选当前节点,当前节点的所有儿子节点都选
    //f[i][1]表示选当前节点,儿子节点可选可不选 
    
    inline int read()
    {
    	int x = 0, f = 1;
    	char ch = getchar();
    	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    	return x * f;
    }
    
    inline void add(int x, int y)
    {
    	to[cnt] = y;
    	next[cnt] = head[x];
    	head[x] = cnt++;
    }
    
    inline void dfs(int u)
    {
    	int i, v;
    	f[u][1] = vis[u] = 1;
    	for(i = head[u]; i ^ -1; i = next[i])
    	{
    		v = to[i];
    		if(!vis[v])
    		{
    			dfs(v);
    			f[u][0] += f[v][1];
    			f[u][1] += min(f[v][0], f[v][1]);
    		}
    	}
    }
    
    int main()
    {
    	int i, j, k, x, y;
    	n = read();
    	memset(head, -1, sizeof(head));
    	for(i = 1; i <= n; i++)
    	{
    		x = read() + 1;
    		k = read();
    		for(j = 1; j <= k; j++)
    		{
    			y = read() + 1;
    			add(x, y);
    			add(y, x);
    		}
    	}
    	dfs(1);
    	printf("%d
    ", min(f[1][0], f[1][1]));
    	return 0;
    }
    

      

  • 相关阅读:
    JSON.parse()和JSON.stringify()
    php结合layui实现前台加后台操作
    微信拨打电话功能
    视觉差效果
    前端开发面试题
    字符串分割--java中String.split()用法
    vue.js实现购物车功能
    localStorage使用总结
    canvas 实现赛车小游戏
    canvas 实现飞碟射击游戏
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7351346.html
Copyright © 2011-2022 走看看