답안 #856866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856866 2023-10-04T17:59:08 Z sofijavelkovska Star Trek (CEOI20_startrek) C++14
30 / 100
1000 ms 13136 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, lose=0;
long long e=0;

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 (power(n, 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);
    }
    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;
    }
    for (i=1; i<=n; i++)
        if (!win1[i])
            lose=lose+1;
    for (i=1; i<=n; i++)
    {
        if (win0[i])
            e=(e+critical[i])%MOD;
        else
            e=(e+MOD-critical[i])%MOD;
    }
    if (win0[1])
        total=(power(n, 2*d)+MOD-critical[1]*dp(d-1)%MOD)%MOD;
    else
        total=critical[1]*dp(d-1)%MOD;
    cout << total;

    return 0;
}


/*#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];

long long power4(long long n)
{
    long long result=1, current=4;
    for (long long i=0; i<64; i++)
    {
        if (n&((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;
}

int dfscritical(int x, int t)
{
    int critical=0;
    if (!win0[x])
    {
        critical=critical+1;
        for (auto y : adj[x])
        {
            if (y==t)
                continue;
            critical=critical+dfscritical(y, x);
        }
    }
    else
    {
        int losenodes=0;
        for (auto y : adj[x])
        {
            if (y==t)
                continue;
            if (!win0[y])
            {
                critical=critical+dfscritical(y, x);
                losenodes=losenodes+1;
            }
        }
        if (losenodes>1)
            critical=0;
    }

    return critical;
}

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;
}

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

    int n, x, y, total, i;
    long long d;
    cin >> n >> d;
    for (i=0; i<n-1; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    if (n==2)
    {
        cout << power4(d);
        return 0;
    }
    dfs0(1, -1);
    countlosechildren(1, -1);
    dfs1(1, -1);
    int win=0;
    for (i=1; i<=n; i++)
        if (win1[i])
            win=win+1;
    int lose=n-win;
    int critical=dfscritical(1, -1);
    if (win0[1])
        total=((long long)n*win+(long long)(n-critical)*lose)%MOD;
    else
        total=(long long)critical*lose%MOD;

    cout << total;

    return 0;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Incorrect 8 ms 3420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 11 ms 3932 KB Output is correct
8 Correct 12 ms 3676 KB Output is correct
9 Correct 8 ms 3640 KB Output is correct
10 Correct 8 ms 3420 KB Output is correct
11 Correct 9 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 11 ms 3932 KB Output is correct
8 Correct 12 ms 3676 KB Output is correct
9 Correct 8 ms 3640 KB Output is correct
10 Correct 8 ms 3420 KB Output is correct
11 Correct 9 ms 3420 KB Output is correct
12 Execution timed out 1044 ms 13136 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 11 ms 3932 KB Output is correct
8 Correct 12 ms 3676 KB Output is correct
9 Correct 8 ms 3640 KB Output is correct
10 Correct 8 ms 3420 KB Output is correct
11 Correct 9 ms 3420 KB Output is correct
12 Correct 1 ms 3420 KB Output is correct
13 Incorrect 9 ms 3420 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 1 ms 3420 KB Output is correct
5 Correct 1 ms 3420 KB Output is correct
6 Correct 1 ms 3420 KB Output is correct
7 Correct 11 ms 3932 KB Output is correct
8 Correct 12 ms 3676 KB Output is correct
9 Correct 8 ms 3640 KB Output is correct
10 Correct 8 ms 3420 KB Output is correct
11 Correct 9 ms 3420 KB Output is correct
12 Execution timed out 1044 ms 13136 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3416 KB Output is correct
2 Incorrect 8 ms 3420 KB Output isn't correct
3 Halted 0 ms 0 KB -