题面

题解

树形\(dp\)(最大独立集)

设\(f_{i,0/1}\)表示\(dp\)到第\(i\)个点,在这个点放了(没放)士兵的最小花费

直接转移即可。

代码

#include<cstdio>
#include<cstring>
#include<vector>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x)); namespace IO
{
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char getchar() { if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return *is++; }
} inline int read()
{
int data = 0, w = 1;
char ch = IO::getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = IO::getchar();
if(ch == '-') w = -1, ch = IO::getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::getchar();
return data*w;
} const int maxn(2000);
int f[maxn][2], n; std::vector<int> G[maxn];
typedef std::vector<int>::iterator iter; void dfs(int x)
{
f[x][1] = 1; f[x][0] = 0;
for(RG iter it = G[x].begin(); it != G[x].end(); ++it)
dfs(*it), f[x][1] += std::min(f[*it][1], f[*it][0]), f[x][0] += f[*it][1];
} int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read();
for(RG int i = 1, x, num; i <= n; i++)
{
x = read() + 1; num = read();
for(RG int i = 1; i <= num; i++) G[x].push_back(read() + 1);
}
dfs(1); printf("%d\n", std::min(f[1][0], f[1][1]));
return 0;
}

最新文章

  1. android系统掉电保护
  2. codeforces 630J Divisibility
  3. JS判断是否是移动设备进行http链接重定向
  4. CCF NOIP2015复赛获奖分数线及名额分配办法
  5. 【linux系统学习】计算机硬件核心知识
  6. Html中插入javascript不识别问题
  7. Java基础入门知识
  8. 死磕 java集合之ConcurrentSkipListMap源码分析——发现个bug
  9. 分布式、服务化的ERP系统架构设计
  10. mybatis-ehcache整合中出现的异常 ibatis处理器异常(executor.ExecutorException)解决方法
  11. Eclipse手动添加web.xml
  12. [2017BUAA软工助教]案例分析小结
  13. Cycle (KMP + hash)
  14. 图解 Java 内存模型
  15. 设计一个带有getmin功能的栈,保证时间复杂度在O(1)
  16. Ajax发送POST请求对数据的封装
  17. Creating popup windows in XBAP applications
  18. C语言初学
  19. Unity shader saturate
  20. pthon爬虫(9)--Selenium的用法

热门文章

  1. python3 安装win32api
  2. yii2数据库简单操作
  3. linux 常用进程使用命令
  4. Hdu 5052 Yaoge’s maximum profit(树链剖分)
  5. DBCP数据库连接池的简单使用
  6. 怎么在一台电脑上安装win7与centos7双系统
  7. 关于osi的7层与tcp的4层网络协议的理解
  8. jQuery+zTree
  9. 百度地图热力图--批量地址转换应用(基于百度api)
  10. CDH升级 5.7.5 --&gt; 5.13.3(tar包方式)