Cell Phone Network——(最小点覆盖-树形DP)
Cell Phone Network
https://ac.nowcoder.com/acm/problem/24953
题意
• 给你一棵无向树,问你最少用多少个点可以覆盖掉所有其他的点。
• (一个点被盖,它自己和与它相邻的点都算被覆盖)
思路
最小的点覆盖
确定三种状态
注意边界情况,设立哨兵 or 特判
0:覆盖u,
1:被son覆盖 (有点复杂:至少被一个v覆盖)
根据贡献来,来,差值从小到大排序
排序后,第一个点必须取,之后根据贡献
2:被fat覆盖
题目链接
//#pragma GCC optimize(2) //#pragma GCC target ("sse4") #include<bits/stdc++.h> //typedef long long ll; #define ull unsigned long long #define int long long #define F first #define S second #define endl "\n"//<<flush #define eps 1e-6 #define base 131 #define lowbit(x) (x&(-x)) #define db double #define PI acos(-1.0) #define inf 0x3f3f3f3f #define MAXN 0x7fffffff #define INF 0x3f3f3f3f3f3f3f3f #define _for(i, x, y) for (int i = x; i <= y; i++) #define for_(i, x, y) for (int i = x; i >= y; i--) #define ferma(a,b) pow(a,b-2) #define mod(x) (x%mod+mod)%mod #define pb push_back #define decimal(x) cout << fixed << setprecision(x); #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define memset(a,b) memset(a,b,sizeof(a)); #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; #ifndef ONLINE_JUDGE #include "local.h" #endif template<typename T> inline T fetch(){T ret;cin >> ret;return ret;} template<typename T> inline vector<T> fetch_vec(int sz){vector<T> ret(sz);for(auto& it: ret)cin >> it;return ret;} template<typename T> inline void makeUnique(vector<T>& v){sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());} void file() { #ifdef ONLINE_JUDGE #else freopen("D:/LSNU/codeforces/duipai/data.txt","r",stdin); // freopen("D:/LSNU/codeforces/duipai/WA.txt","w",stdout); #endif } const int N=1e4+5; int dp[N][3]; vector<int>G[N]; struct node { int num0,num1,value; bool operator<(const node &b)const { return value<b.value; } }; void dfs(int u,int fat) { int sum=0; int sum2=0; vector<node>vec; int flag=1; for(auto v:G[u]) { if(v==fat) continue; dfs(v,u); sum+=min({dp[v][0],dp[v][1],dp[v][2]}); vec.pb({dp[v][0],dp[v][1],dp[v][0]-dp[v][1]}); sum2+=min(dp[v][0],dp[v][1]); flag=0; } sort(all(vec)); dp[u][0]=1+sum; int len=vec.size(); if(len) dp[u][1]=vec[0].num0; for(int i=1;i<len;i++) dp[u][1]+=min(vec[i].num0,vec[i].num1); dp[u][2]=sum2; if(flag) dp[u][1]=1; } signed main() { IOS; file(); int n; cin>>n; _for(i,2,n) { int u,v; cin>>u>>v; G[u].pb(v); G[v].pb(u); } dfs(1,0); if(n<=2) { cout<<1<<endl; return 0; } cout<<min(dp[1][0],dp[1][1])<<endl; return 0; }