답안 #390174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390174 2021-04-15T13:44:29 Z talant117408 Star Trek (CEOI20_startrek) C++17
65 / 100
89 ms 16684 KB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 1e9+7;
 
ll mode(ll a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 1e5+7;
int state[N], l, w, state_as_root[N];
int losing_children[N], critical[N], depth[N], par[N];
ll dp[N];
vector <int> graph[N];

bool dfs(int v, int p) {
	par[v] = p;
	depth[v] = depth[p]+1;
	int cnt = 0, children = 0;
	for (auto to : graph[v]) {
		if (to == p) continue;
		children++;
		dfs(to, v);
		cnt += (state[to] ? 1 : 0);
		if (state[to]) losing_children[v]++;
	}
	if (children == 0) state[v] = 1;
	else if (cnt) state[v] = 0;
	else state[v] = 1;
	return state[v];
}

void dfs2(int v, int p, int origin, int cnt = 0) {
	if (cnt == depth[v] && state[v]) critical[origin]++;
	if (state[v]) {
		for (auto to : graph[v]) {
			if (to == p) continue;
			dfs2(to, v, origin, cnt+1);
		}
	}
	else {
		for (auto to : graph[v]) {
			if (to == p) continue;
			if (state[to]) {
				dfs2(to, v, origin, (losing_children[v] == 1 ? cnt+1 : 0));
			}
		}
	}
}

void dfs3(int v, int p) {
	state_as_root[v] = state[v];
	if (state[v]) l++;
	else w++;
	for (auto to : graph[v]) {
		if (to == p) continue;
		if (state[v] == false && state[to] == true) {
			if (losing_children[v] == 1 && (state_as_root[par[v]] == false)) {
				losing_children[v]--;
				losing_children[to]++;
				state[v] = 1;
				state[to] = 0;
				dfs3(to, v);
				losing_children[v]++;
				losing_children[to]--;
				state[v] = 0;
				state[to] = 1;
			}
			else {
				dfs3(to, v);
			}
		}
		else {
			dfs3(to, v);
		}
	}
}

int main() {
	do_not_disturb
	
	int n;
	ll d;
	cin >> n >> d;
	for (int i = 0; i < n-1; i++) {
		int x, y;
		cin >> x >> y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	
	if (n == 2) {
		cout << binpow(2, d*2);
	}
	else if (d <= 1e5 && n <= 1000) {
		depth[1] = -1;
		dfs(1, 1);
		dfs3(1, 1);
		for (int i = 1; i <= n; i++) {
			depth[i] = -1;
			for (int j = 1; j <= n; j++) losing_children[j] = 0;
			dfs(i, i);
			dfs2(i, i, i);
		}
		dp[d] = l;
		ll E = 0;
		for (int i = 1; i <= n; i++) {
			if (state_as_root[i]) {
				E = subt(E, critical[i]);
			}
			else {
				E = add(E, critical[i]);
			}
		}
		
		for (int i = d-1; i >= 0; i--) {
			dp[i] = add(mult(l, binpow(n, 2*(d-i))), mult(E, dp[i+1]));
		}
		cout << (state_as_root[1] ? mult(critical[1], dp[1]) : subt(binpow(n, 2*d), mult(critical[1], dp[1])));
	}
	else if (d == 1) {
		depth[1] = -1;
		dfs(1, 1);
		dfs2(1, 1, 1);
		dfs3(1, 1);
		if (state_as_root[1]) cout << mult(critical[1], l);
		else cout << add(mult(n, w), mult(subt(n, critical[1]), l));
	}
	
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 27 ms 2744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 32 ms 2764 KB Output is correct
8 Correct 39 ms 2764 KB Output is correct
9 Correct 29 ms 2636 KB Output is correct
10 Correct 21 ms 2756 KB Output is correct
11 Correct 38 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 32 ms 2764 KB Output is correct
8 Correct 39 ms 2764 KB Output is correct
9 Correct 29 ms 2636 KB Output is correct
10 Correct 21 ms 2756 KB Output is correct
11 Correct 38 ms 2636 KB Output is correct
12 Correct 72 ms 11332 KB Output is correct
13 Correct 87 ms 15680 KB Output is correct
14 Correct 57 ms 7952 KB Output is correct
15 Correct 71 ms 7776 KB Output is correct
16 Correct 71 ms 7708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 32 ms 2764 KB Output is correct
8 Correct 39 ms 2764 KB Output is correct
9 Correct 29 ms 2636 KB Output is correct
10 Correct 21 ms 2756 KB Output is correct
11 Correct 38 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 26 ms 2736 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2680 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 33 ms 2792 KB Output is correct
22 Correct 42 ms 2816 KB Output is correct
23 Correct 30 ms 2748 KB Output is correct
24 Correct 23 ms 2764 KB Output is correct
25 Correct 39 ms 2764 KB Output is correct
26 Correct 38 ms 3428 KB Output is correct
27 Correct 54 ms 3472 KB Output is correct
28 Correct 37 ms 3528 KB Output is correct
29 Correct 37 ms 3392 KB Output is correct
30 Correct 46 ms 3140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 32 ms 2764 KB Output is correct
8 Correct 39 ms 2764 KB Output is correct
9 Correct 29 ms 2636 KB Output is correct
10 Correct 21 ms 2756 KB Output is correct
11 Correct 38 ms 2636 KB Output is correct
12 Correct 72 ms 11332 KB Output is correct
13 Correct 87 ms 15680 KB Output is correct
14 Correct 57 ms 7952 KB Output is correct
15 Correct 71 ms 7776 KB Output is correct
16 Correct 71 ms 7708 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 26 ms 2736 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2680 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 33 ms 2792 KB Output is correct
27 Correct 42 ms 2816 KB Output is correct
28 Correct 30 ms 2748 KB Output is correct
29 Correct 23 ms 2764 KB Output is correct
30 Correct 39 ms 2764 KB Output is correct
31 Correct 38 ms 3428 KB Output is correct
32 Correct 54 ms 3472 KB Output is correct
33 Correct 37 ms 3528 KB Output is correct
34 Correct 37 ms 3392 KB Output is correct
35 Correct 46 ms 3140 KB Output is correct
36 Correct 79 ms 12448 KB Output is correct
37 Correct 89 ms 16684 KB Output is correct
38 Correct 56 ms 8840 KB Output is correct
39 Correct 77 ms 9024 KB Output is correct
40 Correct 73 ms 8900 KB Output is correct
41 Incorrect 40 ms 6968 KB Output isn't correct
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Correct 27 ms 2744 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 32 ms 2764 KB Output is correct
15 Correct 39 ms 2764 KB Output is correct
16 Correct 29 ms 2636 KB Output is correct
17 Correct 21 ms 2756 KB Output is correct
18 Correct 38 ms 2636 KB Output is correct
19 Correct 72 ms 11332 KB Output is correct
20 Correct 87 ms 15680 KB Output is correct
21 Correct 57 ms 7952 KB Output is correct
22 Correct 71 ms 7776 KB Output is correct
23 Correct 71 ms 7708 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 26 ms 2736 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2680 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 33 ms 2792 KB Output is correct
34 Correct 42 ms 2816 KB Output is correct
35 Correct 30 ms 2748 KB Output is correct
36 Correct 23 ms 2764 KB Output is correct
37 Correct 39 ms 2764 KB Output is correct
38 Correct 38 ms 3428 KB Output is correct
39 Correct 54 ms 3472 KB Output is correct
40 Correct 37 ms 3528 KB Output is correct
41 Correct 37 ms 3392 KB Output is correct
42 Correct 46 ms 3140 KB Output is correct
43 Correct 79 ms 12448 KB Output is correct
44 Correct 89 ms 16684 KB Output is correct
45 Correct 56 ms 8840 KB Output is correct
46 Correct 77 ms 9024 KB Output is correct
47 Correct 73 ms 8900 KB Output is correct
48 Incorrect 40 ms 6968 KB Output isn't correct
49 Halted 0 ms 0 KB -