旅游

旅游

https://ac.nowcoder.com/acm/problem/15748

入门树形DP

做法

为第个点住宿/不住宿的最大时间,随便转移一下就行了。

CODE

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define I inline
#define ri register int
#define lowbit(x) x & -x
#define For(i , x , y) for(ri i = x ; i <= y ; ++ i)
#define Next(i , x) for(ri i = head[x] ; i ; i = e[i].nxt)
I int read() {
    int s = 0 , w = 1; char ch = getchar();
    while(ch < 48 || ch > 57) {if(ch == '-') w = -1; ch = getchar();}
    while(ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar();
    return s * w;
}
const int N = 1e6 + 5;

int n , s , head[N] , tot , f[N][2];

struct Edge{
    int to , nxt; 
} e[N << 1];

I void addedge(int x , int y) {
    e[++ tot] = (Edge) {y , head[x]} , head[x] = tot;
}

void dfs(int u , int par) {
    f[u][0] = 0 ; f[u][1] = 1;
    for(int i = head[u] ; i ; i = e[i].nxt) {
        int v = e[i].to;
        if(v == par) continue;
        dfs(v , u);
        f[u][0] += max(f[v][1] , f[v][0]);
        f[u][1] += f[v][0];
    }
}
signed main() {
    n = read() , s = read();
    For(i , 1 , n - 1) {
        int u = read() , v = read() ; 
        addedge(u , v) ; addedge(v , u);
    }
    dfs(s , s);
    cout << f[s][1] << endl;
    return 0;
}




全部评论

相关推荐

渐好:软光栅真的写明白了吗,既然是软渲那技术栈不应该使用OpenGL,光追和bvh既不算什么高级渲染技术更不应该属于软渲的内容,git那个项目没啥用,建议把前两个项目重新组织一下语言,比如软渲染那个项目 冯着色和msaa、贴图这几项分开写,写的到位点,如果你还学过光追那就单独写出来,如果没把握考官问你答不上来就别写给自己找麻烦,在技术栈那一栏简单提一下自己学过就行,这样杂的放在一起不太严谨,个人愚见.
点赞 评论 收藏
分享
一天代码十万三:这都不能算简历吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务