【题解】Luogu SP1435 PT07X - Vertex Cover
生活随笔
收集整理的這篇文章主要介紹了
【题解】Luogu SP1435 PT07X - Vertex Cover
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原題傳送門
求樹的最小點(diǎn)覆蓋,就是一個(gè)樹形dp
類似于沒有上司的舞會(huì)
dp的狀態(tài)為\(f[i][0/1]\),表示i節(jié)點(diǎn)是否選擇
邊界是\(f[x][0]=0\),\(f[x][1]=1\)
轉(zhuǎn)移方程是
\(f[i][0]=\sum_{j=son[i]} f[j][1]\)
\(f[i][1]=\sum_{j=son[i]} Min(f[j][0],f[j][1])\)
最后答案就是\(Min(f[1][0],f[1][1])\)
#include <bits/stdc++.h> #define getchar nc #define N 100005 using namespace std;inline char nc(){static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } inline int read() {register int x=0,f=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f; } inline void write(register int x) {if(!x)putchar('0');if(x<0)x=-x,putchar('-');static int sta[20];register int tot=0;while(x)sta[tot++]=x%10,x/=10;while(tot)putchar(sta[--tot]+48); } inline int Min(register int a,register int b) {return a<b?a:b; } struct node{int to,next; }e[N<<1]; int head[N],cnt=0; inline void add(register int u,register int v) {e[++cnt]=(node){v,head[u]};head[u]=cnt; } int n,f[N][2]; inline void dfs(register int u,register int fa) {f[u][1]=1;f[u][0]=0;for(register int i=head[u];i;i=e[i].next){int v=e[i].to;if(v==fa)continue;dfs(v,u);f[u][1]+=Min(f[v][0],f[v][1]);f[u][0]+=f[v][1];} } int main() {n=read();for(register int i=1;i<n;++i){int u=read(),v=read();add(u,v),add(v,u);}dfs(1,0);write(Min(f[1][0],f[1][1]));return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/yzhang-rp-inf/p/10329700.html
總結(jié)
以上是生活随笔為你收集整理的【题解】Luogu SP1435 PT07X - Vertex Cover的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快领!了不起的程序员专属红包封面!!
- 下一篇: 讲点码德!避免这些代码坏味道,努力做一名