Submission #871979

# Submission time Handle Problem Language Result Execution time Memory
871979 2023-11-12T05:32:45 Z Trisanu_Das Star Trek (CEOI20_startrek) C++17
100 / 100
58 ms 20316 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;
#define int ll
 
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> edges[N];
int dp[N], up[N], cnt[N], n;
int sub[N], rev[N], tot[N], tmp[N];
 
void dfs(int u, int p = -1){
	for(auto x : edges[u]){
		if(x == p) continue;
		dfs(x, u);
		if(dp[x] == 0){
			dp[u] = 1, 
			tmp[u] += sub[x], 
			cnt[u]++;
		} 
		
		tot[u] += sub[x];
	}
	
	if(cnt[u] > 1) sub[u] = 0;
	if(cnt[u] == 1) sub[u] = tmp[u];
	if(cnt[u] == 0) sub[u] = tot[u] + 1;
}
 
void re(int u, int p = -1){
	tot[u] += rev[u];
	if(up[u] == 0){
		tmp[u] += rev[u];
		dp[u] = 1, cnt[u]++;
	}
	if(cnt[u] > 1) sub[u] = 0;
	if(cnt[u] == 1) sub[u] = tmp[u];
	if(cnt[u] == 0) sub[u] = tot[u] + 1;
	
	for(auto x : edges[u]){
		if(x == p) continue;
		tot[u] -= sub[x];
		if(!dp[x]){
			cnt[u]--, tmp[u] -= sub[x];
		}
		
		if(cnt[u]) up[x] = 1;
		if(cnt[u] > 1) rev[x] = 0;
		if(cnt[u] == 1) rev[x] = tmp[u];
		if(cnt[u] == 0) rev[x] = tot[u] + 1;
		
		tot[u] += sub[x];
		if(!dp[x]){
			cnt[u]++, tmp[u] += sub[x];
		}
		
		re(x, u);
	}
}
 
int bp(int a, int b){
	int c=1;
	while(b){
		if(b&1) c = c * 1ll * a % mod;
		a = a * 1ll * a % mod, b >>= 1;
	} return c;
}
 
signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int d; cin>>n>>d;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	dfs(1);
	up[1] = 1;
	re(1);
	int E = 0, L = 0;
	for(int i=1;i<=n;i++){
		if(dp[i] == 1) E = (E + sub[i]) % mod;
		else E = (E - sub[i] + mod) % mod, L++;
	}
	
	int F = n * 1ll * n % mod;
	int res = (bp(F, d) - bp(E, d) + mod) % mod * 1ll * bp((F - E + mod) % mod, mod - 2) % mod;
	res = res * 1ll * L % mod;
	if(dp[1]){
		res = res * 1ll * sub[1] % mod;
	} else {
		res = (bp(F, d) - res * 1ll * sub[1] % mod + mod) % mod;
	}
	
	res = (bp(F, d) - res + mod) % mod;
	res = (res % mod + mod) % mod;
	cout<<res<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 1 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5976 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5976 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5976 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5976 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5976 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 36 ms 16208 KB Output is correct
13 Correct 45 ms 20316 KB Output is correct
14 Correct 33 ms 12844 KB Output is correct
15 Correct 32 ms 12884 KB Output is correct
16 Correct 34 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5976 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5976 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 1 ms 5980 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 1 ms 5980 KB Output is correct
16 Correct 1 ms 5980 KB Output is correct
17 Correct 1 ms 6048 KB Output is correct
18 Correct 1 ms 5980 KB Output is correct
19 Correct 1 ms 5980 KB Output is correct
20 Correct 1 ms 5980 KB Output is correct
21 Correct 1 ms 5980 KB Output is correct
22 Correct 2 ms 6132 KB Output is correct
23 Correct 2 ms 5980 KB Output is correct
24 Correct 2 ms 5980 KB Output is correct
25 Correct 2 ms 6052 KB Output is correct
26 Correct 2 ms 5980 KB Output is correct
27 Correct 2 ms 5980 KB Output is correct
28 Correct 1 ms 5980 KB Output is correct
29 Correct 2 ms 6060 KB Output is correct
30 Correct 2 ms 6132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 1 ms 5980 KB Output is correct
3 Correct 2 ms 5976 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5980 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5976 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 36 ms 16208 KB Output is correct
13 Correct 45 ms 20316 KB Output is correct
14 Correct 33 ms 12844 KB Output is correct
15 Correct 32 ms 12884 KB Output is correct
16 Correct 34 ms 12880 KB Output is correct
17 Correct 1 ms 5980 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 1 ms 5980 KB Output is correct
21 Correct 1 ms 5980 KB Output is correct
22 Correct 1 ms 6048 KB Output is correct
23 Correct 1 ms 5980 KB Output is correct
24 Correct 1 ms 5980 KB Output is correct
25 Correct 1 ms 5980 KB Output is correct
26 Correct 1 ms 5980 KB Output is correct
27 Correct 2 ms 6132 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 2 ms 6052 KB Output is correct
31 Correct 2 ms 5980 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 1 ms 5980 KB Output is correct
34 Correct 2 ms 6060 KB Output is correct
35 Correct 2 ms 6132 KB Output is correct
36 Correct 39 ms 16020 KB Output is correct
37 Correct 58 ms 20316 KB Output is correct
38 Correct 30 ms 12624 KB Output is correct
39 Correct 33 ms 12884 KB Output is correct
40 Correct 32 ms 12892 KB Output is correct
41 Correct 39 ms 18268 KB Output is correct
42 Correct 40 ms 19028 KB Output is correct
43 Correct 22 ms 11900 KB Output is correct
44 Correct 31 ms 12880 KB Output is correct
45 Correct 33 ms 12884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 1 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 1 ms 5980 KB Output is correct
8 Correct 1 ms 5980 KB Output is correct
9 Correct 1 ms 5980 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 1 ms 5980 KB Output is correct
12 Correct 1 ms 5980 KB Output is correct
13 Correct 1 ms 5980 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5976 KB Output is correct
17 Correct 2 ms 5976 KB Output is correct
18 Correct 1 ms 5980 KB Output is correct
19 Correct 36 ms 16208 KB Output is correct
20 Correct 45 ms 20316 KB Output is correct
21 Correct 33 ms 12844 KB Output is correct
22 Correct 32 ms 12884 KB Output is correct
23 Correct 34 ms 12880 KB Output is correct
24 Correct 1 ms 5980 KB Output is correct
25 Correct 2 ms 5980 KB Output is correct
26 Correct 2 ms 5980 KB Output is correct
27 Correct 1 ms 5980 KB Output is correct
28 Correct 1 ms 5980 KB Output is correct
29 Correct 1 ms 6048 KB Output is correct
30 Correct 1 ms 5980 KB Output is correct
31 Correct 1 ms 5980 KB Output is correct
32 Correct 1 ms 5980 KB Output is correct
33 Correct 1 ms 5980 KB Output is correct
34 Correct 2 ms 6132 KB Output is correct
35 Correct 2 ms 5980 KB Output is correct
36 Correct 2 ms 5980 KB Output is correct
37 Correct 2 ms 6052 KB Output is correct
38 Correct 2 ms 5980 KB Output is correct
39 Correct 2 ms 5980 KB Output is correct
40 Correct 1 ms 5980 KB Output is correct
41 Correct 2 ms 6060 KB Output is correct
42 Correct 2 ms 6132 KB Output is correct
43 Correct 39 ms 16020 KB Output is correct
44 Correct 58 ms 20316 KB Output is correct
45 Correct 30 ms 12624 KB Output is correct
46 Correct 33 ms 12884 KB Output is correct
47 Correct 32 ms 12892 KB Output is correct
48 Correct 39 ms 18268 KB Output is correct
49 Correct 40 ms 19028 KB Output is correct
50 Correct 22 ms 11900 KB Output is correct
51 Correct 31 ms 12880 KB Output is correct
52 Correct 33 ms 12884 KB Output is correct
53 Correct 43 ms 20308 KB Output is correct
54 Correct 58 ms 18856 KB Output is correct
55 Correct 21 ms 11464 KB Output is correct
56 Correct 35 ms 16212 KB Output is correct
57 Correct 31 ms 13100 KB Output is correct
58 Correct 32 ms 12928 KB Output is correct
59 Correct 31 ms 12892 KB Output is correct
60 Correct 33 ms 12660 KB Output is correct