답안 #1091242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091242 2024-09-20T08:26:08 Z mispertion Star Trek (CEOI20_startrek) C++17
100 / 100
64 ms 42320 KB
#include<bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"

const ld PI = 3.1415926535;
const int N = 1e6 + 6;
const int M = 7e6 + 1;
const int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 31;

int mult(int a, int b) {
    return a * 1LL * b % mod;
}

int sum(int a, int b) { 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}

ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}

int n, d, dp[N], ndp[N], cd[N], cu[N], C[N], st[N], ddp[N];
vector<int> g[N];

void dfs(int v, int p){
    int cnt0 = 0;
    for(auto u : g[v]){
        if(u == p) continue;
        dfs(u, v);
        if(dp[u] == 0)
            cnt0++;
    }
    dp[v] = (cnt0 != 0);
    if(dp[v] == 0){
        cd[v] = 1;
        for(auto u : g[v]){
            if(u == p) continue;
            cd[v] += cd[u];
        }
    }else{
        cd[v] = 0;
        if(cnt0 == 1){
            for(auto u : g[v]){
                if(u == p) continue;
                if(dp[u] == 0)
                    cd[v] += cd[u];
            }
        }
    }
}

void dfs1(int v, int p){
    int cnt0 = (ndp[v] == 0), sm0 = 0, sm = cu[v];
    if(ndp[v] == 0){
        sm0 += cu[v];
    }
    for(auto u : g[v]){
        if(u == p) continue;
        cnt0 += (dp[u] == 0);
        sm += cd[u];
        if(dp[u] == 0)
            sm0 += cd[u];
    }
    for(auto u : g[v]){
        if(u == p) continue;
        cnt0 -= (dp[u] == 0);
        sm -= cd[u];
        if(dp[u] == 0)
            sm0 -= cd[u];
        ndp[u] = (cnt0 != 0);
        if(ndp[u] == 0){
            cu[u] = 1 + sm;
        }else{
            if(cnt0 == 1)
                cu[u] = sm0;
        }
        dfs1(u, v);
        cnt0 += (dp[u] == 0);
        sm += cd[u];
        if(dp[u] == 0)
            sm0 += cd[u];
    }
}

void dfs2(int v, int p){
    int cnt0 = (ndp[v] == 0);
    for(auto u : g[v]){
        if(u == p) continue;
        cnt0 += (dp[u] == 0);
    }
    st[v] = (cnt0 != 0);
    if(st[v] == 0){
        C[v] = 1 + cu[v];
        for(auto u : g[v]){
            if(u == p) continue;
            C[v] += cd[u];
        }
    }else{
        if(cnt0 == 1){
            if(ndp[v] == 0)
                C[v] = cu[v];
            for(auto u : g[v]){
                if(u == p) continue;
                if(dp[u] == 0)
                    C[v] += cd[u];
            }
        }
    }
    for(auto u : g[v])
        if(u != p) 
            dfs2(u, v);
}

void solve(){
    cin >> n >> d;
    for(int i = 2; i <= n; i++){
        int u, v;
        cin >> u >> v;
        g[v].pb(u);
        g[u].pb(v);
    }
    dfs(1, 0);
    ndp[1] = 1;
    cu[1] = 0;
    dfs1(1, 0);
    dfs2(1, 0);
    int L = 0;
    for(int i = 1; i <= n; i++)
        L += (st[i] == 0);
    int E = 0;
    for(int i = 1; i <= n; i++){
        if(st[i])
            E += C[i];
        else
            E -= C[i];
    }
    int n2 = mult(n, n);
    int lst = mult(L, mult(sum(binpow(n2, d), -binpow(E, d)), binpow(sum(n2, -E), mod - 2)));
    int ret;
    if(st[1]){
        ret = sum(binpow(n2, d), -mult(C[1], lst));
    }else{
        ret = mult(C[1], lst);
    }
    cout << ret << '\n';
}   

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 13 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23896 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 11 ms 23900 KB Output is correct
5 Correct 13 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23868 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23868 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23868 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24096 KB Output is correct
12 Correct 59 ms 37204 KB Output is correct
13 Correct 64 ms 42320 KB Output is correct
14 Correct 43 ms 33148 KB Output is correct
15 Correct 54 ms 33108 KB Output is correct
16 Correct 50 ms 33256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23868 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24096 KB Output is correct
12 Correct 11 ms 23900 KB Output is correct
13 Correct 11 ms 23900 KB Output is correct
14 Correct 11 ms 23900 KB Output is correct
15 Correct 10 ms 23896 KB Output is correct
16 Correct 11 ms 23900 KB Output is correct
17 Correct 11 ms 23964 KB Output is correct
18 Correct 11 ms 23956 KB Output is correct
19 Correct 11 ms 23900 KB Output is correct
20 Correct 11 ms 23876 KB Output is correct
21 Correct 11 ms 23896 KB Output is correct
22 Correct 11 ms 24152 KB Output is correct
23 Correct 17 ms 24100 KB Output is correct
24 Correct 14 ms 23896 KB Output is correct
25 Correct 11 ms 23900 KB Output is correct
26 Correct 13 ms 23900 KB Output is correct
27 Correct 11 ms 24000 KB Output is correct
28 Correct 14 ms 23928 KB Output is correct
29 Correct 11 ms 23900 KB Output is correct
30 Correct 11 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23896 KB Output is correct
2 Correct 10 ms 23900 KB Output is correct
3 Correct 10 ms 23868 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 12 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 11 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24096 KB Output is correct
12 Correct 59 ms 37204 KB Output is correct
13 Correct 64 ms 42320 KB Output is correct
14 Correct 43 ms 33148 KB Output is correct
15 Correct 54 ms 33108 KB Output is correct
16 Correct 50 ms 33256 KB Output is correct
17 Correct 11 ms 23900 KB Output is correct
18 Correct 11 ms 23900 KB Output is correct
19 Correct 11 ms 23900 KB Output is correct
20 Correct 10 ms 23896 KB Output is correct
21 Correct 11 ms 23900 KB Output is correct
22 Correct 11 ms 23964 KB Output is correct
23 Correct 11 ms 23956 KB Output is correct
24 Correct 11 ms 23900 KB Output is correct
25 Correct 11 ms 23876 KB Output is correct
26 Correct 11 ms 23896 KB Output is correct
27 Correct 11 ms 24152 KB Output is correct
28 Correct 17 ms 24100 KB Output is correct
29 Correct 14 ms 23896 KB Output is correct
30 Correct 11 ms 23900 KB Output is correct
31 Correct 13 ms 23900 KB Output is correct
32 Correct 11 ms 24000 KB Output is correct
33 Correct 14 ms 23928 KB Output is correct
34 Correct 11 ms 23900 KB Output is correct
35 Correct 11 ms 23900 KB Output is correct
36 Correct 60 ms 37200 KB Output is correct
37 Correct 59 ms 42292 KB Output is correct
38 Correct 42 ms 33100 KB Output is correct
39 Correct 53 ms 33104 KB Output is correct
40 Correct 62 ms 33360 KB Output is correct
41 Correct 56 ms 39832 KB Output is correct
42 Correct 61 ms 40528 KB Output is correct
43 Correct 38 ms 31952 KB Output is correct
44 Correct 50 ms 33172 KB Output is correct
45 Correct 49 ms 33112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 13 ms 23900 KB Output is correct
3 Correct 11 ms 23896 KB Output is correct
4 Correct 11 ms 23896 KB Output is correct
5 Correct 11 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 13 ms 23900 KB Output is correct
8 Correct 10 ms 23896 KB Output is correct
9 Correct 10 ms 23900 KB Output is correct
10 Correct 10 ms 23868 KB Output is correct
11 Correct 10 ms 23900 KB Output is correct
12 Correct 12 ms 23900 KB Output is correct
13 Correct 11 ms 23900 KB Output is correct
14 Correct 11 ms 23900 KB Output is correct
15 Correct 11 ms 24156 KB Output is correct
16 Correct 10 ms 23900 KB Output is correct
17 Correct 11 ms 23900 KB Output is correct
18 Correct 11 ms 24096 KB Output is correct
19 Correct 59 ms 37204 KB Output is correct
20 Correct 64 ms 42320 KB Output is correct
21 Correct 43 ms 33148 KB Output is correct
22 Correct 54 ms 33108 KB Output is correct
23 Correct 50 ms 33256 KB Output is correct
24 Correct 11 ms 23900 KB Output is correct
25 Correct 11 ms 23900 KB Output is correct
26 Correct 11 ms 23900 KB Output is correct
27 Correct 10 ms 23896 KB Output is correct
28 Correct 11 ms 23900 KB Output is correct
29 Correct 11 ms 23964 KB Output is correct
30 Correct 11 ms 23956 KB Output is correct
31 Correct 11 ms 23900 KB Output is correct
32 Correct 11 ms 23876 KB Output is correct
33 Correct 11 ms 23896 KB Output is correct
34 Correct 11 ms 24152 KB Output is correct
35 Correct 17 ms 24100 KB Output is correct
36 Correct 14 ms 23896 KB Output is correct
37 Correct 11 ms 23900 KB Output is correct
38 Correct 13 ms 23900 KB Output is correct
39 Correct 11 ms 24000 KB Output is correct
40 Correct 14 ms 23928 KB Output is correct
41 Correct 11 ms 23900 KB Output is correct
42 Correct 11 ms 23900 KB Output is correct
43 Correct 60 ms 37200 KB Output is correct
44 Correct 59 ms 42292 KB Output is correct
45 Correct 42 ms 33100 KB Output is correct
46 Correct 53 ms 33104 KB Output is correct
47 Correct 62 ms 33360 KB Output is correct
48 Correct 56 ms 39832 KB Output is correct
49 Correct 61 ms 40528 KB Output is correct
50 Correct 38 ms 31952 KB Output is correct
51 Correct 50 ms 33172 KB Output is correct
52 Correct 49 ms 33112 KB Output is correct
53 Correct 60 ms 42320 KB Output is correct
54 Correct 55 ms 40412 KB Output is correct
55 Correct 32 ms 31188 KB Output is correct
56 Correct 50 ms 37472 KB Output is correct
57 Correct 48 ms 33364 KB Output is correct
58 Correct 46 ms 33364 KB Output is correct
59 Correct 47 ms 33296 KB Output is correct
60 Correct 48 ms 33072 KB Output is correct