이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |