Submission #557680

# Submission time Handle Problem Language Result Execution time Memory
557680 2022-05-05T18:41:36 Z status_coding Star Trek (CEOI20_startrek) C++14
30 / 100
1000 ms 12984 KB
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

long long put(long long baza, long long exp)
{
    if(!exp)
        return 1;

    long long ans = put(baza, exp/2);
    ans *= ans;
    ans %= mod;

    if(exp % 2)
    {
        ans *= baza;
        ans %= mod;
    }

    return ans;
}

long long n,d;

bool win[100005];
long long nrCritF[100005];

vector<int> v[100005];

bool dp[100005];
int nrFChild[100005];
int nrCrit[100005];

long long cntF[100005];

void calcDp(int p, int avoid=0)
{
    nrFChild[p] = 0;
    dp[p] = false;
    nrCrit[p] = 0;

    for(int it : v[p])
    {
        if(it == avoid)
            continue;

        if(!dp[it])
        {
            nrFChild[p] ++;
            dp[p] = true;
        }
    }

    if(!dp[p])
        nrCrit[p] = 1;

    for(int it : v[p])
    {
        if(it == avoid)
            continue;

        if(!dp[p] && dp[it])
            nrCrit[p] += nrCrit[it];

        if(dp[p] && !dp[it] && nrFChild[p] == 1)
            nrCrit[p] += nrCrit[it];
    }
}

void dfs(int p, int par)
{
    for(int it : v[p])
    {
        if(it == par)
            continue;

        dfs(it, p);
    }

    calcDp(p, par);
}

void calc(int p, int par)
{
    win[p] = dp[p];
    nrCritF[p] = nrCrit[p];

    for(int it : v[p])
    {
        if(it == par)
            continue;

        calcDp(p, it);
        calcDp(it);

        calc(it, p);

        calcDp(it, p);
        calcDp(p);
    }
}

int main()
{
    cin>>n>>d;

    if(n == 2)
    {
        cout<<put(n, 2*d);
        return 0;
    }

    for(int i=1;i<n;i++)
    {
        int x, y;
        cin>>x>>y;

        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1, 0);
    calc(1, 0);

    /*
    for(int i=1;i<=n;i++)
        cout<<win[i]<<' '<<nrCritF[i]<<'\n';
    */

    for(int i=1;i<=n;i++)
        if(!win[i])
            cntF[i] = 1;

    for(int i=d-1; i>=0; i--)
    {
        long long nrF = 0;

        for(int j=1;j<=n;j++)
        {
            nrF += cntF[j];
            nrF %= mod;
        }


        for(int j=1;j<=n;j++)
        {
            if(win[j])
            {
                cntF[j] = nrCritF[j] * nrF;
                cntF[j] %= mod;
            }
            else
            {
                cntF[j] = nrCritF[j] * (n - nrF);
                cntF[j] %= mod;

                cntF[j] += (n - nrCritF[j]) * n;
                cntF[j] %= mod;
            }
        }
    }

    cout<<(put(n, 2*d) - cntF[1] + mod) % mod;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 9 ms 2728 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 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 1 ms 2644 KB Output is correct
5 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 1 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 1 ms 2644 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 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 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 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 1 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 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 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 162 ms 10380 KB Output is correct
13 Correct 126 ms 12984 KB Output is correct
14 Execution timed out 1083 ms 7848 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 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 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 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 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 Incorrect 10 ms 2644 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 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 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 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 4 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 162 ms 10380 KB Output is correct
13 Correct 126 ms 12984 KB Output is correct
14 Execution timed out 1083 ms 7848 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Incorrect 9 ms 2728 KB Output isn't correct
3 Halted 0 ms 0 KB -