Problem:
Highway Network
The highway network in Zuia consists of cities (points) and the roads that connect them. The Ivashilo Motorway, which runs through the Ivashilo region, is an important national artery. The upstream sections of the Ivashilo Motorway are constructed as follows.
There are $N$ cities (points) assigned the numbers $1$ to $N$.
Point $1$ is the starting point and point $N$ is the ending point.
Roads are one-way, and there is only one road at most that directly connects one point to another.
Any point can always be reached from any starting point, and any point can be reached from any ending point.
No matter how you follow the road, you will never return to the same point.
The Zuia nation decided to survey the upstream line of the Iwashiro Expressway. In this survey, for each point on the upstream line, we need to find the sum of the lengths of all the paths that go from the starting point to the end point through that point.
The number of points and the information about the road are given.  Write a program that outputs the sum of the lengths of all routes from the starting point through point $i$ to the end point for each point $i$. Let the length of the path be the number of roads traversed.
Input
Input is given in the following form.
$N$ $M$
$u_1$ $v_1$
$u_2$ $v_2$
:
$u_M$ $v_M$
In the first line, the number of points $N$ ($2 \leq N \leq 50$) and the number of roads $M$ ($N-1 \leq M \leq N \times (N-1)/2$) are given. In the following $M$ lines, the starting point $u_i$ and the arrival point $v_i$ ($1 \leq u_i < v_i \leq N$) of the $i$-th road are given.
Output
The output consists of $N$ lines, where the $i$-th line is the total length of the path through point $i$.
Sample Input and Output
Sample Input 1
3 3
1 2
2 3
1 3
Sample Output 1
3
2
3
There are 2 routes from the starting point to the ending point through 1: 1 $\rightarrow$ 2 $\rightarrow$ 3, 1 $\rightarrow$ 3, and the total length is 3.
There are 1 routes from the starting point to the ending point through 2: 1 $\rightarrow$ 2 $\rightarrow$ 3, and the total is 2.
There are 2 routes from the starting point to the ending point through 3: 1 $\rightarrow$ 2 $\rightarrow$ 3, 1 $\rightarrow$ 3, and the total is 3.
Sample Input 2
6 8
1 2
1 3
2 3
2 4
3 5
4 5
4 6
5 6
Sample Output 2
14
11
7
7
11
14

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,m;
vector<vector<int>>s(51,vector<int>(0));
vector<int>t(51,0);

void dfs(int now,int bit,int cost){
    if(now==n){
        for(int j=1;j<=n;j++){
            if(bit&1<<j){
                t[j]+=cost;
            }
        }
    }else{
        for(int i=0;i<s[now].size();i++){
            dfs(s[now][i],(bit|(1<<s[now][i])),cost+1);
        }
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int u,v;cin>>u>>v;
        s[u].push_back(v);
    }
    dfs(1,2,0);
    for(int i=1;i<=n;i++){
        cout<<t[i]<<"\n";
    }
}
accuracy: 0.6
time_ms: 80
memory_kb: 3488
final_rank: 8

code_snippet: #include<cstdio>
#include<cstring>
#include<algorithm>
#define y 50*(50-1)/2
using namespace std;
int main(void)
{
	long long n,m,u[y],v[y],sa[51]={0},sum,i,j,k,l;
	long long dp[51][y]={0},dp2[51][y]={0};
	scanf("%lld %lld",&n,&m);
	for(i=0;i<m;i++)	scanf("%lld %lld",&u[i],&v[i]);
	dp[1][0]=1;
	for(i=1;i<=n;i++){
		for(j=0;j<n;j++){
			if(dp[i][j]!=0){
				for(k=0;k<m;k++){
					if(i==u[k]){
						dp[v[k]][j+1]+=dp[i][j];
					}
				}
			}
		}
	}
	for(i=1;i<=n;i++){
		dp2[1][0]=1;
		for(j=1;j<=n;j++){
			for(k=0;k<n;k++){
				if(dp2[j][k]!=0){
					for(l=0;l<m;l++){
						if(j==u[l]){
							if(i!=u[l] && i!=v[l]){
								dp2[v[l]][k+1]+=dp2[j][k];
							}
						}
					}
				}
			}
		}
		for(j=1;j<=n;j++){
			sa[i]+=dp[n][j]*j;
		}
		for(j=1;j<=n;j++){
			sa[i]-=dp2[n][j]*j;
		}
		for(j=0;j<51;j++){
			for(k=0;k<y;k++)	dp2[j][k]=0;
		}
	}
	for(i=1;i<=n;i++)	printf("%lld\n",sa[i]);
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3748
final_rank: 4

code_snippet: #include<cstdio>
#include<cstring>
#include<algorithm>
#define y 50*(50-1)/2
using namespace std;
int main(void)
{
	int n,m,u[y],v[y],sa[51]={0},sum,i,j,k,l;
	int dp[51][y]={0},dp2[51][y]={0};
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++)	scanf("%d %d",&u[i],&v[i]);
	dp[1][0]=1;
	for(i=1;i<=n;i++){
		for(j=0;j<n;j++){
			if(dp[i][j]!=0){
				for(k=0;k<m;k++){
					if(i==u[k]){
						dp[v[k]][j+1]+=dp[i][j];
					}
				}
			}
		}
	}
	for(i=1;i<=n;i++){
		dp2[1][0]=1;
		for(j=1;j<=n;j++){
			for(k=0;k<n;k++){
				if(dp2[j][k]!=0){
					for(l=0;l<m;l++){
						if(j==u[l]){
							if(i!=u[l] && i!=v[l]){
								dp2[v[l]][k+1]+=dp2[j][k];
							}
						}
					}
				}
			}
		}
		for(j=1;j<=n;j++){
			sa[i]+=dp[n][j]*j;
		}
		for(j=1;j<=n;j++){
			sa[i]-=dp2[n][j]*j;
		}
		for(j=0;j<51;j++){
			for(k=0;k<y;k++)	dp2[j][k]=0;
		}
	}
	for(i=1;i<=n;i++)	printf("%d\n",sa[i]);
	return 0;
}
accuracy: 0.84
time_ms: 20
memory_kb: 3260
final_rank: 5

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
long long dp[51][51][51],tol=0;
int main(void)
{
	long long n,m,i,j,k,u[1250],v[1250],cnt=1,l,tol2=0;
	scanf("%lld %lld",&n,&m);
	for(i=0;i<m;i++) scanf("%lld %lld",&u[i],&v[i]);
	for(i=1;i<=n;i++) dp[i][1][0]=1;
	for(l=1;l<n;l++){
		for(i=1;i<=n;i++){
			for(j=0;j<n;j++){
				if(dp[l][i][j]!=0){
					for(k=0;k<m;k++){
						if(u[k]==i&&v[k]!=l) dp[l][v[k]][j+1]+=dp[l][i][j];
					}
				}
			}
		}
	}
	for(i=1;i<n;i++){
		for(j=0;j<n;j++){
			tol+=dp[i][n][j]*j;
		}
		if(i==1){
			tol2=tol;
			printf("%lld\n",tol2);
		}
		else printf("%lld\n",tol2-tol);
		tol=0;
	}
	printf("%lld\n",tol2);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3752
final_rank: 2

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
	long long n,m,i,j,k,l,an,an1=0,a[1300],b[1300],dp[51][50][50],cn[51];
	scanf("%lld %lld",&n,&m);
	for(i=0;i<=n;i++){
		for(j=0;j<n;j++){
			for(k=1;k<n;k++){
				dp[i][j][k]=0;
			}
		}
	}
	for(i=0;i<n;i++) cn[i]=0; 
	for(i=1;i<n;i++) dp[i][1][0]=1;
	for(i=1;i<=m;i++){
		scanf("%lld %lld",&a[i],&b[i]);
		cn[b[i]]++;
	}
	for(l=1;l<n;l++){
		for(i=1;i<n;i++){
			for(j=0;j<n;j++){
				if(dp[l][i][j]!=0){
					for(k=1;k<=m;k++){
						if(a[k]==i&&b[k]!=l){
							dp[l][b[k]][j+1]+=dp[l][i][j];
						}
					}
				}
			}
		}
	}
	an=0;
	for(i=0;i<n;i++){
		an+=i*dp[1][n][i];
	}
	an1=an; an=0;
	printf("%lld\n",an1);
	for(l=2;l<n;l++){
		for(i=0;i<n;i++){
			an+=i*dp[l][n][i];
		}
		printf("%lld\n",an1-an);
		an=0;
	}
	printf("%lld\n",an1);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3720
final_rank: 1

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
long long dp[51][51][51],tol=0;
int main(void)
{
	long long n,m,i,j,k,u[1250],v[1250],cnt=1,l,tol2=0;
	scanf("%lld %lld",&n,&m);
	for(i=0;i<m;i++) scanf("%lld %lld",&u[i],&v[i]);
	for(i=1;i<=n;i++) dp[i][1][0]=1;
	for(l=1;l<n;l++){
		for(i=1;i<=n;i++){
			for(j=0;j<n;j++){
				if(dp[l][i][j]!=0){
					for(k=0;k<m;k++){
						if(u[k]==i){
							if(v[k]!=l) dp[l][v[k]][j+1]+=dp[l][i][j];
						}
					}
				}
			}
		}
	}
	for(i=1;i<n;i++){
		for(j=0;j<n;j++){
			tol+=dp[i][n][j]*j;
		}
		if(i==1){
			tol2=tol;
			printf("%lld\n",tol2);
		}
		else printf("%lld\n",tol2-tol);
		tol=0;
	}
	printf("%lld\n",tol2);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3772
final_rank: 3

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
long long dp[51][51][51],tol=0;
int main(void)
{
	long long n,m,i,j,k,u[1250],v[1250],cnt=1,l,tol2=0;
	scanf("%lld %lld",&n,&m);
	for(i=0;i<m;i++) scanf("%lld %lld",&u[i],&v[i]);
	for(i=1;i<=n;i++) dp[i][1][0]=1;
	for(l=1;l<n;l++){
		for(i=1;i<=n;i++){
			for(j=0;j<n;j++){
				if(dp[l][i][j]!=0){
					for(k=0;k<m;k++){
						if(u[k]==i){
							if(v[k]!=l)dp[l][v[k]][j+1]+=dp[l][i][j];
						}
					}
				}
			}
		}
	}
	for(i=1;i<n;i++){
		for(j=0;j<n;j++){
			tol+=dp[i][n][j]*j;
		}
		if(i==1){
			tol2=tol;
			printf("%d\n",tol2);
		}
		else printf("%d\n",tol2-tol);
		tol=0;
	}
	printf("%d\n",tol2);
	return 0;
}
accuracy: 0.84
time_ms: 10
memory_kb: 3712
final_rank: 7

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
long long dp[50][50][50],tol=0;
int main(void)
{
	long long n,m,i,j,k,u[1250],v[1250],cnt=1,l,tol2=0;
	scanf("%lld %lld",&n,&m);
	for(i=0;i<m;i++) scanf("%lld %lld",&u[i],&v[i]);
	for(i=1;i<=n;i++) dp[i][1][0]=1;
	for(l=1;l<n;l++){
		for(i=1;i<=n;i++){
			for(j=0;j<n;j++){
				if(dp[l][i][j]!=0){
					for(k=0;k<m;k++){
						if(u[k]==i){
							if(v[k]!=l)dp[l][v[k]][j+1]+=dp[l][i][j];
						}
					}
				}
			}
		}
	}
	for(i=1;i<n;i++){
		for(j=0;j<n;j++){
			tol+=dp[i][n][j]*j;
		}
		if(i==1){
			tol2=tol;
			printf("%d\n",tol2);
		}
		else printf("%d\n",tol2-tol);
		tol=0;
	}
	printf("%d\n",tol2);
	return 0;
}
accuracy: 0.84
time_ms: 10
memory_kb: 3668
final_rank: 6