Submission #597049

# Submission time Handle Problem Language Result Execution time Memory
597049 2022-07-15T12:28:53 Z jiahng Star Trek (CEOI20_startrek) C++14
45 / 100
113 ms 56904 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];
        
        sm[i] += dp[x];

        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);

        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 14 ms 28500 KB Output is correct
2 Incorrect 14 ms 28604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28472 KB Output is correct
2 Correct 13 ms 28440 KB Output is correct
3 Correct 16 ms 28512 KB Output is correct
4 Correct 13 ms 28528 KB Output is correct
5 Correct 13 ms 28456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28460 KB Output is correct
2 Correct 13 ms 28500 KB Output is correct
3 Correct 14 ms 28460 KB Output is correct
4 Correct 13 ms 28500 KB Output is correct
5 Correct 14 ms 28524 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28460 KB Output is correct
2 Correct 13 ms 28500 KB Output is correct
3 Correct 14 ms 28460 KB Output is correct
4 Correct 13 ms 28500 KB Output is correct
5 Correct 14 ms 28524 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28756 KB Output is correct
8 Correct 13 ms 28704 KB Output is correct
9 Correct 15 ms 28656 KB Output is correct
10 Correct 13 ms 28628 KB Output is correct
11 Correct 16 ms 28664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28460 KB Output is correct
2 Correct 13 ms 28500 KB Output is correct
3 Correct 14 ms 28460 KB Output is correct
4 Correct 13 ms 28500 KB Output is correct
5 Correct 14 ms 28524 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28756 KB Output is correct
8 Correct 13 ms 28704 KB Output is correct
9 Correct 15 ms 28656 KB Output is correct
10 Correct 13 ms 28628 KB Output is correct
11 Correct 16 ms 28664 KB Output is correct
12 Correct 104 ms 47612 KB Output is correct
13 Correct 113 ms 56904 KB Output is correct
14 Correct 97 ms 40008 KB Output is correct
15 Correct 102 ms 41184 KB Output is correct
16 Correct 96 ms 41296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28460 KB Output is correct
2 Correct 13 ms 28500 KB Output is correct
3 Correct 14 ms 28460 KB Output is correct
4 Correct 13 ms 28500 KB Output is correct
5 Correct 14 ms 28524 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28756 KB Output is correct
8 Correct 13 ms 28704 KB Output is correct
9 Correct 15 ms 28656 KB Output is correct
10 Correct 13 ms 28628 KB Output is correct
11 Correct 16 ms 28664 KB Output is correct
12 Correct 13 ms 28500 KB Output is correct
13 Incorrect 17 ms 28628 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28460 KB Output is correct
2 Correct 13 ms 28500 KB Output is correct
3 Correct 14 ms 28460 KB Output is correct
4 Correct 13 ms 28500 KB Output is correct
5 Correct 14 ms 28524 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28756 KB Output is correct
8 Correct 13 ms 28704 KB Output is correct
9 Correct 15 ms 28656 KB Output is correct
10 Correct 13 ms 28628 KB Output is correct
11 Correct 16 ms 28664 KB Output is correct
12 Correct 104 ms 47612 KB Output is correct
13 Correct 113 ms 56904 KB Output is correct
14 Correct 97 ms 40008 KB Output is correct
15 Correct 102 ms 41184 KB Output is correct
16 Correct 96 ms 41296 KB Output is correct
17 Correct 13 ms 28500 KB Output is correct
18 Incorrect 17 ms 28628 KB Output isn't correct
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28500 KB Output is correct
2 Incorrect 14 ms 28604 KB Output isn't correct
3 Halted 0 ms 0 KB -