D. Sasha and a Walk in the City
1.P3478 [POI2008] STA-Station2.E1. Weights Division (easy version)3.P5007 DDOSvoid 的疑惑4.P2168 [NOI2015] 荷马史诗5.P8306 【模板】字典树6.P1481 魔族密码7.P3128 [USACO15DEC] Max Flow P8.P3258 [JLOI2014] 松鼠的新家9.P5536 【XR-3】核心城市10.P5836 [USACO19DEC] Milk Visits S11.P3384 【模板】重链剖分/树链剖分12.P3038 [USACO11DEC] Grass Planting G13.P4551 最长异或路径14.F. Alex's whims15.J. 上学16.Game on Tree17.E. We Need More Bosses18.A. Party19.D. Strong Vertices20.B. Omkar and Heavenly Tree21.B. Mahmoud and Ehab and the bipartiteness22.P8655 [蓝桥杯 2017 国 B] 发现环23.P10298 [CCC 2024 S4] Painting Roads24.F. Gardening Friends25.D. Playoff Tournament26.E. Lomsat gelral27.F. Minimum Maximum Distance28.D. Book of Evil29.P2195 HXY造公园30.P3304 [SDOI2013] 直径
31.D. Sasha and a Walk in the City
32.D. Vitaly and Cycle33.P10838 『FLA - I』庭中有奇树34.F - Perfect Matching on a Tree题意
树中任意一条路径上黑色点的数量不超过两个,请问存在多少种树
分析
先随便找一个节点作为根节点,然后分类讨论
假如根到叶子节点的路径上有两个黑色节点,则不能再添加其他点了
如果根到叶子节点的路径上有一个黑色节点,则可以还可以在不在这条路径上的地方放黑色节点
在弄清楚规则后,就可以用dp了(真是万能啊,完全没有要用dp的intuition)
其实dp只能算一种主动运用的方法,当后者于前者有继承关系时,就可以用了,状态的设计也是很巧妙
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=998244353;
int dp[300005][3]={0};
vector<int> G[300005];
void dfs(int now,int fa)
{
dp[now][0]=1;
dp[now][2]=0;
int tem=1;
for(auto next:G[now])
{
if(next==fa) continue;
dfs(next,now);
tem*=(1+dp[next][1]);
dp[now][2]+=dp[next][2]+dp[next][1];
}
dp[now][1]=tem;
}
void solve()
{
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1,1);
cout<<dp[1][1]+dp[1][2]+1<<'\n';
for(int i=1;i<=n;i++) G[i].clear();
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~