Submission #896614

# Submission time Handle Problem Language Result Execution time Memory
896614 2024-01-01T18:10:25 Z tsumondai Star Trek (CEOI20_startrek) C++14
100 / 100
69 ms 16656 KB
#include <iostream>
#include <vector>
 
using namespace std;
 
/*
Note that this is an O(N+logD) solution
 
Made for you by Mate Busa
*/
 
const long long mod = 1000000007;
long long N, D, a, b, L, W, LC, WC, sL, sW, sLC, sWC;
vector<int> neighbour[100000];
bool lose[100000];
bool visited[100000];
long long c[100000];
long long oW[100000];
long long oL[100000];
long long h[100000];
 
//calculate the state for all roots
void dfs(int x){
    visited[x] = true; lose[x] = true;
    for(int i:neighbour[x]){
        if(!visited[i]){
            dfs(i);
            if(lose[i]) h[x]++;
        }
    }
    if(h[x]!=0) lose[x] = false;
}
void critical(int x){
    if(lose[x]){
        c[x] = oW[x] + 1;
    } else {
        if(h[x]==1){
            c[x] = oL[x];
        } else {
            c[x] = 0;
        }
    }
}
//calculate critical states
void dfs2(int x){
    visited[x] = true;
    for(int i:neighbour[x]){
        if(!visited[i]){
            dfs2(i);
            if(lose[i]){
                oL[x] += c[i];
            } else {
                oW[x] += c[i];
            }
        }
    }
    critical(x);
}
//reroot the tree in a neighbour
void reroot(int x, int y){
    if(lose[y]){
        h[x]--; oL[x] -= c[y];
    } else {
        oW[x] -= c[y];
    }
    if(h[x]==0) lose[x] = true;
    critical(x);
    if(lose[x]){
        h[y]++; oL[y] += c[x];
    } else {
        oW[y] += c[x];
    }
    if(h[y]!=0) lose[y] = false;
    critical(y);
}
void evaluate(int x){
    visited[x] = true;
    if(lose[x]){
        L++; LC = (LC+c[x])%mod;
    } else {
        W++; WC = (WC+c[x])%mod;
    }
    for(int i:neighbour[x]){
        if(!visited[i]){
            reroot(x,i);
            evaluate(i);
            reroot(i,x);
        }
    }
}
 
//a bonus dimension
void step(long long pl, long long plc, long long pw, long long pwc){
    sL = ((((pl * N) % mod) * W) % mod + (pwc * L) % mod + ((((pl * N)%mod - plc + mod)%mod) * L)%mod) % mod;
    sLC = (plc * WC + pwc * LC) % mod;
    sW = ((((pw * N) % mod) * W) % mod + (plc * L) % mod + ((((pw * N)%mod - pwc + mod)%mod) * L)%mod) % mod;
    sWC = (pwc * WC + plc * LC) % mod;
}
//double up the number of dimensions
void step2(long long pl, long long plc, long long pw, long long pwc){
    L = ((((pl * N) % mod) * pw) % mod + (pwc * pl) % mod + ((((pl * N)%mod - plc + mod)%mod) * pl)%mod) % mod;
    LC = (plc * pwc + pwc * plc) % mod;
    W = ((((pw * N) % mod) * pw) % mod + (plc * pl) % mod + ((((pw * N)%mod - pwc + mod)%mod) * pl)%mod) % mod;
    WC = (pwc * pwc + plc * plc) % mod;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
 
    cin >> N >> D;
    for(int i=0; i<N-1; i++){
        cin >> a >> b;
        neighbour[a-1].push_back(b-1);
        neighbour[b-1].push_back(a-1);
    }
 
    dfs(0);
    for(int i=0; i<N; i++) visited[i] = false;
    dfs2(0);
    for(int i=0; i<N; i++) visited[i] = false;
    evaluate(0);
 
    if(lose[0]){
        sL = 1; sLC = c[0];
    } else {
        sW = 1; sWC = c[0];
    }
 
    //use binary representation of D
    while(D>0){
        if(D%2==1) step(sL, sLC, sW, sWC);
        step2(L,LC,W,WC); D /= 2;
    }
 
    cout << sW << '\n';
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3672 KB Output is correct
2 Correct 1 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3900 KB Output is correct
3 Correct 1 ms 3672 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3900 KB Output is correct
3 Correct 1 ms 3672 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 1 ms 3932 KB Output is correct
9 Correct 1 ms 3920 KB Output is correct
10 Correct 1 ms 3932 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3900 KB Output is correct
3 Correct 1 ms 3672 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 1 ms 3932 KB Output is correct
9 Correct 1 ms 3920 KB Output is correct
10 Correct 1 ms 3932 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
12 Correct 45 ms 13144 KB Output is correct
13 Correct 69 ms 16656 KB Output is correct
14 Correct 32 ms 10440 KB Output is correct
15 Correct 38 ms 10324 KB Output is correct
16 Correct 39 ms 10324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3900 KB Output is correct
3 Correct 1 ms 3672 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 1 ms 3932 KB Output is correct
9 Correct 1 ms 3920 KB Output is correct
10 Correct 1 ms 3932 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
12 Correct 1 ms 3676 KB Output is correct
13 Correct 1 ms 3932 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 1 ms 3676 KB Output is correct
18 Correct 1 ms 3672 KB Output is correct
19 Correct 1 ms 3676 KB Output is correct
20 Correct 1 ms 3676 KB Output is correct
21 Correct 2 ms 3932 KB Output is correct
22 Correct 2 ms 3884 KB Output is correct
23 Correct 1 ms 3928 KB Output is correct
24 Correct 1 ms 3932 KB Output is correct
25 Correct 1 ms 3932 KB Output is correct
26 Correct 1 ms 3932 KB Output is correct
27 Correct 1 ms 3932 KB Output is correct
28 Correct 1 ms 3932 KB Output is correct
29 Correct 2 ms 3904 KB Output is correct
30 Correct 2 ms 3908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3900 KB Output is correct
3 Correct 1 ms 3672 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 3932 KB Output is correct
8 Correct 1 ms 3932 KB Output is correct
9 Correct 1 ms 3920 KB Output is correct
10 Correct 1 ms 3932 KB Output is correct
11 Correct 2 ms 3932 KB Output is correct
12 Correct 45 ms 13144 KB Output is correct
13 Correct 69 ms 16656 KB Output is correct
14 Correct 32 ms 10440 KB Output is correct
15 Correct 38 ms 10324 KB Output is correct
16 Correct 39 ms 10324 KB Output is correct
17 Correct 1 ms 3676 KB Output is correct
18 Correct 1 ms 3932 KB Output is correct
19 Correct 1 ms 3676 KB Output is correct
20 Correct 1 ms 3676 KB Output is correct
21 Correct 1 ms 3676 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 1 ms 3672 KB Output is correct
24 Correct 1 ms 3676 KB Output is correct
25 Correct 1 ms 3676 KB Output is correct
26 Correct 2 ms 3932 KB Output is correct
27 Correct 2 ms 3884 KB Output is correct
28 Correct 1 ms 3928 KB Output is correct
29 Correct 1 ms 3932 KB Output is correct
30 Correct 1 ms 3932 KB Output is correct
31 Correct 1 ms 3932 KB Output is correct
32 Correct 1 ms 3932 KB Output is correct
33 Correct 1 ms 3932 KB Output is correct
34 Correct 2 ms 3904 KB Output is correct
35 Correct 2 ms 3908 KB Output is correct
36 Correct 39 ms 13148 KB Output is correct
37 Correct 49 ms 16436 KB Output is correct
38 Correct 27 ms 10408 KB Output is correct
39 Correct 55 ms 10452 KB Output is correct
40 Correct 56 ms 10580 KB Output is correct
41 Correct 41 ms 15044 KB Output is correct
42 Correct 40 ms 15448 KB Output is correct
43 Correct 25 ms 9688 KB Output is correct
44 Correct 38 ms 10448 KB Output is correct
45 Correct 37 ms 10328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3672 KB Output is correct
2 Correct 1 ms 3932 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 2 ms 3900 KB Output is correct
10 Correct 1 ms 3672 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 1 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 2 ms 3932 KB Output is correct
15 Correct 1 ms 3932 KB Output is correct
16 Correct 1 ms 3920 KB Output is correct
17 Correct 1 ms 3932 KB Output is correct
18 Correct 2 ms 3932 KB Output is correct
19 Correct 45 ms 13144 KB Output is correct
20 Correct 69 ms 16656 KB Output is correct
21 Correct 32 ms 10440 KB Output is correct
22 Correct 38 ms 10324 KB Output is correct
23 Correct 39 ms 10324 KB Output is correct
24 Correct 1 ms 3676 KB Output is correct
25 Correct 1 ms 3932 KB Output is correct
26 Correct 1 ms 3676 KB Output is correct
27 Correct 1 ms 3676 KB Output is correct
28 Correct 1 ms 3676 KB Output is correct
29 Correct 1 ms 3676 KB Output is correct
30 Correct 1 ms 3672 KB Output is correct
31 Correct 1 ms 3676 KB Output is correct
32 Correct 1 ms 3676 KB Output is correct
33 Correct 2 ms 3932 KB Output is correct
34 Correct 2 ms 3884 KB Output is correct
35 Correct 1 ms 3928 KB Output is correct
36 Correct 1 ms 3932 KB Output is correct
37 Correct 1 ms 3932 KB Output is correct
38 Correct 1 ms 3932 KB Output is correct
39 Correct 1 ms 3932 KB Output is correct
40 Correct 1 ms 3932 KB Output is correct
41 Correct 2 ms 3904 KB Output is correct
42 Correct 2 ms 3908 KB Output is correct
43 Correct 39 ms 13148 KB Output is correct
44 Correct 49 ms 16436 KB Output is correct
45 Correct 27 ms 10408 KB Output is correct
46 Correct 55 ms 10452 KB Output is correct
47 Correct 56 ms 10580 KB Output is correct
48 Correct 41 ms 15044 KB Output is correct
49 Correct 40 ms 15448 KB Output is correct
50 Correct 25 ms 9688 KB Output is correct
51 Correct 38 ms 10448 KB Output is correct
52 Correct 37 ms 10328 KB Output is correct
53 Correct 48 ms 16468 KB Output is correct
54 Correct 44 ms 15188 KB Output is correct
55 Correct 23 ms 9176 KB Output is correct
56 Correct 40 ms 13088 KB Output is correct
57 Correct 55 ms 10328 KB Output is correct
58 Correct 34 ms 10328 KB Output is correct
59 Correct 39 ms 10448 KB Output is correct
60 Correct 39 ms 10440 KB Output is correct