Submission #755216

# Submission time Handle Problem Language Result Execution time Memory
755216 2023-06-09T15:20:20 Z amirhoseinfar1385 Star Trek (CEOI20_startrek) C++17
50 / 100
46 ms 588 KB
#include<bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long mypow(long long m,long long y){
	if(y==0){
		return 1;
	}
	long long p=mypow(m,(y>>1));
	p*=p;
	p%=mod;
	if(y&1){
		p*=m;
		p%=mod;
	}
	return p;
}
long long n,d;
const long long maxn=1000+10;
vector<long long>adj[maxn];
long long cnttagh,cntnafaraval,cntnafardovom,dp[maxn],tagh[maxn];
vector<vector<long long>>mat(2,vector<long long>(2)),mata(2,vector<long long>(1)),res;

void zarb(vector<vector<long long>>a,vector<vector<long long>>b){
	long long n1=a.size(),m1=a.back().size(),n2=b.size(),m2=b.back().size();
	res.clear();
	if(m1!=n2){
		return ;
	}
	res.resize(n1,vector<long long>(m2));
	for(long long i=0;i<n1;i++){
		for(long long j=0;j<m2;j++){
			for(long long h=0;h<m1;h++){
				res[i][j]+=a[i][h]*b[h][j];
				res[i][j]%=mod;
			}
		}
	}
}

void tav(vector<vector<long long>>&a,long long b){
	vector<vector<long long>>fake=a;
	b--;
	while(b>0){
		if(b&1){
			zarb(fake,a);
			a=res;
		}
		zarb(fake,fake);
		fake=res;
		b>>=1;
	}
}

void aval(long long u,long long para=0){
	for(auto x:adj[u]){
		if(x!=para){
			aval(x,u);
			if(dp[x]==0){
				dp[u]++;
			}
		}
	}
}

void caltagh(long long u,long long para=0){
	if(para==0){
		tagh[u]=1;
	}
	else if(tagh[para]==1){
		if(dp[para]==0){
			tagh[u]=1;
		}
		else if(dp[para]==1&&dp[u]==0){
			tagh[u]=1;
		}
	}
	if(tagh[u]==1&&dp[u]==0){
		cnttagh++;
	}
	for(auto x:adj[u]){
		if(x==para){
			continue;
		}
		caltagh(x,u);
	}
}

void caldp(long long u,long long para=0,long long cn=1){
	if(cn==0){
		dp[u]++;
	}
	if(dp[u]==0){
		if(u<=n)
			cntnafardovom++;
	}
	else{
		if(u<=n)
			cntnafaraval++;
	}
	for(auto x:adj[u]){
		if(x!=para){
			if(dp[x]==0){
				caldp(x,u,dp[u]-1);
			}
			else{
				caldp(x,u,dp[u]);
			}
		}
	}
}

void clear(){
	for(long long i=1;i<=n+2;i++){
		dp[i]=0;
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>d;
	for(long long i=0;i<n-1;i++){
		long long u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	aval(1);
	caltagh(1);
	caldp(1);
	long long z=dp[1];
	mata[0][0]=cntnafaraval;
	mata[1][0]=cntnafardovom;
	mat[0][0]=cntnafaraval*n;
	mat[1][0]=cntnafardovom*n;
	if(d==1){
		long long res=0;
		if(dp[1]>=1){
			res=1ll*(n-cnttagh)*n%mod;
			res+=1ll*cnttagh*cntnafaraval%mod;
			res%=mod;
			cout<<res<<"\n";
		}
		else{
			res=1ll*cnttagh*cntnafardovom%mod;
			cout<<res<<"\n";
		}
		return 0;
	}
	cntnafaraval=0;
	cntnafardovom=0;
	for(long long i=1;i<=n;i++){
		clear();
		adj[i].push_back(n+1);
		aval(1);
		caldp(1);
		adj[i].pop_back();
	}
	mat[0][1]=cntnafaraval;
	mat[1][1]=cntnafardovom;
	cntnafaraval=0;
	cntnafardovom=0;
	tav(mat,d-1);
	zarb(mat,mata);
	cntnafaraval=res[0][0];
	cntnafardovom=res[1][0];
	long long sm=cntnafaraval+cntnafardovom;
	long long res=0;
	if(z>=1){
		res=1ll*(n-cnttagh)*(sm)%mod;
		res+=1ll*cnttagh*cntnafaraval%mod;
		res%=mod;
		cout<<res<<"\n";
	}
	else{
		res=1ll*cnttagh*cntnafardovom%mod;
		cout<<res<<"\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 25 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Runtime error 1 ms 468 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 26 ms 404 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 352 KB Output is correct
26 Correct 29 ms 432 KB Output is correct
27 Correct 46 ms 588 KB Output is correct
28 Correct 12 ms 340 KB Output is correct
29 Correct 30 ms 396 KB Output is correct
30 Correct 29 ms 420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 352 KB Output is correct
12 Runtime error 1 ms 468 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 25 ms 352 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Runtime error 1 ms 468 KB Execution killed with signal 11
20 Halted 0 ms 0 KB -