Submission #585644

# Submission time Handle Problem Language Result Execution time Memory
585644 2022-06-29T07:13:50 Z 박상훈(#8384) Star Trek (CEOI20_startrek) C++17
100 / 100
113 ms 22524 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int MOD = 1e9+7;
vector<int> adj[100100];
bool _win[100100], _win_pa[100100], _win_root[100100], ok[100100];
ll S[100100];

void dfs1(int s, int pa = -1){
    int wcnt = 0, lcnt = 0;
    for (auto &v:adj[s]) if (v!=pa){
        dfs1(v, s);
        if (_win[v]) wcnt++;
        else lcnt++;
    }

    if (!lcnt) _win[s] = 0;
    else _win[s] = 1;

    if (lcnt<=1) ok[s] = 1;
}

void dfs2(int s, int pa = -1){
    int wcnt = 0, lcnt = 0;
    for (auto &v:adj[s]) if (v!=pa){
        if (_win[v]) wcnt++;
        else lcnt++;
    }
    if (pa!=-1){
        if (_win_pa[s]) wcnt++;
        else lcnt++;
    }

    for (auto &v:adj[s]) if (v!=pa){
        if (_win[v]) wcnt--;
        else lcnt--;

        if (!lcnt) _win_pa[v] = 0;
        else _win_pa[v] = 1;

        if (_win[v]) wcnt++;
        else lcnt++;
    }

    if (!lcnt) _win_root[s] = 0;
    else _win_root[s] = 1;

    for (auto &v:adj[s]) if (v!=pa) dfs2(v, s);
}

int lscnt[100100], lscnt_pa[100100], lscnt_root[100100];
void dfs3(int s, int pa = -1){
    if (!_win[s]) lscnt[s]++;

    int lcnt = 0, wcnt = 0;
    for (auto &v:adj[s]) if (v!=pa){
        dfs3(v, s);
        if (!_win[v]) lcnt++;
        else wcnt++;
    }

    for (auto &v:adj[s]) if (v!=pa){
        if (lcnt==1 && !_win[v]) lscnt[s] += lscnt[v];
        else if (lcnt==0) lscnt[s] += lscnt[v];
    }
}

void dfs4(int s, int pa = -1){
    ///count
    int wcnt = 0, lcnt = 0, sum = 0;
    vector<int> L;
    for (auto &v:adj[s]) if (v!=pa){
        if (_win[v]) wcnt++;
        else lcnt++;
        sum += lscnt[v];

        if (!_win[v]) L.push_back(v);
    }
    if (pa!=-1){
        if (_win_pa[s]) wcnt++;
        else lcnt++;
        sum += lscnt_pa[s];
    }

    ///calc pa
    for (auto &v:adj[s]) if (v!=pa){
        if (_win[v]) wcnt--;
        else lcnt--;
        sum -= lscnt[v];

        if (!lcnt) lscnt_pa[v] = sum+1;
        else if (lcnt==1){
            if (pa!=-1 && !_win_pa[s]) lscnt_pa[v] = lscnt_pa[s];
            else{
                if (L[0]!=v) lscnt_pa[v] = lscnt[L[0]];
                else lscnt_pa[v] = lscnt[L[1]];
            }
        }

        if (_win[v]) wcnt++;
        else lcnt++;
        sum += lscnt[v];
    }

    ///calc root
    if (!lcnt) lscnt_root[s] = sum+1;
    else if (lcnt==1){
        if (pa!=-1 && !_win_pa[s]) lscnt_root[s] = lscnt_pa[s];
        else lscnt_root[s] = lscnt[L[0]];
    }

    for (auto &v:adj[s]) if (v!=pa) dfs4(v, s);
}

ll pw(ll a, ll e){
    if (!e) return 1;
    ll ret = pw(a, e/2);
    if (e&1) return ret*ret%MOD*a%MOD;
    return ret*ret%MOD;
}

ll INV(ll x){return pw(x, MOD-2);}

int lscnt_naive[100100];
void naive(int n){
    for (int i=1;i<=n;i++){
        fill(_win, _win+n+1, 0);
        fill(lscnt, lscnt+n+1, 0);
        dfs1(i);
        dfs3(i);
        lscnt_naive[i] = lscnt[i];
        //lscnt_root[i] = lscnt_naive[i];
        assert(lscnt_root[i]==lscnt_naive[i]);
    }
    fill(_win, _win+n+1, 0);
    fill(lscnt, lscnt+n+1, 0);
    dfs1(1);
    dfs3(1);
}

int main(){
    int n;
    ll d;
    scanf("%d %lld", &n, &d);
    //if (n==2) {printf("%lld\n", pw(4, d)); return 0;}

    for (int i=1;i<=n-1;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs1(1);
    dfs2(1);
    dfs3(1);
    dfs4(1);

    if (n<=1000) naive(n);

/*
    for (int i=1;i<=n;i++) printf("%d ", _win[i]); printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", _win_pa[i]); printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", _win_root[i]); printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", lscnt[i]); printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", lscnt_pa[i]); printf("\n");
    for (int i=1;i<=n;i++) printf("%d ", lscnt_root[i]); printf("\n");
*/

    ll ans = 0, T = 0, L = 0;
    for (int i=1;i<=n;i++){
        if (_win_root[i]) T += lscnt_root[i];
        else T -= lscnt_root[i], L++;

        if (T>=MOD) T -= MOD;
        if (T<0) T += MOD;
    }
    T = T * pw(INV(n), 2) % MOD;

    ll B = 0; ///B = S[d-1];
    if (T==1){
        B = (d%MOD) * L % MOD;
    }
    else if (T==0) B = L;
    else{
        ll X = L * INV(-T+1+MOD) % MOD;
        ll S0 = (L-X+MOD)%MOD;
        B = S0 * pw(T, d-1) % MOD;
        B = (B+X)%MOD;
    }
    B = B * pw(n, (d-1)*2) % MOD;

    /*S[0] = L;
    for (int i=1;i<=d;i++){
        //S[i] = (pw(n, i*2) * L % MOD + T * S[i-1] % MOD) % MOD;
        S[i] = (T * S[i-1] % MOD + L) % MOD;
    }

    S[d-1] = S[d-1] * pw(n, (d-1)*2) % MOD;*/

    if (_win[1]) ans = (pw(n, d*2) + MOD) - (lscnt_root[1] * B % MOD);
    else ans = lscnt_root[1] * B % MOD;
    ans %= MOD;

    printf("%lld\n", ans);

    return 0;
}

/*
int lcnt = 0;
    for (int i=1;i<=n;i++) if (!_win_root[i]) lcnt++;

    ll ans = 0;
    if (_win[1]) ans = (ll)n*n - (ll)lscnt[1] * lcnt;
    else ans = (ll)lscnt[1] * lcnt;
    ans %= MOD;

    printf("%lld\n", ans);
*/

Compilation message

startrek.cpp: In function 'int main()':
startrek.cpp:145:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |     scanf("%d %lld", &n, &d);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
startrek.cpp:150:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 35 ms 2736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 40 ms 2772 KB Output is correct
8 Correct 45 ms 2860 KB Output is correct
9 Correct 25 ms 2644 KB Output is correct
10 Correct 36 ms 2740 KB Output is correct
11 Correct 37 ms 2728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 40 ms 2772 KB Output is correct
8 Correct 45 ms 2860 KB Output is correct
9 Correct 25 ms 2644 KB Output is correct
10 Correct 36 ms 2740 KB Output is correct
11 Correct 37 ms 2728 KB Output is correct
12 Correct 90 ms 13744 KB Output is correct
13 Correct 113 ms 21328 KB Output is correct
14 Correct 63 ms 7456 KB Output is correct
15 Correct 83 ms 7292 KB Output is correct
16 Correct 90 ms 7388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 40 ms 2772 KB Output is correct
8 Correct 45 ms 2860 KB Output is correct
9 Correct 25 ms 2644 KB Output is correct
10 Correct 36 ms 2740 KB Output is correct
11 Correct 37 ms 2728 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 35 ms 2732 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 39 ms 2804 KB Output is correct
22 Correct 41 ms 2856 KB Output is correct
23 Correct 25 ms 2748 KB Output is correct
24 Correct 35 ms 2724 KB Output is correct
25 Correct 40 ms 2728 KB Output is correct
26 Correct 34 ms 2780 KB Output is correct
27 Correct 41 ms 2876 KB Output is correct
28 Correct 16 ms 2728 KB Output is correct
29 Correct 35 ms 2740 KB Output is correct
30 Correct 35 ms 2740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 40 ms 2772 KB Output is correct
8 Correct 45 ms 2860 KB Output is correct
9 Correct 25 ms 2644 KB Output is correct
10 Correct 36 ms 2740 KB Output is correct
11 Correct 37 ms 2728 KB Output is correct
12 Correct 90 ms 13744 KB Output is correct
13 Correct 113 ms 21328 KB Output is correct
14 Correct 63 ms 7456 KB Output is correct
15 Correct 83 ms 7292 KB Output is correct
16 Correct 90 ms 7388 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 35 ms 2732 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 39 ms 2804 KB Output is correct
27 Correct 41 ms 2856 KB Output is correct
28 Correct 25 ms 2748 KB Output is correct
29 Correct 35 ms 2724 KB Output is correct
30 Correct 40 ms 2728 KB Output is correct
31 Correct 34 ms 2780 KB Output is correct
32 Correct 41 ms 2876 KB Output is correct
33 Correct 16 ms 2728 KB Output is correct
34 Correct 35 ms 2740 KB Output is correct
35 Correct 35 ms 2740 KB Output is correct
36 Correct 97 ms 13772 KB Output is correct
37 Correct 85 ms 21404 KB Output is correct
38 Correct 48 ms 7548 KB Output is correct
39 Correct 78 ms 7300 KB Output is correct
40 Correct 70 ms 7404 KB Output is correct
41 Correct 81 ms 17724 KB Output is correct
42 Correct 96 ms 19756 KB Output is correct
43 Correct 49 ms 6984 KB Output is correct
44 Correct 87 ms 7332 KB Output is correct
45 Correct 91 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 35 ms 2736 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 40 ms 2772 KB Output is correct
15 Correct 45 ms 2860 KB Output is correct
16 Correct 25 ms 2644 KB Output is correct
17 Correct 36 ms 2740 KB Output is correct
18 Correct 37 ms 2728 KB Output is correct
19 Correct 90 ms 13744 KB Output is correct
20 Correct 113 ms 21328 KB Output is correct
21 Correct 63 ms 7456 KB Output is correct
22 Correct 83 ms 7292 KB Output is correct
23 Correct 90 ms 7388 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 35 ms 2732 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 39 ms 2804 KB Output is correct
34 Correct 41 ms 2856 KB Output is correct
35 Correct 25 ms 2748 KB Output is correct
36 Correct 35 ms 2724 KB Output is correct
37 Correct 40 ms 2728 KB Output is correct
38 Correct 34 ms 2780 KB Output is correct
39 Correct 41 ms 2876 KB Output is correct
40 Correct 16 ms 2728 KB Output is correct
41 Correct 35 ms 2740 KB Output is correct
42 Correct 35 ms 2740 KB Output is correct
43 Correct 97 ms 13772 KB Output is correct
44 Correct 85 ms 21404 KB Output is correct
45 Correct 48 ms 7548 KB Output is correct
46 Correct 78 ms 7300 KB Output is correct
47 Correct 70 ms 7404 KB Output is correct
48 Correct 81 ms 17724 KB Output is correct
49 Correct 96 ms 19756 KB Output is correct
50 Correct 49 ms 6984 KB Output is correct
51 Correct 87 ms 7332 KB Output is correct
52 Correct 91 ms 7380 KB Output is correct
53 Correct 91 ms 22524 KB Output is correct
54 Correct 90 ms 19668 KB Output is correct
55 Correct 35 ms 7328 KB Output is correct
56 Correct 72 ms 14968 KB Output is correct
57 Correct 63 ms 8572 KB Output is correct
58 Correct 62 ms 8564 KB Output is correct
59 Correct 64 ms 8508 KB Output is correct
60 Correct 99 ms 8556 KB Output is correct