E - Demiurges Play Again

感觉这种类型的dp以前没遇到过。。。 不是很好想。。

dp[u] 表示的是以u为子树进行游戏得到的值是第几大的。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, sum[N], dp[N], f[N], dp2[N];
vector<int> edge[N]; void dfs(int u, int fa) {
for(int v : edge[u]) {
if(v == fa) continue;
f[u] = true;
dfs(v, u);
sum[u] += sum[v];
}
sum[u] += !f[u];
} void dfs2(int u, int fa, int depth) {
if(!f[u]) {
dp[u] = ;
dp2[u] = ;
return;
}
if(depth & ) {
dp2[u] = inf;
for(int v : edge[u]) {
if(v == fa) continue;
dfs2(v, u, depth + );
dp[u] += dp[v];
dp2[u] = min(dp2[u], dp2[v]);
}
} else {
dp[u] = inf;
for(int v : edge[u]) {
if(v == fa) continue;
dfs2(v, u, depth + );
dp[u] = min(dp[u], dp[v]);
dp2[u] += dp2[v];
}
}
} int main() {
scanf("%d", &n);
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dfs(, );
dfs2(, , );
printf("%d %d\n", sum[] - dp[] + , dp2[]);
return ;
} /*
5
1 5 4 3 2
*/

Codeforces Round #300 E - Demiurges Play Again的更多相关文章

  1. 贪心 Codeforces Round #300 A Cutting Banner

    题目传送门 /* 贪心水题:首先,最少的个数为n最大的一位数字mx,因为需要用1累加得到mx, 接下来mx次循环,若是0,输出0:若是1,输出1,s[j]--: 注意:之前的0的要忽略 */ #inc ...

  2. 水题 Codeforces Round #300 A Cutting Banner

    题目传送门 /* 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 判断是否余下的是 "CODEFORCES" :) */ #include <cs ...

  3. Codeforces Round #300 解题报告

    呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...

  4. Codeforces Round #300(A.【字符串,多方法】,B.【思维题】,C.【贪心,数学】)

    A. Cutting Banner time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. Codeforces Round #300(Div. 2)-538A.str.substr 538B.不会 538C.不会 。。。

    A. Cutting Banner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #300 A. Cutting Banner 水题

    A. Cutting Banner Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pro ...

  7. Codeforces Round #300 D. Weird Chess 水题

    D. Weird Chess Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/proble ...

  8. Codeforces Round #300 C. Tourist's Notes 水题

    C. Tourist's Notes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pr ...

  9. Codeforces Round #300 B. Quasi Binary 水题

    B. Quasi Binary Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/probl ...

随机推荐

  1. C++内存分配与释放

    C++内存分配与释放 1. new 运算符 与 operator new一条 new 表达式语句( new Type; )中的 new 是指 new 运算符.operator new 是定义在 #in ...

  2. 「Django」rest_framework学习系列-分页

    分页a.分页,看第N页,每页显示N条数据方式一:使用PageNumberPagination创建分页对象,配合settings全局配置 views设置 from rest_framework.pagi ...

  3. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  4. Python学习笔记(四十六)网络编程(2)— UDP编程

    摘抄:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320049779 ...

  5. 开发技巧:高效的使用 Response.Redirect

    我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortException. 这 ...

  6. 【bzoj】2326 [HNOI2011]数学作业

    [题意]给定n和m,求1~n从高位到低位连接%m的结果.n=11时,ans=1234567891011%m.n<=10^18,m<=10^9. [算法]递推+矩阵快速幂 [题解] 考虑枚举 ...

  7. 【CodeForces】704 B. Ant Man

    [题目]B. Ant Man [题意]给定n个人的xi,ai,bi,ci,di,起点为s,终点为e,移动: In simpler words, jumping from i-th chair to j ...

  8. iOS 程序启动流程

    iOS程序启动原理   技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong   iOS应用程序运行 ...

  9. 用jquery实现小火箭到页面顶部的效果

    恩,不知道之前在哪看过一个页面效果就是如果页面被滑动了就出现一个小火箭,点击这个小火箭就可以慢慢回到页面顶部,闲的没事,自己搞了一下 需要引入jquery 代码和布局都很简单 <!DOCTYPE ...

  10. 【leetcode 简单】第六题 有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...