当前位置:网站首页>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";
}
边栏推荐
- 使用ssh连接被拒
- Recursive implementation of department tree
- Introduction of Xia Zhigang
- 8086指令码汇总表(表格)
- Cf960g - bandit Blues (type I Stirling number +ogf)
- Guangzhou's first data security summit will open in Baiyun District
- Microservice architecture debate between radical technologists vs Project conservatives
- Redisson bug analysis
- 22-07-05 七牛云存储图片、用户头像上传
- Method keywords deprecated, externalprocname, final, forcegenerate
猜你喜欢
新一代垃圾回收器—ZGC
学习打卡web
22-07-05 upload of qiniu cloud storage pictures and user avatars
redisson bug分析
It's enough to read this article to analyze the principle in depth
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
Hudi vs Delta vs Iceberg
Standardized QCI characteristics
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
Configuration and simple usage of the EXE backdoor generation tool quasar
随机推荐
BUUCTF---Reverse---easyre
Wonderful coding [hexadecimal conversion]
Linear distance between two points of cesium
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
String长度限制?
小微企业难做账?智能代账小工具快用起来
BUUCTF---Reverse---easyre
Transformer model (pytorch code explanation)
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
腾讯云数据库公有云市场稳居TOP 2!
Node. Js: express + MySQL realizes registration, login and identity authentication
Appx代码签名指南
Classic 100 questions of algorithm interview, the latest career planning of Android programmers
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
An East SMS login resurrection installation and deployment tutorial
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
From spark csc. csr_ Matrix generate adjacency matrix
AsyncHandler
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行