日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【题解】Luogu SP1435 PT07X - Vertex Cover

發布時間:2025/3/16 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【题解】Luogu SP1435 PT07X - Vertex Cover 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原題傳送門

求樹的最小點覆蓋,就是一個樹形dp

類似于沒有上司的舞會

dp的狀態為\(f[i][0/1]\),表示i節點是否選擇

邊界是\(f[x][0]=0\)\(f[x][1]=1\)

轉移方程是

\(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; }

轉載于:https://www.cnblogs.com/yzhang-rp-inf/p/10329700.html

總結

以上是生活随笔為你收集整理的【题解】Luogu SP1435 PT07X - Vertex Cover的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。