标签:状态 依次 using dfs iostream mat 其它 continue oid
\[ dp[u][0]=\sum_{i=1}^{q}dp[son[i]][1];\dp[u][1]=\sum_{i=1}^{q}Min(dp[son[i]][0],dp[son[i]][1]); \]
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 1501
using namespace std;
struct edge{
int to,next;
edge(){}
edge(const int &_to,const int &_next){
to=_to,next=_next;
}
}e[maxn<<1];
int head[maxn],k;
int dp[maxn][2],n;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
inline void add(const int &u,const int &v){ e[k]=edge(v,head[u]); head[u]=k++; }
void dfs(int u,int pre){
dp[u][0]=0,dp[u][1]=1;
for(register int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(v==pre) continue;
dfs(v,u);
dp[u][0]+=dp[v][1];
dp[u][1]+=min(dp[v][0],dp[v][1]);
}
}
int main(){
memset(head,-1,sizeof head);
n=read();
for(register int i=1;i<=n;i++){
int u=read()+1,cnt=read();
while(cnt--){ int v=read()+1; add(u,v),add(v,u); }
}
memset(dp,0x3f,sizeof dp);
dfs(1,0);
printf("%d\n",min(dp[1][0],dp[1][1]));
return 0;
}
标签:状态 依次 using dfs iostream mat 其它 continue oid
原文地址:https://www.cnblogs.com/akura/p/10913924.html