Codeforces Round #300 E - Demiurges Play Again

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 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

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] = 1;
        dp2[u] = 1;
        return;
    }
    if(depth & 1) {
        dp2[u] = inf;
        for(int v : edge[u]) {
            if(v == fa) continue;
            dfs2(v, u, depth + 1);
            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 + 1);
            dp[u] = min(dp[u], dp[v]);
            dp2[u] += dp2[v];
        }
    }
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1, 0);
    dfs2(1, 0, 0);
    printf("%d %d\n", sum[1] - dp[1] + 1, dp2[1]);
    return 0;
}

/*
5
1 5 4 3 2
*/

原文地址:https://www.cnblogs.com/CJLHY/p/9671050.html

时间: 2024-10-08 04:34:26

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

Codeforces Round #300 (A,B,C,D)

题目传送:Codeforces Round #300 A. Cutting Banner 思路:一看题就会错意了,然后一顿猛敲,果不其然的被hack了,然后才发现只需要剪中间那一段就可以了,然后又傻逼得少写一个等号,还是被hack了,心累啊 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #in

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

贪心 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 贪心水题:首先,最少的个数为n最大的一位数字mx,因为需要用1累加得到mx, 3 接下来mx次循环,若是0,输出0:若是1,输出1,s[j]--: 4 注意:之前的0的要忽略 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 #include

水题 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 3 判断是否余下的是 "CODEFORCES" :) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath>

Codeforces Round #300

A. Cutting Banner 关键字:[读题][字符串][枚举] 坑点:能不能只减去某一段字符串把剩下的按原来的顺序连起来组成"CODEFORCES",而减下一段"CODEFORCES"是不行的 e.g. "CODEFAAAAORCES"是可行的         而 "AACODEFORCESAA"是不可行的 B. Quasi Binary 题意:给出一个数n,求最少用几个形如"1010"这样只包含'

Codeforces Round #300 B

B. Quasi Binary A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - are quasibinary and numbers 2, 12, 900 are not. You are given a positive integer n. Represent it as a s

Codeforces Round #300 - Quasi Binary(贪心)

Quasi Binary Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - ar

Codeforces Round #257 (Div. 2)

A.Jzzhu and Children 计算每个人会出现的轮次数,取最后一个且轮次数最大的,注意是用a[i]-1 % m,倒着扫一遍就行了 #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int maxn = 100+10; int n, m; int a[maxn]; int main() { #ifdef LOCAL freopen("

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/