Submission #755217

# Submission time Handle Problem Language Result Execution time Memory
755217 2023-06-09T15:21:21 Z amirhoseinfar1385 Star Trek (CEOI20_startrek) C++17
65 / 100
1000 ms 14828 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=100000+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 2 ms 2644 KB Output is correct
2 Correct 26 ms 2736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 81 ms 11468 KB Output is correct
13 Correct 87 ms 14828 KB Output is correct
14 Correct 44 ms 8640 KB Output is correct
15 Correct 82 ms 8820 KB Output is correct
16 Correct 66 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 28 ms 2736 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 30 ms 2744 KB Output is correct
27 Correct 40 ms 2824 KB Output is correct
28 Correct 13 ms 2644 KB Output is correct
29 Correct 31 ms 2736 KB Output is correct
30 Correct 29 ms 2744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 81 ms 11468 KB Output is correct
13 Correct 87 ms 14828 KB Output is correct
14 Correct 44 ms 8640 KB Output is correct
15 Correct 82 ms 8820 KB Output is correct
16 Correct 66 ms 8960 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 28 ms 2736 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 30 ms 2744 KB Output is correct
32 Correct 40 ms 2824 KB Output is correct
33 Correct 13 ms 2644 KB Output is correct
34 Correct 31 ms 2736 KB Output is correct
35 Correct 29 ms 2744 KB Output is correct
36 Correct 70 ms 11436 KB Output is correct
37 Correct 63 ms 14724 KB Output is correct
38 Correct 51 ms 8688 KB Output is correct
39 Correct 78 ms 8840 KB Output is correct
40 Correct 74 ms 9036 KB Output is correct
41 Execution timed out 1086 ms 12396 KB Time limit exceeded
42 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 26 ms 2736 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 81 ms 11468 KB Output is correct
20 Correct 87 ms 14828 KB Output is correct
21 Correct 44 ms 8640 KB Output is correct
22 Correct 82 ms 8820 KB Output is correct
23 Correct 66 ms 8960 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 28 ms 2736 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 30 ms 2744 KB Output is correct
39 Correct 40 ms 2824 KB Output is correct
40 Correct 13 ms 2644 KB Output is correct
41 Correct 31 ms 2736 KB Output is correct
42 Correct 29 ms 2744 KB Output is correct
43 Correct 70 ms 11436 KB Output is correct
44 Correct 63 ms 14724 KB Output is correct
45 Correct 51 ms 8688 KB Output is correct
46 Correct 78 ms 8840 KB Output is correct
47 Correct 74 ms 9036 KB Output is correct
48 Execution timed out 1086 ms 12396 KB Time limit exceeded
49 Halted 0 ms 0 KB -