当前位置:网站首页>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";
}边栏推荐
- 深度学习分类网络 -- ZFNet
- POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
- Classic 100 questions of algorithm interview, the latest career planning of Android programmers
- Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
- Database specific interpretation of paradigm
- Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
- A5000 vgpu display mode switching
- 系统与应用监控的思路和方法
- AsyncHandler
- Problems encountered in using RT thread component fish
猜你喜欢
随机推荐
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
js获取浏览器系统语言
Poj3617 best cow line
OceanBase社区版之OBD方式部署方式单机安装
广州首个数据安全峰会将在白云区开幕
rt-thread i2c 使用教程
Tencent T2 Daniel explained in person and doubled his job hopping salary
logstash高速入口
Technology sharing | packet capturing analysis TCP protocol
棋盘左上角到右下角方案数(2)
Unity load AB package
Node. Js: express + MySQL realizes registration, login and identity authentication
5. 无线体内纳米网:十大“可行吗?”问题
beegfs高可用模式探讨
Qinglong panel white screen one key repair
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
JS get browser system language


![[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN](/img/b8/3d48e185bb6eafcdd49889f0a90657.png)




