Submission #597044

# Submission time Handle Problem Language Result Execution time Memory
597044 2022-07-15T12:19:41 Z jiahng Star Trek (CEOI20_startrek) C++14
50 / 100
1000 ms 28992 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 400010
#define INF (ll)(1e9+10)
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;

int N,D,a,b;
vi adj[maxn];
int qexp(int a,int b){
	int ans = 1;
	while (b){
		//~ cout << a << ' ' << b << ' ' << ans << '\n';
		if (b & 1) (ans *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return ans;
}
bool state[maxn];
int dp[maxn], num[maxn];
void dfs(int x,int p){
    state[x] = 0; dp[x] = num[x] = 0;
    aFOR(i,adj[x]) if (i != p){
        dfs(i,x);
        if (!state[i]){
            num[x]++; state[x] = 1;
        }
    }
    if (num[x] == 1){
        aFOR(i, adj[x]) if (i != p && !state[i]) dp[x] = dp[i];
    }else if (num[x] == 0){
        aFOR(i,adj[x]) if (i != p) dp[x] += dp[i];
    }
    if (!state[x]) dp[x]++;
}
bool win[maxn];
int crit[maxn];
void reroot(int x,int p){
    crit[x] = dp[x];
    win[x] = state[x];
    aFOR(i,adj[x]) if (i != p){
        int statex = state[x], statei = state[i], numx = num[x], numi = num[i], dpx = dp[x], dpi = dp[i];
        if (!state[i]) num[x]--;
        state[x] = (num[x] > 0);
        if (!state[x]) num[i]++;
        state[i] = (num[i] > 0);
        
        dp[x] = 0;
        if (num[x] == 1){
            aFOR(j, adj[x]) if (j != i && !state[j]) dp[x] = dp[j];
        }else if (num[x] == 0){
            aFOR(j,adj[x]) if (j != i) dp[x] += dp[j];
        }
        if (!state[x]) dp[x]++;
        dp[i] = 0;
        if (num[i] == 1){
            aFOR(j, adj[i]) if (!state[j]) dp[i] = dp[j];
        }else if (num[i] == 0){
            aFOR(j,adj[i]) dp[i] += dp[j];
        }
        if (!state[i]) dp[i]++;
        reroot(i,x);

        state[x] = statex; state[i] = statei;
        num[x] = numx; num[i] = numi;
        dp[x] = dpx; dp[i] = dpi;
    }
}

int32_t main(){
    fast;
    cin >> N >> D;
    FOR(i,1,N-1){
        cin >> a >> b; adj[a].pb(b); adj[b].pb(a);
    }
    dfs(1,-1);
    reroot(1,-1);

    int E = 0;
    FOR(i,1,N){
        if (win[i]) E += crit[i];
        else E -= crit[i];
        //cout << crit[i] << ' ';
    }
    //cout << '\n';
    int L = 0;
    FOR(i,1,N) if (!win[i]) L++;

    //cout << L << ' ' << E << '\n';
    D--;
    int invN = qexp(N, MOD - 2);
    int EN2 = (E * ((invN * invN) % MOD)) % MOD;
    int N2D = qexp(N, 2*D);
    int LD1 = (((((L * N2D) % MOD) * (qexp(EN2, D) - 1)) % MOD) * qexp(EN2 - 1, MOD - 2)) % MOD;
    (LD1 += (qexp(E, D) * L) % MOD) %= MOD;
    D++;
    //cout << LD1 << ' ';
    int ans;
    if (win[1]) ans = (crit[1] * LD1) % MOD;
    else ans = qexp(N, 2*D) - (crit[1] * LD1) % MOD;
    ans = qexp(N, 2*D) - ans;
    ans %= MOD; ans += MOD; ans %= MOD;

    cout << ans;
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 8 ms 9824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9664 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9724 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9884 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 8 ms 9736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9724 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9884 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 8 ms 9736 KB Output is correct
12 Correct 63 ms 22340 KB Output is correct
13 Correct 75 ms 28992 KB Output is correct
14 Execution timed out 1091 ms 16780 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9724 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9884 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 8 ms 9736 KB Output is correct
12 Correct 5 ms 9728 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9724 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9740 KB Output is correct
21 Correct 7 ms 9812 KB Output is correct
22 Correct 6 ms 9884 KB Output is correct
23 Correct 6 ms 9724 KB Output is correct
24 Correct 6 ms 9812 KB Output is correct
25 Correct 5 ms 9812 KB Output is correct
26 Correct 5 ms 9812 KB Output is correct
27 Correct 5 ms 9856 KB Output is correct
28 Correct 6 ms 9728 KB Output is correct
29 Correct 5 ms 9812 KB Output is correct
30 Correct 5 ms 9764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9724 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9884 KB Output is correct
9 Correct 6 ms 9812 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 8 ms 9736 KB Output is correct
12 Correct 63 ms 22340 KB Output is correct
13 Correct 75 ms 28992 KB Output is correct
14 Execution timed out 1091 ms 16780 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 8 ms 9824 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9664 KB Output is correct
5 Correct 6 ms 9728 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9724 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 7 ms 9884 KB Output is correct
16 Correct 6 ms 9812 KB Output is correct
17 Correct 6 ms 9732 KB Output is correct
18 Correct 8 ms 9736 KB Output is correct
19 Correct 63 ms 22340 KB Output is correct
20 Correct 75 ms 28992 KB Output is correct
21 Execution timed out 1091 ms 16780 KB Time limit exceeded
22 Halted 0 ms 0 KB -