Submission #856879

# Submission time Handle Problem Language Result Execution time Memory
856879 2023-10-04T18:35:40 Z sofijavelkovska Star Trek (CEOI20_startrek) C++14
50 / 100
1000 ms 14320 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=1e5, MOD=1e9+7;

vector<int> adj[MAXN+1];
bool /*win0[MAXN+1],*/ win1[MAXN+1];
int losechildren[MAXN+1], critical[MAXN+1];
int n;
long long lose=0, e=0;
long long npower[2*MAXN+1];

long long power(long long a, long long k)
{
    long long result=1, current=a;
    for (long long i=0; i<64; i++)
    {
        if (k&((long long)1<<i))
            result=result*current%MOD;
        current=current*current%MOD;
    }

    return result;
}

/*bool dfs0(int x, int t)
{
    bool possible=false;
    for (auto y : adj[x])
    {
        if (y==t)
            continue;
        if (!dfs0(y, x))
            possible=true;
    }

    return win0[x]=possible;
}*/

/*void dfs1(int x, int t)
{
    if (t==-1)
    {
        win1[x]=win0[x];
        for (auto y : adj[x])
        {
            if (y==t)
                continue;
            dfs1(y, x);
        }

        return;
    }
    int memox=losechildren[x];
    int memot=losechildren[t];
    if (!win0[x])
        losechildren[t]=losechildren[t]-1;
    if (losechildren[t]==0)
        losechildren[x]=losechildren[x]+1;
    if (losechildren[x]>0)
        win1[x]=true;
    else
        win1[x]=false;
    for (auto y : adj[x])
    {
        if (y==t)
            continue;
        dfs1(y, x);
    }
    losechildren[x]=memox;
    losechildren[t]=memot;

    return;
}*/

pair<bool, int> dfscritical(int x, int t)
{
    int critical=0, losecritical=0, losenodes=0;
    for (auto y : adj[x])
    {
        if (y==t)
            continue;
        auto result=dfscritical(y, x);
        critical=critical+result.second;
        if (!result.first)
        {
            losecritical=losecritical+result.second;
            losenodes=losenodes+1;
        }
    }
    if (losenodes==0)
        return {false, critical+1};
    else if (losenodes==1)
        return {true, losecritical};

    return {true, 0};
}

/*void countlosechildren(int x, int t)
{
    losechildren[x]=0;
    for (auto y : adj[x])
    {
        if (y==t)
            continue;
        countlosechildren(y, x);
        if (!win0[y])
            losechildren[x]=losechildren[x]+1;
    }

    return;
}*/

long long dp(int d)
{
    if (d==0)
        return lose;

    return (lose*npower[2*d]%MOD+e*dp(d-1)%MOD)%MOD;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);

    int x, y, i;
    long long d, total;
    cin >> n >> d;
    for (i=0; i<n-1; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    //cout << "LOL" << '\n';
    if (n==2)
    {
        cout << power(4, d);
        return 0;
    }
    //dfs0(1, -1);
    //countlosechildren(1, -1);
    //dfs1(1, -1);
    for (i=1; i<=n; i++)
    {
        auto result=dfscritical(i, -1);
        critical[i]=result.second;
        win1[i]=result.first;
    }
    //cout << "LOL" << '\n';
    for (i=1; i<=n; i++)
        if (!win1[i])
            lose=lose+1;
    //long long ewin=0, elose=0;
    for (i=1; i<=n; i++)
    {
        //cout << "prefix ei " << e << '\n';
        if (win1[i])
            e=e+critical[i];
        else
            e=e-critical[i];
    }
    //e=(ewin+MOD-elose)%MOD;
    //for (i=1; i<=n; i++)
      //  cout << critical[i] << " ";
    //cout << '\n';
    //cout << "LOL" << '\n';
    //cout << "e " << e << '\n';
    npower[0]=1;
    for (i=1; i<=2*d; i++)
        npower[i]=npower[i-1]*n%MOD;
    if (win1[1])
        total=(npower[2*d]+MOD-critical[1]*dp(d-1)%MOD)%MOD;
    else
        total=critical[1]*dp(d-1)%MOD;
    cout << total;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 10 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 11 ms 4956 KB Output is correct
8 Correct 13 ms 4956 KB Output is correct
9 Correct 8 ms 4956 KB Output is correct
10 Correct 11 ms 5208 KB Output is correct
11 Correct 9 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 11 ms 4956 KB Output is correct
8 Correct 13 ms 4956 KB Output is correct
9 Correct 8 ms 4956 KB Output is correct
10 Correct 11 ms 5208 KB Output is correct
11 Correct 9 ms 4952 KB Output is correct
12 Execution timed out 1063 ms 14320 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 11 ms 4956 KB Output is correct
8 Correct 13 ms 4956 KB Output is correct
9 Correct 8 ms 4956 KB Output is correct
10 Correct 11 ms 5208 KB Output is correct
11 Correct 9 ms 4952 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 8 ms 4952 KB Output is correct
14 Correct 1 ms 5040 KB Output is correct
15 Correct 1 ms 5048 KB Output is correct
16 Correct 2 ms 5052 KB Output is correct
17 Correct 1 ms 4956 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 1 ms 4956 KB Output is correct
20 Correct 1 ms 5060 KB Output is correct
21 Correct 12 ms 5140 KB Output is correct
22 Correct 12 ms 5152 KB Output is correct
23 Correct 8 ms 4952 KB Output is correct
24 Correct 8 ms 4956 KB Output is correct
25 Correct 9 ms 5096 KB Output is correct
26 Correct 12 ms 6492 KB Output is correct
27 Correct 15 ms 6452 KB Output is correct
28 Correct 8 ms 6748 KB Output is correct
29 Correct 11 ms 6376 KB Output is correct
30 Correct 12 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 11 ms 4956 KB Output is correct
8 Correct 13 ms 4956 KB Output is correct
9 Correct 8 ms 4956 KB Output is correct
10 Correct 11 ms 5208 KB Output is correct
11 Correct 9 ms 4952 KB Output is correct
12 Execution timed out 1063 ms 14320 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 10 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4952 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 11 ms 4956 KB Output is correct
15 Correct 13 ms 4956 KB Output is correct
16 Correct 8 ms 4956 KB Output is correct
17 Correct 11 ms 5208 KB Output is correct
18 Correct 9 ms 4952 KB Output is correct
19 Execution timed out 1063 ms 14320 KB Time limit exceeded
20 Halted 0 ms 0 KB -