当前位置:网站首页>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";
}
边栏推荐
- Oceanbase Community Edition OBD mode deployment mode stand-alone installation
- PowerPivot——DAX(初识)
- 22-07-05 upload of qiniu cloud storage pictures and user avatars
- 数据的同步为每个站点创建触发器同步表
- js获取浏览器系统语言
- 句号压缩过滤器
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- Guangzhou's first data security summit will open in Baiyun District
- Classic 100 questions of algorithm interview, the latest career planning of Android programmers
- Color is converted to tristimulus value (r/g/b) (dry stock)
猜你喜欢
深度学习分类网络 -- ZFNet
Introduction to enterprise lean management system
Tencent Android interview must ask, 10 years of Android development experience
BeagleBoneBlack 上手记
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
Discussion on beegfs high availability mode
Selenium advanced operations
Configuration and simple usage of the EXE backdoor generation tool quasar
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
随机推荐
How to handle the timeout of golang
腾讯字节阿里小米京东大厂Offer拿到手软,老师讲的真棒
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
PowerPivot——DAX(初识)
Technology sharing | packet capturing analysis TCP protocol
[network planning] Chapter 3 data link layer (3) channel division medium access control
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
颜色(color)转换为三刺激值(r/g/b)(干股)
Classic 100 questions of algorithm interview, the latest career planning of Android programmers
微信小程序常用集合
PowerPivot - DAX (first time)
rt-thread i2c 使用教程
8086 instruction code summary (table)
腾讯安卓开发面试,android开发的基础知识
String长度限制?
Appx代码签名指南
Selenium advanced operations
Method keywords deprecated, externalprocname, final, forcegenerate
Guangzhou's first data security summit will open in Baiyun District
小微企业难做账?智能代账小工具快用起来