답안 #557574

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557574 2022-05-05T13:42:14 Z status_coding Star Trek (CEOI20_startrek) C++14
30 / 100
1000 ms 13368 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];

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';
    */

    if(d == 1)
    {
        long long nrF = 0;

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

        long long ans = 0;

        if(dp[1])
        {
            ans = nrCritF[1] * (n - nrF);
            ans %= mod;

            ans += (n-nrCritF[1]) * n;
            ans %= mod;
        }
        else
        {
            ans = nrCritF[1] * nrF;
            ans %= mod;
        }

        cout<<ans;
        return 0;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 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 110 ms 10880 KB Output is correct
13 Correct 112 ms 13368 KB Output is correct
14 Execution timed out 1094 ms 8664 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 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 2 ms 2644 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2660 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2660 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 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 110 ms 10880 KB Output is correct
13 Correct 112 ms 13368 KB Output is correct
14 Execution timed out 1094 ms 8664 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -