AcWing 285. 没有上司的舞会
标签:bool print pre string mem sizeof class bsp algo
//f[u][0]是所有以u为根的子树中选择,并且不选u这个点的方案 //f[u][1]是所有以u为根的子树中选择,并且 选u这个点的方案 #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 6010; int n; int h[N], e[N], ne[N], idx; int happy[N]; int f[N][2];//两种状态 bool has_fa[N]; void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ; } void dfs(int u) { f[u][1] = happy[u];//选择这个点 for (int i = h[u]; i!=-1; i = ne[i]) {//枚举所有儿子 int j = e[i]; dfs(j); f[u][1] = f[j][0]; f[u][0] = max(f[j][0], f[j][1]); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i ) scanf("%d", &happy[i]); memset(h, -1, sizeof h); for (int i = 0; i < n - 1; i ) { int a, b; scanf("%d%d", &a, &b); add(b, a); has_fa[a] = true;//表示有爹,不是根节点 } int root = 1; while (has_fa[root]) root ;//找根节点 dfs(root); printf("%dn", max(f[root][0], f[root][1])); return 0; }
AcWing 285. 没有上司的舞会
标签:bool print pre string mem sizeof class bsp algo
温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/14120.html
- 上一篇:C#中的协变和逆变
- 下一篇:webapi接口上传大文件