# Count Scorecards

### Problem Statement :

```In a tournament, n players play against each other exactly once. Each game results in exactly one player winning. There are no ties. You have been given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How many possible scorecards are consistent with the input scorecard?

Input Format

The first line contains a single integer t denoting the number of test cases. t test cases follow.

The first line of each test case contains a single integer n. The second line contains n space-separated integers s1,s2,...,sn. si denotes the score of the ith player. If the score of the ith player has been erased, it is represented by -1.

Constraints
1 <= t <= 20
1 <= n <= 40
-1 <= si < n

Output Format

For each test case, output a single line containing the answer for that test case modulo 10^9+7.```

### Solution :

```                            ```Solution in C :

In C++ :

#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std ;
#define MAXN 42
#define MOD 1000000007

int n ;
vector<int> G ;

int check(vector<int> gg)
{
sort(gg.begin(),gg.end()) ;
int sum = 0 ;
for(int i = 0;i < n;i++)
{
sum += gg[i] ;
if(sum < i * (i + 1) / 2) return 0 ;
}
return 1 ;
}

int solve1(int k,int sum)
{
if(sum > n * (n - 1) / 2) return 0 ;
if(k == n) return sum == n * (n - 1) / 2 && check(G) ? 1 : 0 ;
if(G[k] != -1) return solve1(k + 1,sum + G[k]) ;

int ret = 0 ;
for(int i = 0;i < n;i++)
{
G[k] = i ;
ret += solve1(k + 1,sum + i) ;
G[k] = -1 ;
}
return ret ;
}

int solve1(vector<int> g)
{
G = g ;
n = G.size() ;
int ret = solve1(0,0) ;
return ret ;
}

int fac[MAXN],inv[MAXN] ;
int pow(int a,int b)
{
if(b == 0) return 1 ;
int ret = pow(a,b / 2) ;
ret = 1LL * ret * ret % MOD ;
if(b & 1) ret = 1LL * ret * a % MOD ;
return ret ;
}

int occ[MAXN],big[MAXN] ;
int memo[MAXN][MAXN][MAXN * MAXN] ;
int solve2(int k,int last,int sum)
{
if(last == n) return 0 ;
if(sum > n * (n - 1) / 2) return 0 ;
if(k == n) return big[last + 1] == 0 && sum == n * (n - 1) / 2 ? 1 : 0 ;
if(memo[k][last + 1][sum] != -1) return memo[k][last + 1][sum] ;
int occr = occ[last + 1] ;
int ret = 0,added = 0 ;
for(int i = 0;k + i <= n;i++)
{
if(sum + added < (k + i) * (k + i - 1) / 2) break ;
if(i >= occr) ret += 1LL * inv[i - occr] * solve2(k + i,last + 1,sum + added) % MOD ;
if(ret >= MOD) ret -= MOD ;
added += last + 1 ;
}
return memo[k][last + 1][sum] = ret ;
}

int solve2(vector<int> g)
{
fac = inv = 1 ;
for(int i = 1;i < MAXN;i++)
{
fac[i] = 1LL * i * fac[i - 1] % MOD ;
inv[i] = pow(fac[i],MOD - 2) ;
}
n = g.size() ;

int start = 0 ;
memset(occ,0,sizeof occ) ;
memset(big,0,sizeof big) ;
for(int i = 0;i < g.size();i++) if(g[i] != -1) occ[g[i]]++,start++ ;
for(int i = 0;i < n;i++)
for(int j = i;j < n;j++)
big[i] += occ[j] ;
memset(memo,255,sizeof memo) ;
int ret = solve2(0,-1,0) ;
ret = 1LL * ret * fac[n - start] % MOD ;
return ret ;
}

vector<int> gen()
{
int n = rand() % 10 + 1 ;
vector<int> ret ;
for(int i = 0;i < n;i++)
{
if(rand() % 2 == 0) ret.push_back(-1) ;
else ret.push_back(rand() % n) ;
}
return ret ;
}

void test()
{
for(int t = 1;t < 100;t++)
{
vector<int> g = gen() ;
int ret1 = solve1(g) ;
int ret2 = solve2(g) ;

cout << ret1 << " " << ret2 << endl ;
if(ret1 != ret2)
{
cout << "failed on: " << t << endl ;
cout << g.size() << " : " ;
for(int i = 0;i < g.size();i++) cout << g[i] << " " ; cout << endl ;
while(1) ;
}
}
}

void generate()
{
char in[] = "in .txt" ;
for(int test = 0;test < 10;test++)
{
in = test + '0' ;
FILE * fout = fopen(in,"w") ;
int runs = 20 ;
fprintf(fout,"%d\n",runs) ;
for(int t = 0;t < runs;t++)
{
if(rand() % 3 != 0) n = rand() % 40 + 1 ;
else n = 40 - rand() % 10 ;
int per = 70 ;

vector<int> g ;
for(int i = 0;i < n;i++)
{
if(rand() % 100 < per) g.push_back(-1) ;
else g.push_back(rand() % n) ;
}

fprintf(fout,"%d\n",n) ;
for(int i = 0;i < n;i++)
{
if(i) fprintf(fout," ") ;
fprintf(fout,"%d",g[i]) ;
}
fprintf(fout,"\n") ;
}
}
}

int main()
{
// srand(time(NULL)) ;
// generate() ; return 0 ;
// test() ; return 0 ;

int runs ;
cin >> runs ;
while(runs--)
{
int n ;
vector<int> g ;
cin >> n ;
for(int i = 0;i < n;i++)
{
int k ;
cin >> k ;
g.push_back(k) ;
}
int ret = solve2(g) ;
printf("%d\n",ret) ;
}
return 0 ;
}

In C :

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include <stdio.h>
#include <stdlib.h>

#define P 1000000007
#define N 450

long long bin,jj,kk,h,ll,ii,t,a,i,j,k,l,m,n,c,b;

int com(const void * xx, const void *yy)
{
if(*(long long *)xx < *(long long*)yy) return 1;
return -1;
}

int main()
{

for(i=0;i<50;i++) bin[i] = bin[i][i]=1;
for(i=1;i<50;i++)
for(j=1;j<i;j++) bin[i][j] = (bin[i-1][j-1]+ bin[i-1][j])%P;

scanf("%lld",&t);
while(t--)
{
scanf("%lld",&n);
for(i=0;i<n;i++) scanf("%lld",&c[i]);

qsort(c,n,sizeof(c),com);

for(i=0;i<=n;i++) b[i]=0;

m=0;
for(i=0;i<n;i++) if(c[i]!=-1) b[c[i]]++; else m++;

for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
for(k=0;k<=N;k++) a[i][j][k]=0;

a[n] = 1;
l=0;

for(i=n-1;i>=0;i--)
{
for(j=0;j<=m;j++)
{
h=0;
ll=l;
for(ii=0;ii<b[i];ii++)
{
h += (n-ll-j-1) - i;
ll++;
}

// printf("%lld %lld h %lld %lld=l\n",i,j,h,l);

for(k=0;k<=N;k++)
if(k+h>=0) a[i][j][k+h] = (a[i][j][k+h] + a[i+1][j][k])%P;
}

//for(ii=0;ii<=n;ii++)
// for(jj=0;jj<=m;jj++)
//  for(kk=0;kk<=1000;kk++)
//   if(a[ii][jj][kk]) printf("%lld %lld %lld -> %lld\n", ii,jj,kk,a[ii][jj][kk]);
//printf("stred -----------%lld\n",i);

// continue;

for(j=m;j>=0;j--)
for(k=0;k<N;k++)
{
h = k;
for(jj=1;jj<=j && h>=0;jj++)
{
h -= (n-l-b[i]-(j-jj)-1) - i;

if(h<0) break;

a[i][j][k] = (a[i][j][k] + bin[m-j+jj][jj]*a[i][j-jj][h])%P;
//    if(a[i][j-jj][h]>0)     printf("...%lld=i %lld=j %lld=k %lld=jj %lld=h\n",i,j,k,jj,h);
}
}

//for(ii=0;ii<=n;ii++)
// for(jj=0;jj<=m;jj++)
//  for(kk=0;kk<=1000;kk++)
//   if(a[ii][jj][kk]) printf("%lld %lld %lld -> %lld\n", ii,jj,kk,a[ii][jj][kk]);

//printf("-----------%lld\n",i);

l+=b[i];
}

/*
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
for(k=0;k<=1000;k++)
if(a[i][j][k]) printf("%lld %lld %lld -> %lld\n", i,j,k,a[i][j][k]);

for(i=0;i<=n;i++) printf("%lld %lld\n",i,c[i]);
*/
printf("%lld\n",(a[m]+P)%P);
//return 0;

}

return 0;
}```
```

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio