답안 #659646

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
659646 2022-11-18T21:06:14 Z Lobo Star Trek (CEOI20_startrek) C++17
43 / 100
1000 ms 62024 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 5e5+10;
const int mod = 1e9+7;

int n, d, dpw[maxn][2], dpl[maxn][2], dpwup[maxn][2], dplup[maxn][2];
vector<int> g[maxn];
int SL, SW, SL0, SW0, SWb, SLb;

void dfs1(int u, int ant) {
    dpw[u][1] = SW*dpw[u][0] + SL*(dpw[u][0]+dpl[u][0]); dpw[u][1]%= mod;
    dpl[u][1] = SW*dpl[u][0]; dpl[u][1]%= mod;

    int qtl = 0;
    for(auto v : g[u]) if(v != ant) {
        qtl+= dpl[v][0];
        dfs1(v,u);
    }

    for(auto v : g[u]) if(v != ant) {
        qtl-= dpl[v][0];
        if(qtl) {
            dpw[u][1]+= dpw[v][1]+dpl[v][1]; dpw[u][1]%= mod;
            dpl[u][1]+= 0;
        }
        else {
            dpw[u][1]+= dpl[v][1]; dpw[u][1]%= mod;
            dpl[u][1]+= dpw[v][1]; dpl[u][1]%= mod;
        }
        qtl+= dpl[v][0];
    }
}

int dw[maxn][2], dl[maxn][2], sdw[maxn][2], sdl[maxn][2], sbsz[maxn];
set<int> dl0s[maxn];
void sol(int u, int ant) {
    dw[u][0] = 0;
    dl[u][0] = 0;
    sdw[u][0] = 0;
    sdl[u][0] = 0;
    dw[u][1] = 0;
    dl[u][1] = 0;
    sdw[u][1] = 0;
    sdl[u][1] = 0;
    sbsz[u] = 1;
    dl0s[u].clear();
    for(auto v : g[u]) if(v != ant) {
        sol(v,u);
        sdw[u][0]+= dw[v][0];
        sdw[u][1]+= dw[v][1];
        sdl[u][0]+= dl[v][0];
        sdl[u][1]+= dl[v][1];
        sbsz[u]+= sbsz[v];
        if(dl[v][0]) {
            dl0s[u].insert(v);
        }
    }

    if(sdl[u][0] >= 1) {
        dw[u][0] = 1;
    }
    else {
        dw[u][0] = 0;
    }

    if(sdl[u][0] >= 2) {
        dw[u][1] = 1+sdw[u][1]+sdl[u][1];
    }
    else if(sdl[u][0] == 1) {
        dw[u][1] = 1+sdw[u][1]+sdl[u][1];
        int v1 = *dl0s[u].begin();
        dw[u][1]-= dw[v1][1];
    }
    else {
        dw[u][1] = 1+sdl[u][1];
    }
    dl[u][0] = 1-dw[u][0];
    dl[u][1] = sbsz[u]-dw[u][1];
}

void dfs0(int u, int ant) {
    dpw[u][0] = 0;
    int qtl = 0;
    for(auto v : g[u]) if(v != ant) {
        dfs0(v,u);
        qtl+= dpl[v][0];
    }

    if(qtl) dpw[u][0] = 1;
    dpl[u][0] = 1-dpw[u][0];
}

void dfs0up(int u, int ant) {
    int qtl = 0;
    for(auto v : g[u]) if(v != ant) {
        qtl+= dpl[v][0];
    }

    for(auto v : g[u]) if(v != ant) {
        qtl-= dpl[v][0];
        if(qtl || dpwup[u][0]) {
            dpwup[v][0] = 0;
            dplup[v][0] = 1-dpwup[v][0];
        }
        else {
            dpwup[v][0] = 1;
            dplup[v][0] = 1-dpwup[v][0];
        }
        qtl+= dpl[v][0];
    }

    for(auto v : g[u]) if(v != ant) {
        dfs0up(v,u);
    }
}

void solve() {
    cin >> n >> d;

    for(int i = 1; i <= n-1; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    dfs0(1,0);
    dpwup[1][0] = 0;
    dplup[1][0] = 1;
    dfs0up(1,0);
    for(int i = 1; i <= n; i++) {
        if(dpw[i][0] || dpwup[i][0]) SW0++;
        else SL0++;
    }

    for(int i = 1; i <= n; i++) {
        sol(i,0);
        SWb+= dw[i][1];
        // cout << i << " " << dw[i][1] << endl;
    }
    SLb = n*n-SWb;

    // cout << SWb << " " << SLb << endl;
    // cout << SW0 << " " << SL0 << endl;
    SWb%= mod;
    SLb%= mod;
    SW = SW0;
    SL = SL0;
    for(int i = 1; i <= d-1; i++) {
        int newSW = n*(SW*SW0%mod)%mod + SL*SWb; newSW%= mod;
        int newSL = n*(SW*SL0%mod)%mod + SL*SLb; newSL%= mod;
        SW = newSW;
        SL = newSL;
    }
    dfs1(1,0);
    // cout << SW << " " << SL << endl;
    cout << dpw[1][1] << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 73 ms 35784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 18 ms 35500 KB Output is correct
3 Execution timed out 1089 ms 35488 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 17 ms 35552 KB Output is correct
3 Correct 18 ms 35636 KB Output is correct
4 Correct 17 ms 35568 KB Output is correct
5 Correct 19 ms 35636 KB Output is correct
6 Correct 18 ms 35540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 17 ms 35552 KB Output is correct
3 Correct 18 ms 35636 KB Output is correct
4 Correct 17 ms 35568 KB Output is correct
5 Correct 19 ms 35636 KB Output is correct
6 Correct 18 ms 35540 KB Output is correct
7 Correct 71 ms 35840 KB Output is correct
8 Correct 72 ms 35876 KB Output is correct
9 Correct 52 ms 35780 KB Output is correct
10 Correct 77 ms 35728 KB Output is correct
11 Correct 67 ms 35728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 17 ms 35552 KB Output is correct
3 Correct 18 ms 35636 KB Output is correct
4 Correct 17 ms 35568 KB Output is correct
5 Correct 19 ms 35636 KB Output is correct
6 Correct 18 ms 35540 KB Output is correct
7 Correct 71 ms 35840 KB Output is correct
8 Correct 72 ms 35876 KB Output is correct
9 Correct 52 ms 35780 KB Output is correct
10 Correct 77 ms 35728 KB Output is correct
11 Correct 67 ms 35728 KB Output is correct
12 Execution timed out 1094 ms 62024 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 17 ms 35552 KB Output is correct
3 Correct 18 ms 35636 KB Output is correct
4 Correct 17 ms 35568 KB Output is correct
5 Correct 19 ms 35636 KB Output is correct
6 Correct 18 ms 35540 KB Output is correct
7 Correct 71 ms 35840 KB Output is correct
8 Correct 72 ms 35876 KB Output is correct
9 Correct 52 ms 35780 KB Output is correct
10 Correct 77 ms 35728 KB Output is correct
11 Correct 67 ms 35728 KB Output is correct
12 Correct 17 ms 35576 KB Output is correct
13 Correct 67 ms 35796 KB Output is correct
14 Correct 17 ms 35616 KB Output is correct
15 Correct 18 ms 35608 KB Output is correct
16 Correct 18 ms 35540 KB Output is correct
17 Correct 18 ms 35640 KB Output is correct
18 Correct 18 ms 35540 KB Output is correct
19 Correct 18 ms 35540 KB Output is correct
20 Correct 19 ms 35732 KB Output is correct
21 Correct 66 ms 35844 KB Output is correct
22 Correct 74 ms 35876 KB Output is correct
23 Correct 52 ms 35796 KB Output is correct
24 Correct 70 ms 35796 KB Output is correct
25 Correct 74 ms 35796 KB Output is correct
26 Correct 69 ms 35796 KB Output is correct
27 Correct 73 ms 35916 KB Output is correct
28 Correct 43 ms 35668 KB Output is correct
29 Correct 67 ms 35796 KB Output is correct
30 Correct 76 ms 35800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35540 KB Output is correct
2 Correct 17 ms 35552 KB Output is correct
3 Correct 18 ms 35636 KB Output is correct
4 Correct 17 ms 35568 KB Output is correct
5 Correct 19 ms 35636 KB Output is correct
6 Correct 18 ms 35540 KB Output is correct
7 Correct 71 ms 35840 KB Output is correct
8 Correct 72 ms 35876 KB Output is correct
9 Correct 52 ms 35780 KB Output is correct
10 Correct 77 ms 35728 KB Output is correct
11 Correct 67 ms 35728 KB Output is correct
12 Execution timed out 1094 ms 62024 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 35540 KB Output is correct
2 Correct 73 ms 35784 KB Output is correct
3 Correct 17 ms 35540 KB Output is correct
4 Correct 18 ms 35500 KB Output is correct
5 Execution timed out 1089 ms 35488 KB Time limit exceeded
6 Halted 0 ms 0 KB -