답안 #1091240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091240 2024-09-20T08:22:34 Z mispertion Star Trek (CEOI20_startrek) C++17
78 / 100
1000 ms 65428 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];
    }
    ddp[0] = L;
    int n2 = mult(n, n);
    for(int i = 1; i < d; i++){
        ddp[i] = sum(mult(ddp[i - 1], E), mult(L, binpow(n2, i)));
    }
    int ret;
    if(st[1]){
        ret = sum(binpow(n2, d), -mult(C[1], ddp[d - 1]));
    }else{
        ret = mult(C[1], ddp[d - 1]);
    }
    cout << ret << '\n';
}   

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Execution timed out 1064 ms 65428 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23816 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23816 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 11 ms 23880 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23816 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 11 ms 23880 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24044 KB Output is correct
12 Correct 57 ms 37200 KB Output is correct
13 Correct 61 ms 42324 KB Output is correct
14 Correct 41 ms 33100 KB Output is correct
15 Correct 48 ms 33320 KB Output is correct
16 Correct 49 ms 33364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23816 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 11 ms 23880 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24044 KB Output is correct
12 Correct 11 ms 23900 KB Output is correct
13 Correct 10 ms 23900 KB Output is correct
14 Correct 10 ms 23900 KB Output is correct
15 Correct 12 ms 23900 KB Output is correct
16 Correct 10 ms 23900 KB Output is correct
17 Correct 12 ms 23976 KB Output is correct
18 Correct 11 ms 23900 KB Output is correct
19 Correct 11 ms 23900 KB Output is correct
20 Correct 11 ms 23960 KB Output is correct
21 Correct 12 ms 23900 KB Output is correct
22 Correct 12 ms 24152 KB Output is correct
23 Correct 11 ms 23900 KB Output is correct
24 Correct 11 ms 23976 KB Output is correct
25 Correct 11 ms 23960 KB Output is correct
26 Correct 22 ms 24508 KB Output is correct
27 Correct 24 ms 24668 KB Output is correct
28 Correct 27 ms 24668 KB Output is correct
29 Correct 26 ms 24728 KB Output is correct
30 Correct 23 ms 24476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23896 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 11 ms 23900 KB Output is correct
4 Correct 10 ms 23900 KB Output is correct
5 Correct 10 ms 23816 KB Output is correct
6 Correct 10 ms 23900 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 11 ms 24156 KB Output is correct
9 Correct 11 ms 23880 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 24044 KB Output is correct
12 Correct 57 ms 37200 KB Output is correct
13 Correct 61 ms 42324 KB Output is correct
14 Correct 41 ms 33100 KB Output is correct
15 Correct 48 ms 33320 KB Output is correct
16 Correct 49 ms 33364 KB Output is correct
17 Correct 11 ms 23900 KB Output is correct
18 Correct 10 ms 23900 KB Output is correct
19 Correct 10 ms 23900 KB Output is correct
20 Correct 12 ms 23900 KB Output is correct
21 Correct 10 ms 23900 KB Output is correct
22 Correct 12 ms 23976 KB Output is correct
23 Correct 11 ms 23900 KB Output is correct
24 Correct 11 ms 23900 KB Output is correct
25 Correct 11 ms 23960 KB Output is correct
26 Correct 12 ms 23900 KB Output is correct
27 Correct 12 ms 24152 KB Output is correct
28 Correct 11 ms 23900 KB Output is correct
29 Correct 11 ms 23976 KB Output is correct
30 Correct 11 ms 23960 KB Output is correct
31 Correct 22 ms 24508 KB Output is correct
32 Correct 24 ms 24668 KB Output is correct
33 Correct 27 ms 24668 KB Output is correct
34 Correct 26 ms 24728 KB Output is correct
35 Correct 23 ms 24476 KB Output is correct
36 Correct 57 ms 37264 KB Output is correct
37 Correct 58 ms 42204 KB Output is correct
38 Correct 41 ms 33108 KB Output is correct
39 Correct 48 ms 33108 KB Output is correct
40 Correct 48 ms 33364 KB Output is correct
41 Correct 72 ms 40640 KB Output is correct
42 Correct 64 ms 41336 KB Output is correct
43 Correct 50 ms 32548 KB Output is correct
44 Correct 51 ms 33364 KB Output is correct
45 Correct 62 ms 33868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 10 ms 23900 KB Output is correct
4 Correct 11 ms 23900 KB Output is correct
5 Execution timed out 1064 ms 65428 KB Time limit exceeded
6 Halted 0 ms 0 KB -