【CodeForces - 1156D】0-1-Tree(树形dp)

博客围绕计算树中有效顶点对数量展开。给定带0、1标记边的树,有效顶点对要求路径中不出现先1边后0边情况。采用树形DP求解,通过设4个状态(全白、全黑、上白下黑、上黑下白)来计算,还提及实现时易犯的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given a tree (an undirected connected acyclic graph) consisting of n vertices and n−1 edges. A number is written on each edge, each number is either 0 (let’s call such edges 0-edges) or 1 (those are 1-edges).

Let’s call an ordered pair of vertices (x,y) (x≠y) valid if, while traversing the simple path from x to y, we never go through a 0-edge after going through a 1-edge. Your task is to calculate the number of valid pairs in the tree.

Input
The first line contains one integer n (2≤n≤200000) — the number of vertices in the tree.

Then n−1 lines follow, each denoting an edge of the tree. Each edge is represented by three integers xi, yi and ci (1≤xi,yi≤n, 0≤ci≤1, xi≠yi) — the vertices connected by this edge and the number written on it, respectively.

It is guaranteed that the given edges form a tree.

Output
Print one integer — the number of valid pairs of vertices.

Example
Input
7
2 1 1
3 2 0
4 2 1
5 2 0
6 7 1
7 2 1
Output
34
思路:
显然树形dp,一开始我以为只是求链都是黑色的路径的数量,发现这个怎么这么简单,发现读错了题,题目实际上求得是是黑白两条拼接的路径数,就是一侧都是白色,另一侧都是黑色,也可以全黑或者全白。
然后我设两个状态,就是全黑和全白,发现貌似还少了一些case,然后我就又设了一个上头是黑色,下面至少有一头白色。然后这个我在实际去写的时候,当在白边转移时,自然而想到少了另一个状态,就是上面是白色,下面至少有一头黑色。

回过头来看,其实发现,白色和黑色的地位是等价的,根据对称性,就应该能设计出偶数个状态,所有最终是4个状态,在转移时,遇到黑色和遇到白色,的转移方程也是对称的。

然后智商下线了,忘记把全黑和全白的情况乘二了。。。怎么算都还是少了。。。自己手动枚举样例发现和我做也是一样。。。对自己无语了。。。注意是ordered pair

状态:
f[u][0]f[u][0]f[u][0]为以u为子树的时候,u为端点,边全是白色的pair个数
f[u][1]f[u][1]f[u][1]为以u为子树的时候,u为端点,边全是黑色的pair个数
f[u][2]f[u][2]f[u][2]为以u为子树的时候,u为端点,靠近u一头边为白色,终点是白色的pair个数
f[u][3]f[u][3]f[u][3]为以u为子树的时候,u为端点,靠近u一头边为黑色,终点是黑色的pair个数

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<cmath>
#include<vector>
#include<set>
#define INF 0x7f7f7f7f
#define maxx 200005
using namespace std;
typedef long long ll;
int head[maxx],to[maxx<<1],_next[maxx<<1],w[maxx<<1];
int edge;
void addEdge(int x,int y,int c)
{
    to[++edge]=y,w[edge]=c,_next[edge]=head[x],head[x]=edge;
    to[++edge]=x,w[edge]=c,_next[edge]=head[y],head[y]=edge;
}
int f[maxx][4];
int n;
ll ans=0;
void dfs(int u,int fa)
{
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)continue;
        dfs(v,u);
        if(w[i])
        {
            ans+=(ll)2*f[u][1]*(f[v][1]+1);
            ans+=(ll)(f[u][0]+f[u][3])*(f[v][1]+1);
            ans+=(ll)f[u][1]*(f[v][0]+f[v][3]);

            f[u][1]+=f[v][1]+1;
            f[u][3]+=f[v][3];
            f[u][3]+=f[v][0];
        }
        else
        {
            ans+=(ll)2*f[u][0]*(f[v][0]+1);
            ans+=(ll)(f[u][1]+f[u][2])*(f[v][0]+1);
            ans+=(ll)f[u][0]*(f[v][1]+f[v][2]);

            f[u][0]+=f[v][0]+1;
            f[u][2]+=f[v][2];
            f[u][2]+=f[v][1];

        }
    }
    ans+=2*f[u][0];
    ans+=2*f[u][1];
    ans+=f[u][2];
    ans+=f[u][3];
}
int main()
{
    cin>>n;
    int x,y,c;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%d",&x,&y,&c);
        addEdge(x,y,c);
    }
    dfs(1,0);
    cout<<ans<<endl;
    return 0;
}

### 关于C++中的树形动态规划(Tree DP树形动态规划是一种基于图论的算法设计技术,通常用于解决定义在树结构上的优化问题。这类问题的特点是输入数据可以表示为一棵无环连通图(即树),并且可以通过自底向上的方式计算最优解。 #### 树形DP的核心概念 树形DP的关键在于状态的设计以及如何通过子节点的状态来更新父节点的状态。常见的做法是从叶子节点开始逐步向上回溯,直到根节点完成整个树的状态转移过程[^1]。 以下是实现树形DP的一个通用模板: ```cpp #include <bits/stdc++.h> using namespace std; // 定义最大节点数 const int MAXN = 100005; vector<int> tree[MAXN]; // 邻接表存储树结构 long long dp[MAXN][2]; // 动态规划数组 void dfs(int u, int parent) { // 初始化当前节点的状态 dp[u][0] = 0; // 不选当前节点的情况 dp[u][1] = some_value(u); // 选当前节点的情况 for (auto &v : tree[u]) { if (v != parent) { // 跳过父节点 dfs(v, u); // 更新dp[u][0], 假设不选u,则可以从子节点任意选择 dp[u][0] += max(dp[v][0], dp[v][1]); // 更新dp[u][1], 假设选择了u,则不能选择其直接子节点 dp[u][1] += dp[v][0]; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; // 构建树 for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; tree[a].push_back(b); tree[b].push_back(a); } // 开始DFS遍历 dfs(1, -1); cout << max(dp[1][0], dp[1][1]); // 输出最终结果 } ``` 上述代码展示了一个典型的树形DP实现流程。`dfs`函数负责递归访问每一个节点并填充动态规划表格 `dp` 的值。具体来说: - `dp[u][0]`: 表示不选取节点 `u` 时的最大收益。 - `dp[u][1]`: 表示选取节点 `u` 时的最大收益。 此模板可以根据实际问题调整状态定义和转移方程。 #### 学习资源推荐 对于希望深入学习 Tree DP 的开发者而言,以下是一些可能有帮助的学习材料或教程链接[^2]: - **LeetCode**: 提供大量有关树形DP的实际编程题目,例如“House Robber III”等问题可以直接练习该技巧。 - **Codeforces Blog Entries**: Codeforces社区中有许多高质量博客文章专门讨论各种类型的动态规划及其应用案例。 - **GeeksforGeeks Articles on Dynamic Programming over Trees**: 这里包含了详细的理论解释加上多个实例分析。 #### 注意事项 当处理大规模数据集时需注意效率问题;此外还需考虑边界条件如孤立点或者仅有单条边连接的小型测试样例是否会引发错误逻辑判断等情况发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值