poj1463 NC106060 Strategic game(树形DP,树的最小覆盖点)
Strategic game
https://ac.nowcoder.com/acm/problem/106060
题意: 一城堡的所有的道路形成一个n个节点的树,如果在一个节点上放上一个士兵,那么和这个节 点相连的边就会被看守住,问把所有边看守住最少需要放多少士兵。
思路:用dp[i][0]表示以i为根节点,但是i节点不放士兵需要看守的最少士兵是多少,dp[i][1]表示以i为根节点,i节点放置守卫时所需的最少士兵,v为u的子节点,当u这个点不选的时候,那么以u延展的边则无人看守此时u的子节点必须有守卫那么
,若u放置守卫,那么对于与u相连的边都被看守,此时子节点守卫可有可无,
,因为是树形所以我们吧根节点找到,最后答案就是min(dp[root][1],dp[root][0]) 很像没有上司的舞会
#include <cstdio> #include <cstring> #include <algorithm> #include <set> #include<iostream> #include<vector> #include<queue> #include<stack> #include<bits/stdc++.h> using namespace std; typedef long long ll; #define SIS std::ios::sync_with_stdio(false) #define space putchar(' ') #define enter putchar('\n') #define lson root<<1 #define rson root<<1|1 typedef pair<int,int> PII; const int mod=998244353; const int N=2e6+10; const int M=2e3+10; const int inf=0x7f7f7f7f; const int maxx=2e5+7; ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } ll lcm(ll a,ll b) { return a*(b/gcd(a,b)); } template <class T> void read(T &x) { char c; bool op = 0; while(c = getchar(), c < '0' || c > '9') if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x; } template <class T> void write(T x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar('0' + x % 10); } ll qsm(int a,int b,int p) { ll res=1%p; while(b) { if(b&1) res=res*a%p; a=1ll*a*a%p; b>>=1; } return res; } vector<int> G[1505]; int dp[1505][2]; int vis[1505]; void dfs(int u) { dp[u][1]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; //if(v==fa)continue; dfs(v); dp[u][1]+=min(dp[v][0],dp[v][1]); dp[u][0]+=dp[v][1]; } } int main() { int n; while(~scanf("%d",&n)) { memset(vis,0,sizeof vis); memset(dp,0,sizeof dp); for(int i=0;i<=n;i++) G[i].clear(); for(int i=1;i<=n;i++) { int a,m; scanf("%d:(%d)",&a,&m); while(m--) { int b; scanf("%d",&b); G[a].push_back(b); vis[b]++; //G[b].push_back(a); } } int root=0; while(vis[root])root++; dfs(root); printf("%d\n",min(dp[root][1],dp[root][0])); } return 0; }