当前位置:网站首页>B-jiege's tree (pressed tree DP)
B-jiege's tree (pressed tree DP)
2022-07-06 20:15:00 【Park Xiaoming】
link : Jack's tree
The question :
Given a tree , Border right yes
These six characters , Find different path numbers , The characters on the path are required to appear exactly an even number of times .
practice :
Only care about parity , Then a character can have two states ——1 Represents an odd number of ,0 Represents an even number of , To indicate whether six characters are odd or even at the same time , You can use 0~63 To express .
Definition
In order to
A node is the starting point , Status as
How many paths are there , If
My son is
, The current character is
, that
, Except for the transfer from the son's state , There will be a new state , The state of not inheriting a son ,
To
This side of can also be used as a state alone , Then when
when ,
.
There are also some considerations
As an intermediate node , This simple , from
Calculation is enough .
Code :
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]++; // Don't add 1, The reason to add 1, Because of the definition x This point has only one side
}
}
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";
}边栏推荐
- Tencent Android development interview, basic knowledge of Android Development
- 【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
- 青龙面板白屏一键修复
- 精彩编码 【进制转换】
- 腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
- Digital triangle model acwing 1018 Minimum toll
- Tencent architects first, 2022 Android interview written examination summary
- RT-Thread 组件 FinSH 使用时遇到的问题
- mod_ WSGI + pymssql path SQL server seat
- 小微企业难做账?智能代账小工具快用起来
猜你喜欢
Tencent T4 architect, Android interview Foundation
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]

棋盘左上角到右下角方案数(2)

Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022

5. 无线体内纳米网:十大“可行吗?”问题

【云原生与5G】微服务加持5G核心网

redisson bug分析

5. Nano - Net in wireless body: Top 10 "is it possible?" Questions

(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
Tencent T2 Daniel explained in person and doubled his job hopping salary
随机推荐
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
BUUCTF---Reverse---easyre
Leetcode brush first_ Maximum Subarray
A5000 vgpu display mode switching
Tencent cloud database public cloud market ranks top 2!
What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
数字三角形模型 AcWing 1018. 最低通行费
BUUCTF---Reverse---easyre
青龙面板白屏一键修复
广州首个数据安全峰会将在白云区开幕
Tencent Android development interview, basic knowledge of Android Development
Digital triangle model acwing 1018 Minimum toll
Node.js: express + MySQL实现注册登录,身份认证
8086 instruction code summary (table)
[Yann Lecun likes the red stone neural network made by minecraft]
Linear distance between two points of cesium
持续测试(CT)实战经验分享
Appx代码签名指南
It's enough to read this article to analyze the principle in depth
An East SMS login resurrection installation and deployment tutorial