当前位置:网站首页>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";
}
边栏推荐
- [cloud native and 5g] micro services support 5g core network
- [calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
- Cesium Click to draw a circle (dynamically draw a circle)
- 22-07-05 七牛云存储图片、用户头像上传
- PHP与EXCEL PHPExcel
- Color is converted to tristimulus value (r/g/b) (dry stock)
- Leetcode 30. Concatenate substrings of all words
- Appx代码签名指南
- 5. 无线体内纳米网:十大“可行吗?”问题
- 数字三角形模型 AcWing 1018. 最低通行费
猜你喜欢
激进技术派 vs 项目保守派的微服务架构之争
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
【云原生与5G】微服务加持5G核心网
[network planning] Chapter 3 data link layer (3) channel division medium access control
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
Cesium Click to draw a circle (dynamically draw a circle)
Introduction to enterprise lean management system
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
系统与应用监控的思路和方法
随机推荐
JVM_常见【面试题】
AsyncHandler
广州首个数据安全峰会将在白云区开幕
永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
枚举根据参数获取值
新一代垃圾回收器—ZGC
《数字经济全景白皮书》保险数字化篇 重磅发布
[play with Linux] [docker] MySQL installation and configuration
5. 無線體內納米網:十大“可行嗎?”問題
PHP与EXCEL PHPExcel
Example of shutter text component
Tencent Android development interview, basic knowledge of Android Development
Wechat applet common collection
Cesium 点击绘制圆形(动态绘制圆形)
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
Logstash expressway entrance
Poj1149 pigs [maximum flow]