ZOJ1427 POJ1694 An Old Stone Game

来源:互联网 发布:语言障碍评估软件 编辑:程序博客网 时间:2024/05/17 23:46

很简单的递归题。注意记录下节点子节点需要的石子数,然后从最大开始排列,取第一个为需要的石头数,遍历记录,如果余下的石头不足以支付,则需要补充。


/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2012-03-21 19:27 # Filename: ZOJ1427 POJ1694 An Old Stone Game.cpp # Description :  ******************************************************************************/#ifdef _MSC_VER#define DEBUG#define _CRT_SECURE_NO_DEPRECATE#endif#include <fstream>#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <limits.h>#include <algorithm>#include <math.h>#include <numeric>#include <functional>#include <ctype.h>#define MAX 210using namespace std;int children[MAX][MAX];int func(const int &r){int array[MAX];if(children[r][0]==0)return 1;int cnt=0;while(children[r][cnt]){array[cnt]=func(children[r][cnt]);++cnt;}sort(array,array+cnt,greater<int>());int ans=array[0],tmp=array[0]-1;for(int i=1;i<cnt;++i)if(tmp<array[i]){ans+=(array[i]-tmp);tmp=array[i]-1;}else--tmp;return ans;}int main(void){#ifdef DEBUG    freopen("../stdin.txt","r",stdin);  freopen("../stdout.txt","w",stdout); #endif  int n,ncases,parent,tmp;scanf("%d",&ncases);while(ncases--){scanf("%d",&n);memset(children,0,sizeof(children));for(int i=0;i<n;++i){scanf("%d%d",&parent,&tmp);for(int j=0;j<tmp;++j)scanf("%d",&children[parent][j]);}int ans=func(1);printf("%d\n",ans);}  return 0;}


原创粉丝点击