Submission #1002540

# Submission time Handle Problem Language Result Execution time Memory
1002540 2024-06-19T16:01:41 Z doducanh Star Trek (CEOI20_startrek) C++14
100 / 100
46 ms 18008 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int maxn=1e5+7;
vector<int>a[maxn];
int n,m,A,B,L;
int lc[maxn],wc[maxn];
int dp[maxn],dpc[maxn];
int dpr[maxn],dpcrit[maxn];
int add(int a,int b){return (a+b)%mod;}
int sub(int a,int b){return (a%mod-b%mod+mod)%mod;}
int mul(int a,int b){return 1ll*a*b%mod;}
int Pow(int a,int b)
{
    int res=1;
    for(;b;b=b/2,a=(a*a)%mod)if(b&1)res=(res*a)%mod;
    return res;
}
void dfs(int u,int par)
{
    dpr[u]=0;
    for(int v:a[u])if(v!=par){
        dfs(v,u);
        if(dpr[v]==0)dpr[u]++;
    }
}
void dfs1(int u,int par)
{
    if(dpr[u]==0)dpcrit[u]=1;
    for(int v:a[u])if(v!=par){
        dfs1(v,u);
        if(!dpr[v])lc[u]+=dpcrit[v];
        else wc[u]+=dpcrit[v];
        if(dpr[u]==1&&dpr[v]==0)dpcrit[u]+=dpcrit[v];
        if(dpr[u]==0&&dpr[v]==1)dpcrit[u]+=dpcrit[v];
    }
}
void change_root(int aft,int bef)
{
    if(!dpr[aft]){
        dpr[bef]--;
        lc[bef]-=dpcrit[aft];
    }
    else wc[bef]-=dpcrit[aft];
    if(dpr[bef]>=2)dpcrit[bef]=0;
    if(dpr[bef]==1)dpcrit[bef]=lc[bef];
    if(dpr[bef]==0)dpcrit[bef]=wc[bef]+1;
    if(dpr[bef]==0){
        dpr[aft]++;
        lc[aft]+=dpcrit[bef];
    }
    else wc[aft]+=dpcrit[bef];
    if(dpr[aft]>=2){
        dpcrit[aft]=0;
    }
    if(dpr[aft]==1){
        dpcrit[aft]=lc[aft];
    }
    if(dpr[aft]==0){
        dpcrit[aft]=wc[aft]+1;
    }
}
void dfs2(int u,int par)
{
    dp[u]=dpr[u];
    dpc[u]=dpcrit[u];
    for(int v:a[u])if(v!=par){
        change_root(v,u);
        dfs2(v,u);
        change_root(u,v);
    }
}
int calc(int d)
{
    if(d==0)return L;
    if(d&1){
        return mul(calc(d/2),add(Pow(A,d/2+1),Pow(B,d/2+1)));
    }
    else{
        int X=calc(d/2-1);
        return add(mul(X,Pow(B,d/2+1)),add(mul(L,mul(Pow(A,d/2),Pow(B,d/2))),mul(X,Pow(A,d/2+1))));
    }
}
main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<n;i++){
        int x,y;
        cin>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    if(n==2){
        cout<<Pow(4,m);
        return 0;
    }
    dfs(1,0);
    dfs1(1,0);
    dfs2(1,0);
//    for(int i=1;i<=n;i++){
//        cout<<i<<" "<<dp[i]<<" "<<dpc[i]<<"\n";
//    }
    L=0;
    int CW=0,CL=0;
    for(int i=1;i<=n;i++){
        if(dp[i])CW+=dpc[i];
        else L++,CL+=dpc[i];
    }
    B=sub(CW,CL);
    A=Pow(n,2);
//    cout<<add(A,B)<<"\n";
//    cout<<calc(1)<<"\n";
    int LD=calc(m-1);
//    cout<<LD<<"\n";
    if(dp[1]){
        cout<<sub(Pow(n,2*m),mul(dpc[1],LD));
    }
    else cout<<sub(Pow(n,2*m),sub(Pow(n,2*m),mul(dpc[1],LD)));

}

Compilation message

startrek.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   85 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 1 ms 3164 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 1 ms 7260 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 1 ms 7260 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
7 Correct 1 ms 7260 KB Output is correct
8 Correct 1 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 1 ms 7260 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 1 ms 7260 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
7 Correct 1 ms 7260 KB Output is correct
8 Correct 1 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 1 ms 7260 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
12 Correct 35 ms 14672 KB Output is correct
13 Correct 43 ms 17840 KB Output is correct
14 Correct 25 ms 11868 KB Output is correct
15 Correct 34 ms 11924 KB Output is correct
16 Correct 31 ms 12116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 1 ms 7260 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
7 Correct 1 ms 7260 KB Output is correct
8 Correct 1 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 1 ms 7260 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
12 Correct 1 ms 7256 KB Output is correct
13 Correct 1 ms 7344 KB Output is correct
14 Correct 1 ms 3160 KB Output is correct
15 Correct 1 ms 3164 KB Output is correct
16 Correct 1 ms 7260 KB Output is correct
17 Correct 1 ms 7260 KB Output is correct
18 Correct 1 ms 7260 KB Output is correct
19 Correct 1 ms 7260 KB Output is correct
20 Correct 1 ms 7260 KB Output is correct
21 Correct 2 ms 7260 KB Output is correct
22 Correct 1 ms 7260 KB Output is correct
23 Correct 1 ms 7368 KB Output is correct
24 Correct 1 ms 7260 KB Output is correct
25 Correct 1 ms 7260 KB Output is correct
26 Correct 1 ms 7260 KB Output is correct
27 Correct 1 ms 7260 KB Output is correct
28 Correct 1 ms 7260 KB Output is correct
29 Correct 1 ms 7260 KB Output is correct
30 Correct 1 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 7260 KB Output is correct
4 Correct 1 ms 7260 KB Output is correct
5 Correct 1 ms 7260 KB Output is correct
6 Correct 1 ms 7260 KB Output is correct
7 Correct 1 ms 7260 KB Output is correct
8 Correct 1 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 1 ms 7260 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
12 Correct 35 ms 14672 KB Output is correct
13 Correct 43 ms 17840 KB Output is correct
14 Correct 25 ms 11868 KB Output is correct
15 Correct 34 ms 11924 KB Output is correct
16 Correct 31 ms 12116 KB Output is correct
17 Correct 1 ms 7256 KB Output is correct
18 Correct 1 ms 7344 KB Output is correct
19 Correct 1 ms 3160 KB Output is correct
20 Correct 1 ms 3164 KB Output is correct
21 Correct 1 ms 7260 KB Output is correct
22 Correct 1 ms 7260 KB Output is correct
23 Correct 1 ms 7260 KB Output is correct
24 Correct 1 ms 7260 KB Output is correct
25 Correct 1 ms 7260 KB Output is correct
26 Correct 2 ms 7260 KB Output is correct
27 Correct 1 ms 7260 KB Output is correct
28 Correct 1 ms 7368 KB Output is correct
29 Correct 1 ms 7260 KB Output is correct
30 Correct 1 ms 7260 KB Output is correct
31 Correct 1 ms 7260 KB Output is correct
32 Correct 1 ms 7260 KB Output is correct
33 Correct 1 ms 7260 KB Output is correct
34 Correct 1 ms 7260 KB Output is correct
35 Correct 1 ms 7260 KB Output is correct
36 Correct 39 ms 14780 KB Output is correct
37 Correct 46 ms 18000 KB Output is correct
38 Correct 26 ms 11860 KB Output is correct
39 Correct 31 ms 11904 KB Output is correct
40 Correct 31 ms 12032 KB Output is correct
41 Correct 38 ms 16468 KB Output is correct
42 Correct 35 ms 16856 KB Output is correct
43 Correct 21 ms 11476 KB Output is correct
44 Correct 42 ms 12036 KB Output is correct
45 Correct 32 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 1 ms 7260 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 7260 KB Output is correct
9 Correct 1 ms 7260 KB Output is correct
10 Correct 1 ms 7260 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
12 Correct 1 ms 7260 KB Output is correct
13 Correct 1 ms 7260 KB Output is correct
14 Correct 1 ms 7260 KB Output is correct
15 Correct 1 ms 7260 KB Output is correct
16 Correct 1 ms 7260 KB Output is correct
17 Correct 1 ms 7260 KB Output is correct
18 Correct 1 ms 7260 KB Output is correct
19 Correct 35 ms 14672 KB Output is correct
20 Correct 43 ms 17840 KB Output is correct
21 Correct 25 ms 11868 KB Output is correct
22 Correct 34 ms 11924 KB Output is correct
23 Correct 31 ms 12116 KB Output is correct
24 Correct 1 ms 7256 KB Output is correct
25 Correct 1 ms 7344 KB Output is correct
26 Correct 1 ms 3160 KB Output is correct
27 Correct 1 ms 3164 KB Output is correct
28 Correct 1 ms 7260 KB Output is correct
29 Correct 1 ms 7260 KB Output is correct
30 Correct 1 ms 7260 KB Output is correct
31 Correct 1 ms 7260 KB Output is correct
32 Correct 1 ms 7260 KB Output is correct
33 Correct 2 ms 7260 KB Output is correct
34 Correct 1 ms 7260 KB Output is correct
35 Correct 1 ms 7368 KB Output is correct
36 Correct 1 ms 7260 KB Output is correct
37 Correct 1 ms 7260 KB Output is correct
38 Correct 1 ms 7260 KB Output is correct
39 Correct 1 ms 7260 KB Output is correct
40 Correct 1 ms 7260 KB Output is correct
41 Correct 1 ms 7260 KB Output is correct
42 Correct 1 ms 7260 KB Output is correct
43 Correct 39 ms 14780 KB Output is correct
44 Correct 46 ms 18000 KB Output is correct
45 Correct 26 ms 11860 KB Output is correct
46 Correct 31 ms 11904 KB Output is correct
47 Correct 31 ms 12032 KB Output is correct
48 Correct 38 ms 16468 KB Output is correct
49 Correct 35 ms 16856 KB Output is correct
50 Correct 21 ms 11476 KB Output is correct
51 Correct 42 ms 12036 KB Output is correct
52 Correct 32 ms 12124 KB Output is correct
53 Correct 37 ms 18008 KB Output is correct
54 Correct 38 ms 16676 KB Output is correct
55 Correct 19 ms 10964 KB Output is correct
56 Correct 35 ms 14740 KB Output is correct
57 Correct 32 ms 12372 KB Output is correct
58 Correct 31 ms 12116 KB Output is correct
59 Correct 31 ms 12020 KB Output is correct
60 Correct 31 ms 12084 KB Output is correct