题目大意:反正是没怎么看懂,可以看一下这位大牛的博客 http://hi.baidu.com/_lt_zyc/item/bcfb7af5f9280801d6ff8cd7,说实话,看了之后,也不是很懂。。。
思路:这位大牛里的博客里也说了,可我还不是很理解,题意有点坑,先放一放吧,今天太晚了,以后再来填。。 = =
代码如下(借鉴着别人的代码,自己也写了个 ,因为写得比较挫,INF由于一直加,会爆,因为这个还检查了很久。。= =):
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int INF = 0x0fffffff ; const int MAXN = 50055; vector <int> G[MAXN]; int trap[MAXN]; int d[MAXN][5]; void dfs(int u,int fa) { if(trap[u]) { d[u][0] = INF; d[u][1] = 0; d[u][2] = 0; return ; } int l = -1,r = -1; for(int i = 0;i<G[u].size();i++) { int v = G[u][i]; if(v == fa) continue; if(l == -1) l = v; else r = v; dfs(v,u); } if(l == -1) { d[u][0] = 0; d[u][1] = 0; d[u][2] = 1; } else if(r == -1) { d[u][0] = d[l][0]; d[u][1] = d[l][1]; d[u][2] = d[l][2]; } else { d[u][0] = d[l][0] + d[r][0]; d[u][0] = min(d[u][0],INF); d[u][1] = min(d[l][0] + d[r][1],d[l][1] + d[r][0]); d[u][1] = min(d[u][1],min(d[l][2] + d[r][0],d[l][0] + d[r][2])); d[u][1] = min(INF,d[u][1]); d[u][2] = min(d[u][0],d[u][1]) + 1; d[u][2] = min(d[u][2],d[l][2] + d[r][2]); d[u][2] = min(INF,d[u][2]); } //printf("u = %d,%d,%d,%d\n",u,d[u][0],d[u][1],d[u][2]); } int main() { int n,m; while(~scanf("%d%d",&n,&m) && (n+m)) { for(int i = 1;i<=n;i++) G[i].clear(); for(int i = 1;i<=n;i++) { int k; scanf("%d",&k); while(k--) { int a; scanf("%d",&a); G[i].push_back(a); } trap[i] = 0; } for(int i = 1;i<=m;i++) { int a; scanf("%d",&a); trap[a] = 1; } dfs(1,-1); printf("%d\n",d[1][2]); } return 0; }