4eyes4u

Untitled

Mar 14th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define MOD1 1000100009
  5. #define MOD2 1000100007
  6. #define DOUBLES (false)
  7. using namespace std;
  8.  
  9. int H1[10005],H2[10005];
  10.  
  11. int O[10005],N,S,PA[10005],A[10005],la=MOD2,hb=0,rn;
  12. bool Root[10005];
  13.  
  14. void ucitajisredi(){
  15.     int i,c,tot=0,j;
  16.     scanf("%d%d",&N,&S);
  17.     for (i=1; i<=N; i++) Root[i] = true;
  18.     for (i=1; i<=N; i++){
  19.         scanf("%d%d",A+i,&c);
  20.         PA[i] = tot+1;
  21.         for (j=1; j<=c; j++){
  22.             scanf("%d",&O[tot+j]);
  23.             //printf("not root %d\n",O[tot+j]);
  24.             Root[O[tot+j]] = false;
  25.         }
  26.         tot += c;
  27.     }
  28.     for (i=1; i<=N; i++) if (Root[i]) rn = i;
  29.     PA[N+1] = tot+1;
  30. }
  31.  
  32. inline bool ima2(int x){
  33.     return (H1[x]!=0)||(H2[x]!=0);
  34. }
  35.  
  36. inline bool ima1(int x){
  37.     return H1[x]!=0;
  38. }
  39.  
  40. void rek(int x){
  41.     //printf("rek %d\n",x);
  42.     int i,j;
  43.     //dinamicki ubacimo
  44.     for (i=S; i>=0; i--){
  45.         j = i+A[x];
  46.         if (DOUBLES){
  47.             if (!ima2(i)) continue;
  48.         } else {
  49.             if (!ima1(i)) continue;
  50.         }
  51.         //printf("ima %d %d hb = %d\n",i,j,hb);
  52.         if (j > S){
  53.             la = min(la,j);
  54.         } else {
  55.             H1[j] += H1[i];
  56.             if (H1[j] >= MOD1) H1[j] -= MOD1;
  57.             if (DOUBLES){
  58.                 H2[j] += H2[i];
  59.                 if (H2[j] >= MOD2) H2[j] -= MOD2;
  60.             }
  61.             hb = max(hb,j);
  62.         }
  63.     }
  64.  
  65.     if (hb==S){
  66.         printf("%d\n",S);
  67.         exit(0);
  68.     }
  69.  
  70.     //rekurzija
  71.     for (i=PA[x]; i<PA[x+1]; i++) rek(O[i]);
  72.     //vratimo se
  73.     for (i=A[x]; i<=S; i++){
  74.         j = i-A[x];
  75.         //potencijalno obrisi ovaj red
  76.         if (DOUBLES){
  77.             if (!ima2(j)) continue;
  78.         } else {
  79.             if (!ima1(j)) continue;
  80.         }
  81.         H1[i] -= H1[j];
  82.         if (H1[i] < 0) H1[i] += MOD1;
  83.         if (DOUBLES){
  84.             H2[i] -= H2[j];
  85.             if (H2[i] < 0) H2[i] += MOD2;
  86.         }
  87.     }
  88. }
  89.  
  90. int main(){
  91.     //freopen("porodicno.in","r",stdin);
  92.     H1[0] = 1;
  93.     H2[0] = 1;
  94.     ucitajisredi();
  95.     //printf("%d\n",rn);
  96.     rek(rn);
  97.     //printf("%d %d\n",hb,la);
  98.     if (S-hb <= la-S){
  99.         printf("%d\n",hb);
  100.     } else {
  101.         printf("%d\n",la);
  102.     }
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment