The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)

简介: The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)

linkkkk

题意:

给出一棵树,每次可以删去任意点,要求删完后不能有孤立的点,求方案数。

思路:

大概很容易看出来是个树形dp,状态不太好想。

d p [ u ] [ 0 ]表示删去这个点

d p [ u ] [ 1 ]表示不删这个点,而且删去所有子节点

d p [ u ] [ 2 ] 表示不删这个点,而且至少留一个子节点

对于第一种情况,此时的u一定满足条件;

对于第二种情况,要求u的父亲节点也不被删去才可以满足条件;

对于第三种情况,此时的u已经满足条件了。

转移的话:

dp[u][0]=(dp[t][0]+dp[t][2])%mod*dp[u][0]%mod;
dp[u][1]=dp[u][1]*dp[t][0]%mod;

如果删去u的话,那么想让子节点t满足条件,可以删除t,也可以不删t并且至少保留t的一个子节点;

如果不删u并且删去所有子节点的话,说明节点t是被删除的状态。

对于d p [ u ] [ 2 ]可以用所有合法的方案数减去不合法的方案数。

最后答案就是d p [ 1 ] [ 0 ] + d p [ 1 ] [ 2 ]

代码:

const int maxn=1e5+7,inf=0x3f3f3f3f;
const ll mod=998244353;
const double eps=1e-5;
vector<int>g[maxn];
ll dp[maxn][3],n;
void dfs(int u,int fa){
  dp[u][0]=dp[u][1]=dp[u][2]=1;
  for(auto t:g[u]){
    if(t==fa) continue;
    dfs(t,u);
    dp[u][0]=(dp[t][0]+dp[t][2])%mod*dp[u][0]%mod;
    dp[u][1]=dp[u][1]*dp[t][0]%mod;
    dp[u][2]=(dp[t][0]+dp[t][2]+dp[t][1])%mod*dp[u][2]%mod; 
  }
  dp[u][2]=(dp[u][2]-dp[u][1])%mod;
}
int main(){
  int _=read;
  while(_--){
    n=read;
    rep(i,1,n-1){
      int u=read,v=read;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    dfs(1,-1);
    ll ans=dp[1][0]+dp[1][2];
    ans=(ans+mod)%mod;
    printf("%d\n",ans);
    rep(i,1,n) g[i].clear(),dp[i][0]=dp[i][1]=dp[i][2]=0;
  }
    return 0;
}
目录
相关文章
DCFEE: A Document-level Chinese Financial Event Extraction System based on Automatically Labeled论文解读
我们提出了一个事件抽取框架,目的是从文档级财经新闻中抽取事件和事件提及。到目前为止,基于监督学习范式的方法在公共数据集中获得了最高的性能(如ACE 2005、KBP 2015)。这些方法严重依赖于人工标注的训练数据。
225 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
237 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
German collegiate programming contest 2012 - Ski Jumping
首先用动能定理算出平抛的初速度v0,然后分三种情况,0~L/2,L/2~L,L~无穷远。
159 0
German collegiate programming contest 2012 - Ski Jumping
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
164 0
When Tech Meets Love – Smarter Ways to NOT be Single
It’s that time of year again. Single’s Day (a.k.a Double 11) is just around the corner, people buying gifts for loved ones.
1663 0
When Tech Meets Love – Smarter Ways to NOT be Single
Protecting Websites through Semantics-Based Malware Detection
Malware detection is a fundamental feature of web security and serves as the first line of defense for most websites.
1387 0
English Contest -- The Programmer&#39;s Dream
<p></p> <p>Yes, The Programmer’s Dream!</p> <p>How many programmers here? I am sure of that every body here has a dream inside. What’s the biggest dream or goal you have in your life at this mom
1719 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.8
Prove that for any matrices $A,B$ we have $$\bex |\per (AB)|^2\leq \per (AA^*)\cdot \per (B^*B). \eex$$ (The corresponding relation for determinants is an easy equality.
599 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等