当前位置:网站首页>B-杰哥的树(状压树形dp)
B-杰哥的树(状压树形dp)
2022-07-06 12:18:00 【朴小明】
链接:杰哥的树
题意:
给定一棵树,边权 是
这六个字符,求不同的路径数,要求路径上的字符都恰好出现偶数次。
做法:
只关心奇偶,那么一个字符就可有两个状态——1代表奇数个,0代表偶数个,要想同时表示六个字符是奇数还是偶数,就可用0~63来表示。
定义
为以第
个节点为起点,状态为
有多少路径数量,如果
的儿子是
,当前字符是
,那么
,除了由儿子的状态转移而来,还会有新的状态,不继承儿子的状态,
到
的这条边也可单独作为一个状态,则当
时,
。
还要考虑点
作为中间节点的情况,这个简单,由
计算即可。
代码:
struct tree
{
int y, val;
};
vector<tree> v[N];
int f[N][1ll << 6];
int ans;
void dfs(int x, int fa)
{
f[x][0] = 0;
for (auto tt : v[x])
{
int y = tt.y, val = tt.val;
if (y == fa) continue;
dfs(y, x);
for (int i = 0; i < (1ll << 6); i++)
{
f[x][i] += f[y][i ^ (1ll << val)];
if (i == (1ll << val)) f[x][i]++; //不能乱加1,之所以加1,是因为定义x这个点只出一条边
}
}
ans += f[x][0];
int sum = 0;
for (auto tt : v[x])
{
int y = tt.y, val = tt.val;
if (y == fa) continue;
for (int i = 0; i < (1ll << 6); i++)
{
int x1 = f[x][i] - f[y][i ^ (1ll << val)] - (i == (1ll << val));
int x2 = f[y][i ^ (1ll << val)] + (i == (1ll << val));
sum += x1 * x2;
}
}
ans += sum / 2;
}
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++)
{
int x, y;
char c;
scanf("%lld%lld", &x, &y);
cin >> c;
v[x].emplace_back(tree{y, c - 'a'});
v[y].emplace_back(tree{x, c - 'a'});
}
dfs(1, 0);
cout << ans << "\n";
}边栏推荐
- Leetcode brush first_ Maximum Subarray
- HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
- 语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- Cesium Click to draw a circle (dynamically draw a circle)
- 8086指令码汇总表(表格)
- Problems encountered in using RT thread component fish
- [play with Linux] [docker] MySQL installation and configuration
- Example of applying fonts to flutter
- JVM_ Common [interview questions]
猜你喜欢

It's enough to read this article to analyze the principle in depth

爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解

Hudi vs Delta vs Iceberg

A5000 vgpu display mode switching

New generation garbage collector ZGC

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother

【计网】第三章 数据链路层(3)信道划分介质访问控制
Classic 100 questions of algorithm interview, the latest career planning of Android programmers

持续测试(CT)实战经验分享

Li Kou 101: symmetric binary tree
随机推荐
新一代垃圾回收器—ZGC
【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
【计网】第三章 数据链路层(3)信道划分介质访问控制
[network planning] Chapter 3 data link layer (3) channel division medium access control
持续测试(CT)实战经验分享
Enumeration gets values based on parameters
激进技术派 vs 项目保守派的微服务架构之争
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
How to handle the timeout of golang
学习打卡web
New generation garbage collector ZGC
Leetcode brush first_ Maximum Subarray
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
腾讯架构师首发,2022Android面试笔试总结
转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
A5000 vGPU显示模式切换
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
PowerPivot - DAX (first time)
Web开发小妙招:巧用ThreadLocal规避层层传值