Submission #975288

# Submission time Handle Problem Language Result Execution time Memory
975288 2024-05-04T17:04:50 Z efedmrlr Star Trek (CEOI20_startrek) C++17
78 / 100
1000 ms 15452 KB
#include <bits/stdc++.h>

#define lli long long int
#define ld long double
#define pb push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)

using namespace std;

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int N = 1e5 + 5;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;

int add(int x, int y) {
    if(x + y >= MOD) return x + y - MOD;
    return x + y;
}
int mult(int x, int y) {
    return (int)((1ll * x * y) % MOD);
}
int subt(int x, int y) {
    if(x - y < 0) return x - y + MOD;
    return x - y;
}
int fp(int x, lli y) {
    int ret = 1;
    while(y > 0ll) {
        if(y & 1ll) {
            ret = mult(ret, x);
        }
        x = mult(x, x);
        y /= 2ll;
    }
    return ret;
}

int n;
lli d;
vector<vector<int> > adj(N, vector<int>());
vector<int> dp(N, 0), dpr(N, 0);
vector<int> dpcrit(N, 0), dpc(N, 0);
vector<int> wc(N, 0), lc(N, 0);
int L = 0;
int CL = 0, CW = 0;
void dfs1(int node, int par) {
    for(auto c : adj[node]) {
        if(c == par) continue;
        dfs1(c, node);
        if(!dpr[c]) dpr[node]++;
    }

}
void dfs2(int node, int par) {
    if(!dpr[node]) {
        dpcrit[node] = 1;
    }
    for(auto c : adj[node]) {
        if(c == par) continue;
        dfs2(c, node);
        if(dpr[c]) wc[node] += dpcrit[c];
        else lc[node] += dpcrit[c];
        if(dpr[node] == 1 && !dpr[c]) {
            dpcrit[node] += dpcrit[c];
        } 
        if(!dpr[node] && dpr[c]) {
            dpcrit[node] += dpcrit[c];
        }
    }
    
}
void change_root(int p, int x) {
    if(!dpr[x]) {
        dpr[p]--;
        lc[p] -= dpcrit[x];
    }
    else {
        wc[p] -= dpcrit[x];
    }
    if(dpr[p] >= 2) {
        dpcrit[p] = 0;
    }
    else if(dpr[p] == 1) {
        dpcrit[p] = lc[p];
    }
    else {
        dpcrit[p] = wc[p] + 1;
    }

    if(!dpr[p]) {
        dpr[x]++;
        lc[x] += dpcrit[p];
    }
    else {
        wc[x] += dpcrit[p];
    }
    if(dpr[x] >= 2) {
        dpcrit[x] = 0;
    }
    else if(dpr[x] == 1) {
        dpcrit[x] = lc[x];
    }
    else {
        dpcrit[x] = wc[x] + 1;
    }

}

void reroot(int node, int par) {
    dp[node] = dpr[node];
    dpc[node] = dpcrit[node];
    for(auto c : adj[node]) {
        if(c == par) continue;
        change_root(node, c);
        reroot(c, node);
        change_root(c, node); 
    }

}

void solve() {
    cin >> n >> d;
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs1(1, 0);
    dfs2(1, 0);
    // for(int i = 1; i <= n; i++) {
    //     cout << "i:" << i << " dpr:" << dpr[i] << " crit:" << dpcrit[i] << "\n";
    // }
    reroot(1, 0);
    // for(int i = 1; i <= n; i++) {
    //     cout << "i:" << i << " dp:" << dp[i] << " c:" << dpc[i] << "\n";
    // }
    for(int i = 1; i <= n; i++) {
        if(!dp[i]) {
            L++;
            CL = add(CL, dpc[i]);
        }
        else {
            CW = add(CW, dpc[i]);
        }
    }
    // cout << "L:" << L << " CL:" << CL << " CW:" << CW << "\n";
    int LD = L, LDN = 0;
    for(int i = 1; i < d; i++) {
        LDN = add(mult(LD, subt(CW, CL)), mult(L, fp(n, 2ll * i)));
        swap(LD, LDN);
    }
    int ans = 0;
    if(dp[1]) {
        ans = mult(dpc[1], LD);
    }
    else {
        ans = subt(fp(n, 2ll * d), mult(dpc[1], LD));
    }
    ans = subt(fp(n, 2ll * d), ans);
    cout << ans << "\n";
}

signed main() {
    fastio();
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Execution timed out 1092 ms 4956 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 42 ms 12344 KB Output is correct
13 Correct 56 ms 15444 KB Output is correct
14 Correct 31 ms 9632 KB Output is correct
15 Correct 35 ms 9308 KB Output is correct
16 Correct 41 ms 9276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 5140 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 3 ms 4956 KB Output is correct
16 Correct 2 ms 4920 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 3 ms 4956 KB Output is correct
24 Correct 3 ms 4952 KB Output is correct
25 Correct 3 ms 5208 KB Output is correct
26 Correct 9 ms 5172 KB Output is correct
27 Correct 11 ms 5212 KB Output is correct
28 Correct 12 ms 4956 KB Output is correct
29 Correct 9 ms 4956 KB Output is correct
30 Correct 7 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 42 ms 12344 KB Output is correct
13 Correct 56 ms 15444 KB Output is correct
14 Correct 31 ms 9632 KB Output is correct
15 Correct 35 ms 9308 KB Output is correct
16 Correct 41 ms 9276 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 3 ms 5140 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 2 ms 4920 KB Output is correct
22 Correct 3 ms 4956 KB Output is correct
23 Correct 3 ms 4956 KB Output is correct
24 Correct 2 ms 4956 KB Output is correct
25 Correct 3 ms 4956 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 4956 KB Output is correct
29 Correct 3 ms 4952 KB Output is correct
30 Correct 3 ms 5208 KB Output is correct
31 Correct 9 ms 5172 KB Output is correct
32 Correct 11 ms 5212 KB Output is correct
33 Correct 12 ms 4956 KB Output is correct
34 Correct 9 ms 4956 KB Output is correct
35 Correct 7 ms 5208 KB Output is correct
36 Correct 39 ms 12332 KB Output is correct
37 Correct 42 ms 15452 KB Output is correct
38 Correct 30 ms 9432 KB Output is correct
39 Correct 43 ms 9732 KB Output is correct
40 Correct 37 ms 9308 KB Output is correct
41 Correct 47 ms 13916 KB Output is correct
42 Correct 45 ms 14672 KB Output is correct
43 Correct 33 ms 8916 KB Output is correct
44 Correct 50 ms 9360 KB Output is correct
45 Correct 60 ms 9352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 5208 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Execution timed out 1092 ms 4956 KB Time limit exceeded
6 Halted 0 ms 0 KB -