Tuesday 8 August 2017

Lightoj 1382 - The Queue

http://lightoj.com/volume_showproblem.php?problem=1382

Problem analysis:

This is a rare problem i wrote about so far.  After much struggling we can actually see that this is nothing but a tree combination that means we can represent each permutation of queue as a tree where the root is the supervisor and each of the  employee under that supervisor is the the child of that root. that means in the queue no child can be before it's root. after building the tree the only question is how many ways are there to represent the queue using the tree where no child is before and root or root of root?

After much struggling and using different combinatorics rules we can see that there is no way without making the problem smaller (yeah i am talking about DP) and building the ultimate answer using those smaller results. So first these questions are important to answer??
1. What this root actually means of the tree??
2. How the childs are connected to the root?? By this I mean how to combine the results of the childs results to construct the roots result??
and finally
3. how to find the result of the childs??

Answers:
1. root means whatever i do later that is not my concern but right now this root must be in the position (the child or child of child is for later)  and  than go for the childs.

2. here for each root we can calculate how many positions available after fixing the position of the root and than we can also calculate how many positions are needed for a certain child(total number of that child including that child) so if our function is dp(int root) for each child we can just use

ncr(total_positions_available,positions_needed_for_child_i)*dp(i)

we can pre calculate the values of ncr and after constructing the tree we can easily calculate number of childs of each root. that we can use the DP function to find the answer.

Here goes the code.

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MOD 1000000007
  4. #define llu long long unsigned
  5. vector<int>v[1005];
  6. bool vis[1005];
  7. llu chi[1005];
  8. llu ncr[1005][1005];
  9. llu dp[1005];
  10. llu child(llu root){
  11.     if(v[root].size() == 0) return chi[root] = 1;
  12.     llu sum = 0;
  13.     for(int i = 0; i <(int)v[root].size(); i++){
  14.         sum += child(v[root][i]);
  15.     }
  16.     return chi[root] = 1+sum;
  17. }
  18. void NCR(){
  19.     ncr[0][0] = 1;
  20.     for(int i = 1; i <1005; i++){
  21.         for(int j = 0; j <=i; j++){
  22.             if(== 0 || j == i) ncr[i][j] = 1;
  23.             else ncr[i][j]= (ncr[i-1][j]+ncr[i-1][j-1])%MOD;
  24.         }
  25.     }
  26.     /*
  27.     for(int i = 0; i <10; i++){
  28.         for(int j = 0; j <10; j++){
  29.             printf("%d ", ncr[i][j]);
  30.         }
  31.         printf("\n");
  32.     }
  33.     */
  34. }
  35. llu DP(llu root){
  36.     llu totalsize = chi[root]-1; // except himself
  37.     llu ans = 1;
  38.     for(int i = 0; i <(int)v[root].size(); i++){
  39.         llu chld = v[root][i];
  40.         llu chldsize = chi[chld];
  41.         ans*=(ncr[totalsize][chldsize]*DP(chld))%MOD;
  42.         ans%=MOD;
  43.         totalsize-=chldsize;
  44.     }
  45.     return dp[root] = ans;
  46. }
  47. int main(){
  48.     NCR();
  49.     int ts;
  50.     scanf("%d"&ts);
  51.     for(int p = 1; p <=ts; p++){
  52.     int n;
  53.     scanf("%d"&n);
  54.     memset(vis, falsesizeof(vis));
  55.     for(int i = 1; i <=n; i++) v[i].clear();
  56.     for(int i = 1; i <n; i++){
  57.         int a, b;
  58.         scanf("%d%d"&a, &b);
  59.         v[a].push_back(b);
  60.         vis[b] = true;
  61.     }
  62.     int mark;
  63.     for(int i = 1; i <=n; i++){
  64.         if(!vis[i]){mark = i;break;}
  65.     }
  66.     child(mark);
  67.     printf("Case %d: %lld\n",p,  DP(mark));
  68.     }
  69.     return 0;
  70. }

2 comments:

  1. Superb, what a weblog it is! This website presents helpful information to us, keep
    it up.

    We can also get more about queue from here -
    https://www.etutorialspoint.com/index.php/data-structure/queue-introduction

    ReplyDelete

Most Featured Post

Lightoj 1159 - Batman

http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...