Submission #343165

# Submission time Handle Problem Language Result Execution time Memory
343165 2021-01-03T13:07:50 Z sealnot123 Star Trek (CEOI20_startrek) C++14
78 / 100
1000 ms 18764 KB
/*
	Author: AquaBlaze
	Time: 2021-01-03 18:10:22
	Generated by powerful Codeforces Tool :^)
 	You can download the binary file in here https://github.com/xalanq/cf-tool (Windows, macOS, Linux)	
    Keqing best girl :)
    Nephren will always be in my heart
*/
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
#define pc(x) putchar(x)
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long i64;
typedef tuple<int,int,int> iii;
typedef pair<int,int> pii;
typedef pair<i64,i64> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int N = 100005;
const int mod = 1000000007;

int add(int a, int b){ return ((a+=b)>=mod)?a-mod:a; }
void adding(int &a, int b){ a = add(a, b); }
int mul(int a, int b){ return a*1ll*b%mod; }

pii operator + (const pii& A, const pii& B){
    return pii(add(A.x,B.x), add(A.y,B.y));
}
pii operator - (const pii& A, const pii& B){
    return pii(add(A.x,mod-B.x), add(A.y,mod-B.y));
}
inline pii& operator += (pii& A, const pii& B){
    A = A+B;
    return A;
}
inline pii& operator -= (pii& A, const pii& B){
    A = A-B;
    return A;
}

vi g[N];
pii dp[N][2], final_dp[N][2], sum[N][2], lose[N][2];
int result[N], final_res[N];

void adjust(int u){
    if(result[u]) dp[u][1]+=pii(1,1);
    else{
        dp[u][1]+=pii(0,1);
        dp[u][0]+=pii(1,0);
    }
}

void dejust(int u){
    if(result[u]) dp[u][1]-=pii(1,1);
    else{
        dp[u][1]-=pii(0,1);
        dp[u][0]-=pii(1,0);
    }
}

void add_node(int u, int v){
    dejust(u);
    if(result[u]==0){
        if(!result[v]){
            dp[u][1] = sum[u][0]+sum[u][1]+dp[v][0];
            dp[u][0] = dp[v][1];
        }else{
            dp[u][1] += dp[v][0];
            dp[u][0] += dp[v][1];
        }
    }else if(result[u]==1){
        if(!result[v]){
            dp[u][0] -= lose[u][1];
            dp[u][1] += lose[u][1]+dp[v][0]+dp[v][1];
        }else{
            dp[u][1] += dp[v][0]+dp[v][1];
        }
    }else{
        dp[u][1] += dp[v][0]+dp[v][1];
    }

    if(!result[v]){
        FOR(i,0,1) lose[u][i]+=dp[v][i];
    }
    result[u] += !result[v];
    adjust(u);
    FOR(i,0,1) sum[u][i]+=dp[v][i];
}

void sub_node(int u, int v){
    dejust(u);
    if(result[u]==0){
        if(!result[v]){
            assert(0);
        }else{
            dp[u][1] -= dp[v][0];
            dp[u][0] -= dp[v][1];
        }
    }else if(result[u]==1){
        if(!result[v]){
            dp[u][0] = sum[u][1]-dp[v][1];
            dp[u][1] = sum[u][0]-dp[v][0];
        }else{
            dp[u][1] -= dp[v][0]+dp[v][1];
        }
    }else if(result[u]==2){
        if(!result[v]){
            dp[u][1] -= lose[u][0]+lose[u][1];
            dp[u][1] += lose[u][0]-dp[v][0];
            dp[u][0] += lose[u][1]-dp[v][1];
        }else{
            dp[u][1] -= dp[v][0]+dp[v][1];
        }
    }else{
        dp[u][1] -= dp[v][0]+dp[v][1];
    }

    if(!result[v]){
        FOR(i,0,1) lose[u][i]-=dp[v][i];
    }
    result[u] -= !result[v];
    adjust(u);
    FOR(i,0,1) sum[u][i]-=dp[v][i];
}

void dfs(int u, int p){
    for(int &e : g[u]){
        if(e == p){
            swap(e, g[u].back());
            g[u].pop_back();
            break;
        }
    }
    for(const int &e : g[u]){
        dfs(e, u);
    }
    adjust(u);
    for(const int &e : g[u]){
        add_node(u, e);
    }
}

void tour(int u){
    final_res[u] = !(!result[u]);
    FOR(i, 0, 1) final_dp[u][i]=dp[u][i];
    for(const int &e : g[u]){
        sub_node(u, e);
        add_node(e, u);
        tour(e);
        sub_node(e, u);
        add_node(u, e);
    }
}

void solve(){
    int n;
    i64 d;
    cin >> n >> d;
    FOR(i, 2, n){
        int a, b;
        cin >> a >> b;
        g[a].eb(b);
        g[b].eb(a);
    }
    dfs(1, -1);
    tour(1);

    assert(dp[1][0]==final_dp[1][0]);
    assert(dp[1][1]==final_dp[1][1]);

    vvi m(2, vi(2,0));
    vi res(2, 0);
    ROF(i, n, 1){
        int r = final_res[i];
        //printf("%d\n",r);
        res[1-r]++;
        FOR(j, 0, 1){
            adding(m[j^1][0], final_dp[i][j].x);
            adding(m[j^1][1], final_dp[i][j].y);
        }
    }
    FOR(t, 2, d){
        vi tmp(2, 0);
        FOR(i, 0, 1){
            FOR(j, 0, 1){
                adding(tmp[i], mul(res[j], m[i][j]));
            }
        }
        res = tmp;
    }
    int ans = 0;
    adding(ans, mul(final_dp[1][1].x, res[0]));
    adding(ans, mul(final_dp[1][1].y, res[1]));
    printf("%d",ans);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
	return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Execution timed out 1096 ms 2668 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 2 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 2 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 125 ms 15084 KB Output is correct
13 Correct 151 ms 17516 KB Output is correct
14 Correct 102 ms 13032 KB Output is correct
15 Correct 115 ms 12908 KB Output is correct
16 Correct 128 ms 13036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 2 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2796 KB Output is correct
14 Correct 2 ms 2668 KB Output is correct
15 Correct 2 ms 2668 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2668 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 2 ms 2924 KB Output is correct
23 Correct 2 ms 2796 KB Output is correct
24 Correct 2 ms 2796 KB Output is correct
25 Correct 2 ms 2796 KB Output is correct
26 Correct 5 ms 2796 KB Output is correct
27 Correct 5 ms 2924 KB Output is correct
28 Correct 5 ms 2796 KB Output is correct
29 Correct 5 ms 2796 KB Output is correct
30 Correct 4 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 2 ms 2924 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 2 ms 2796 KB Output is correct
11 Correct 2 ms 2796 KB Output is correct
12 Correct 125 ms 15084 KB Output is correct
13 Correct 151 ms 17516 KB Output is correct
14 Correct 102 ms 13032 KB Output is correct
15 Correct 115 ms 12908 KB Output is correct
16 Correct 128 ms 13036 KB Output is correct
17 Correct 2 ms 2668 KB Output is correct
18 Correct 2 ms 2796 KB Output is correct
19 Correct 2 ms 2668 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2668 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 2 ms 2924 KB Output is correct
28 Correct 2 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 2 ms 2796 KB Output is correct
31 Correct 5 ms 2796 KB Output is correct
32 Correct 5 ms 2924 KB Output is correct
33 Correct 5 ms 2796 KB Output is correct
34 Correct 5 ms 2796 KB Output is correct
35 Correct 4 ms 2796 KB Output is correct
36 Correct 126 ms 16236 KB Output is correct
37 Correct 153 ms 18764 KB Output is correct
38 Correct 109 ms 14056 KB Output is correct
39 Correct 123 ms 14188 KB Output is correct
40 Correct 127 ms 14060 KB Output is correct
41 Correct 147 ms 17644 KB Output is correct
42 Correct 132 ms 17388 KB Output is correct
43 Correct 92 ms 12776 KB Output is correct
44 Correct 114 ms 14188 KB Output is correct
45 Correct 121 ms 14060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2796 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Execution timed out 1096 ms 2668 KB Time limit exceeded
6 Halted 0 ms 0 KB -