Submission #597051

# Submission time Handle Problem Language Result Execution time Memory
597051 2022-07-15T12:33:56 Z jiahng Star Trek (CEOI20_startrek) C++14
45 / 100
126 ms 56052 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];
set <int> st[maxn];
int sm[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]++; st[x].insert(i);state[x] = 1;
        }
        sm[x] += dp[i];
    }
    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]) st[i].insert(x);

        if (!state[i]) st[x].erase(i);
        if (!state[x]) num[i]++;
        state[i] = (num[i] > 0);
        
        sm[x] -= dp[i];
        dp[x] = 0;
        if (num[x] == 1) dp[x] = dp[*st[x].begin()];
        else if (num[x] == 0) dp[x] = sm[x];
        if (!state[x]) dp[x]++;

        sm[i] += dp[x];

        dp[i] = 0;
        if (num[i] == 1) dp[i] = dp[*st[i].begin()];
        else if (num[i] == 0) dp[i] = sm[i];

        if (!state[i]) dp[i]++;

        reroot(i,x);

        if (!state[x]) st[i].erase(x);
        state[x] = statex; state[i] = statei;
        if (!state[i]) st[x].insert(i);
        num[x] = numx; num[i] = numi;

        dp[i] -= dp[x];
        dp[x] = dpx; dp[i] = dpi;
        dp[x] += dp[i];
    }
}

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 21 ms 28492 KB Output is correct
2 Incorrect 14 ms 28628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 13 ms 28500 KB Output is correct
4 Correct 14 ms 28524 KB Output is correct
5 Correct 16 ms 28500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28508 KB Output is correct
2 Correct 13 ms 28492 KB Output is correct
3 Correct 15 ms 28556 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 17 ms 28500 KB Output is correct
6 Correct 15 ms 28444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28508 KB Output is correct
2 Correct 13 ms 28492 KB Output is correct
3 Correct 15 ms 28556 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 17 ms 28500 KB Output is correct
6 Correct 15 ms 28444 KB Output is correct
7 Correct 15 ms 28628 KB Output is correct
8 Correct 14 ms 28756 KB Output is correct
9 Correct 14 ms 28628 KB Output is correct
10 Correct 14 ms 28576 KB Output is correct
11 Correct 16 ms 28640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28508 KB Output is correct
2 Correct 13 ms 28492 KB Output is correct
3 Correct 15 ms 28556 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 17 ms 28500 KB Output is correct
6 Correct 15 ms 28444 KB Output is correct
7 Correct 15 ms 28628 KB Output is correct
8 Correct 14 ms 28756 KB Output is correct
9 Correct 14 ms 28628 KB Output is correct
10 Correct 14 ms 28576 KB Output is correct
11 Correct 16 ms 28640 KB Output is correct
12 Correct 121 ms 46132 KB Output is correct
13 Correct 126 ms 56052 KB Output is correct
14 Correct 87 ms 37728 KB Output is correct
15 Correct 91 ms 37776 KB Output is correct
16 Correct 94 ms 38016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28508 KB Output is correct
2 Correct 13 ms 28492 KB Output is correct
3 Correct 15 ms 28556 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 17 ms 28500 KB Output is correct
6 Correct 15 ms 28444 KB Output is correct
7 Correct 15 ms 28628 KB Output is correct
8 Correct 14 ms 28756 KB Output is correct
9 Correct 14 ms 28628 KB Output is correct
10 Correct 14 ms 28576 KB Output is correct
11 Correct 16 ms 28640 KB Output is correct
12 Correct 17 ms 28588 KB Output is correct
13 Incorrect 16 ms 28600 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28508 KB Output is correct
2 Correct 13 ms 28492 KB Output is correct
3 Correct 15 ms 28556 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 17 ms 28500 KB Output is correct
6 Correct 15 ms 28444 KB Output is correct
7 Correct 15 ms 28628 KB Output is correct
8 Correct 14 ms 28756 KB Output is correct
9 Correct 14 ms 28628 KB Output is correct
10 Correct 14 ms 28576 KB Output is correct
11 Correct 16 ms 28640 KB Output is correct
12 Correct 121 ms 46132 KB Output is correct
13 Correct 126 ms 56052 KB Output is correct
14 Correct 87 ms 37728 KB Output is correct
15 Correct 91 ms 37776 KB Output is correct
16 Correct 94 ms 38016 KB Output is correct
17 Correct 17 ms 28588 KB Output is correct
18 Incorrect 16 ms 28600 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 28492 KB Output is correct
2 Incorrect 14 ms 28628 KB Output isn't correct
3 Halted 0 ms 0 KB -