PDD3.30笔试第二题,树形DP

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin>>n;
    int u,v,w;
    vector<vector<pair<int,int>>> g(n+1, vector<pair<int,int>>());
    for(int i = 0; i < n-1; ++i)
    {
        cin>>u>>v>>w;
        g[u].emplace_back(v,w);
        g[v].emplace_back(u,w);
    }
    g[0].emplace_back(1,0);
    vector<int> dp(n+1, 0);
    function<void(int,int)> helper = [&](int father, int son){
        for(auto it = g[son].begin(); it != g[son].end(); ++it)
        {
            int node = it->first, v = it->second;
            if(node == father)continue;
            helper(son, node);
            if(dp[node] > 0)dp[son] += dp[node];
            else if(v == 1)dp[son] += 1;
        }
    };
    helper(0,1);
    cout<<dp[1]<<endl;
    return 0;
}
#拼多多笔试#
全部评论

相关推荐

11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务