답안 #597052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597052 2022-07-15T12:34:58 Z jiahng Star Trek (CEOI20_startrek) C++14
100 / 100
142 ms 57284 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;

        sm[i] -= dp[x];
        dp[x] = dpx; dp[i] = dpi;
        sm[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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 28500 KB Output is correct
2 Correct 16 ms 28552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 28500 KB Output is correct
2 Correct 14 ms 28456 KB Output is correct
3 Correct 13 ms 28536 KB Output is correct
4 Correct 14 ms 28500 KB Output is correct
5 Correct 16 ms 28548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28412 KB Output is correct
2 Correct 13 ms 28456 KB Output is correct
3 Correct 15 ms 28464 KB Output is correct
4 Correct 20 ms 28424 KB Output is correct
5 Correct 14 ms 28500 KB Output is correct
6 Correct 13 ms 28472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28412 KB Output is correct
2 Correct 13 ms 28456 KB Output is correct
3 Correct 15 ms 28464 KB Output is correct
4 Correct 20 ms 28424 KB Output is correct
5 Correct 14 ms 28500 KB Output is correct
6 Correct 13 ms 28472 KB Output is correct
7 Correct 16 ms 28676 KB Output is correct
8 Correct 17 ms 28776 KB Output is correct
9 Correct 13 ms 28628 KB Output is correct
10 Correct 14 ms 28636 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28412 KB Output is correct
2 Correct 13 ms 28456 KB Output is correct
3 Correct 15 ms 28464 KB Output is correct
4 Correct 20 ms 28424 KB Output is correct
5 Correct 14 ms 28500 KB Output is correct
6 Correct 13 ms 28472 KB Output is correct
7 Correct 16 ms 28676 KB Output is correct
8 Correct 17 ms 28776 KB Output is correct
9 Correct 13 ms 28628 KB Output is correct
10 Correct 14 ms 28636 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 105 ms 46168 KB Output is correct
13 Correct 142 ms 56120 KB Output is correct
14 Correct 105 ms 37704 KB Output is correct
15 Correct 108 ms 37736 KB Output is correct
16 Correct 126 ms 38056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28412 KB Output is correct
2 Correct 13 ms 28456 KB Output is correct
3 Correct 15 ms 28464 KB Output is correct
4 Correct 20 ms 28424 KB Output is correct
5 Correct 14 ms 28500 KB Output is correct
6 Correct 13 ms 28472 KB Output is correct
7 Correct 16 ms 28676 KB Output is correct
8 Correct 17 ms 28776 KB Output is correct
9 Correct 13 ms 28628 KB Output is correct
10 Correct 14 ms 28636 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 16 ms 28500 KB Output is correct
13 Correct 20 ms 28544 KB Output is correct
14 Correct 14 ms 28500 KB Output is correct
15 Correct 15 ms 28532 KB Output is correct
16 Correct 16 ms 28460 KB Output is correct
17 Correct 18 ms 28500 KB Output is correct
18 Correct 18 ms 28500 KB Output is correct
19 Correct 15 ms 28448 KB Output is correct
20 Correct 21 ms 28504 KB Output is correct
21 Correct 19 ms 28628 KB Output is correct
22 Correct 17 ms 28796 KB Output is correct
23 Correct 15 ms 28628 KB Output is correct
24 Correct 15 ms 28568 KB Output is correct
25 Correct 16 ms 28636 KB Output is correct
26 Correct 15 ms 28628 KB Output is correct
27 Correct 18 ms 28704 KB Output is correct
28 Correct 18 ms 28500 KB Output is correct
29 Correct 14 ms 28636 KB Output is correct
30 Correct 14 ms 28528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 28412 KB Output is correct
2 Correct 13 ms 28456 KB Output is correct
3 Correct 15 ms 28464 KB Output is correct
4 Correct 20 ms 28424 KB Output is correct
5 Correct 14 ms 28500 KB Output is correct
6 Correct 13 ms 28472 KB Output is correct
7 Correct 16 ms 28676 KB Output is correct
8 Correct 17 ms 28776 KB Output is correct
9 Correct 13 ms 28628 KB Output is correct
10 Correct 14 ms 28636 KB Output is correct
11 Correct 16 ms 28628 KB Output is correct
12 Correct 105 ms 46168 KB Output is correct
13 Correct 142 ms 56120 KB Output is correct
14 Correct 105 ms 37704 KB Output is correct
15 Correct 108 ms 37736 KB Output is correct
16 Correct 126 ms 38056 KB Output is correct
17 Correct 16 ms 28500 KB Output is correct
18 Correct 20 ms 28544 KB Output is correct
19 Correct 14 ms 28500 KB Output is correct
20 Correct 15 ms 28532 KB Output is correct
21 Correct 16 ms 28460 KB Output is correct
22 Correct 18 ms 28500 KB Output is correct
23 Correct 18 ms 28500 KB Output is correct
24 Correct 15 ms 28448 KB Output is correct
25 Correct 21 ms 28504 KB Output is correct
26 Correct 19 ms 28628 KB Output is correct
27 Correct 17 ms 28796 KB Output is correct
28 Correct 15 ms 28628 KB Output is correct
29 Correct 15 ms 28568 KB Output is correct
30 Correct 16 ms 28636 KB Output is correct
31 Correct 15 ms 28628 KB Output is correct
32 Correct 18 ms 28704 KB Output is correct
33 Correct 18 ms 28500 KB Output is correct
34 Correct 14 ms 28636 KB Output is correct
35 Correct 14 ms 28528 KB Output is correct
36 Correct 131 ms 47292 KB Output is correct
37 Correct 133 ms 57284 KB Output is correct
38 Correct 117 ms 38708 KB Output is correct
39 Correct 115 ms 38948 KB Output is correct
40 Correct 115 ms 39244 KB Output is correct
41 Correct 109 ms 52572 KB Output is correct
42 Correct 97 ms 54644 KB Output is correct
43 Correct 88 ms 37420 KB Output is correct
44 Correct 100 ms 39212 KB Output is correct
45 Correct 109 ms 38836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 28500 KB Output is correct
2 Correct 16 ms 28552 KB Output is correct
3 Correct 14 ms 28500 KB Output is correct
4 Correct 14 ms 28456 KB Output is correct
5 Correct 13 ms 28536 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 16 ms 28548 KB Output is correct
8 Correct 13 ms 28412 KB Output is correct
9 Correct 13 ms 28456 KB Output is correct
10 Correct 15 ms 28464 KB Output is correct
11 Correct 20 ms 28424 KB Output is correct
12 Correct 14 ms 28500 KB Output is correct
13 Correct 13 ms 28472 KB Output is correct
14 Correct 16 ms 28676 KB Output is correct
15 Correct 17 ms 28776 KB Output is correct
16 Correct 13 ms 28628 KB Output is correct
17 Correct 14 ms 28636 KB Output is correct
18 Correct 16 ms 28628 KB Output is correct
19 Correct 105 ms 46168 KB Output is correct
20 Correct 142 ms 56120 KB Output is correct
21 Correct 105 ms 37704 KB Output is correct
22 Correct 108 ms 37736 KB Output is correct
23 Correct 126 ms 38056 KB Output is correct
24 Correct 16 ms 28500 KB Output is correct
25 Correct 20 ms 28544 KB Output is correct
26 Correct 14 ms 28500 KB Output is correct
27 Correct 15 ms 28532 KB Output is correct
28 Correct 16 ms 28460 KB Output is correct
29 Correct 18 ms 28500 KB Output is correct
30 Correct 18 ms 28500 KB Output is correct
31 Correct 15 ms 28448 KB Output is correct
32 Correct 21 ms 28504 KB Output is correct
33 Correct 19 ms 28628 KB Output is correct
34 Correct 17 ms 28796 KB Output is correct
35 Correct 15 ms 28628 KB Output is correct
36 Correct 15 ms 28568 KB Output is correct
37 Correct 16 ms 28636 KB Output is correct
38 Correct 15 ms 28628 KB Output is correct
39 Correct 18 ms 28704 KB Output is correct
40 Correct 18 ms 28500 KB Output is correct
41 Correct 14 ms 28636 KB Output is correct
42 Correct 14 ms 28528 KB Output is correct
43 Correct 131 ms 47292 KB Output is correct
44 Correct 133 ms 57284 KB Output is correct
45 Correct 117 ms 38708 KB Output is correct
46 Correct 115 ms 38948 KB Output is correct
47 Correct 115 ms 39244 KB Output is correct
48 Correct 109 ms 52572 KB Output is correct
49 Correct 97 ms 54644 KB Output is correct
50 Correct 88 ms 37420 KB Output is correct
51 Correct 100 ms 39212 KB Output is correct
52 Correct 109 ms 38836 KB Output is correct
53 Correct 125 ms 57232 KB Output is correct
54 Correct 134 ms 53552 KB Output is correct
55 Correct 65 ms 36564 KB Output is correct
56 Correct 113 ms 47576 KB Output is correct
57 Correct 111 ms 39492 KB Output is correct
58 Correct 107 ms 39516 KB Output is correct
59 Correct 101 ms 38812 KB Output is correct
60 Correct 120 ms 38864 KB Output is correct