답안 #999442

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999442 2024-06-15T13:49:04 Z yoav_s Star Trek (CEOI20_startrek) C++17
78 / 100
1000 ms 93184 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll,ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()

const ll INF = 1e18;
const ll mod = 1e9 + 7;

ll modPow(ll a, ll b)
{
    if (b == 0) return 1;
    ll res = modPow(a, b / 2);
    res = (res * res) % mod;
    if (b % 2 == 1) res = (res * a) % mod;
    return res;
}

void getWinningStates(ll i, vv &graph, vb &winning, vb &visited, v &losingChildrenCount, vector<set<ll>> &losingChildren)
{
    if (visited[i]) return;
    visited[i] = true;
    for (ll x : graph[i])
    {
        if (!visited[x])
        {
            getWinningStates(x, graph, winning, visited, losingChildrenCount, losingChildren);
            if (!winning[x])
            {
                winning[i] = true;
                losingChildrenCount[i]++;
                losingChildren[i].insert(x);
            }
        }
    }
}

ll getSubtreeSize(ll i, vv &graph, v &res)
{
    if (res[i] != 0) return 0;
    res[i] = 1;
    for (ll x : graph[i]) res[i] += getSubtreeSize(x, graph, res);
    return res[i];
}

p operator+(p a, p b)
{
    return p(a.f + b.f, a.s + b.s);
}

void operator+=(p &a, p b)
{
    a.f += b.f; a.s += b.s;
}

p operator-(p a, p b)
{
    return p(a.f - b.f, a.s - b.s);
}

void operator-=(p &a, p b)
{
    a.f -= b.f;
    a.s -= b.s;
}

p operator%(p a, ll mod)
{
    return p(((a.f % mod) + mod) % mod, ((a.s % mod) + mod) % mod);
}

void operator%=(p &a, ll mod)
{
    a = a % mod;
}

void solve(ll i, vv &graph, vb &isWinning, vvp &res, vb &visited, v &subtreeSize, vvp &childrenS)
{
    ll N = graph.size();
    visited[i] = true;
    for (ll x : graph[i])
        if (!visited[x])
        {
            solve(x, graph, isWinning, res, visited, subtreeSize, childrenS);
            childrenS[i][0] += res[x][0];
            childrenS[i][1] += res[x][1];
        }

    ll losingChildren = 0;
    ll losingChild;
    for (ll x : graph[i])
    {
        if (!visited[x] && !isWinning[x])
        {
            losingChildren++;
            losingChild = x;
        }
    }
    if (losingChildren == 0)
    {
        res[i][1] = p(N, mod - 1) + childrenS[i][0];
        res[i][0] = p(0, 1) + childrenS[i][1];
        res[i][0] %= mod;
    }
    else if (losingChildren == 1)
    {
        res[i][1] = p((N * (subtreeSize[i] - subtreeSize[losingChild])) % mod, 0);
        res[i][1] += res[losingChild][0];
        res[i][0] = res[losingChild][1];
    }
    else
    {
        res[i][0] = p(0, 0);
        res[i][1] = p((N * subtreeSize[i]) % mod, 0);
    }
    res[i][1] %= mod;
    visited[i] = false;
}

void rerootSolve(ll i, ll par, vv &graph, vvp &res, vb &visited, v &subtreeSize, vvp &childrenS, vvp &curRootDP, vb &isWinningAsRoot, v &losingChildrenCount, vector<set<ll>> &losingChildren)
{
    ll N = graph.size();
    if (visited[i]) return;
    visited[i] = true;
    vp parChildrenS(2),myChildrenS(2),parCurRootDP(2),myCurRootDP(2);
    if (par != -1)
    {
        copy(all(childrenS[par]), parChildrenS.begin());
        copy(all(childrenS[i]), myChildrenS.begin());
        copy(all(curRootDP[par]), parCurRootDP.begin());
        copy(all(curRootDP[i]), myCurRootDP.begin());
        if (losingChildrenCount[i] == 0)
        {
            losingChildrenCount[par]--;
            losingChildren[par].erase(i);
        }
        if (losingChildrenCount[par] == 0)
        {
            losingChildrenCount[i]++;
            losingChildren[i].insert(par);
        }
        subtreeSize[par] -= subtreeSize[i];
        subtreeSize[i] += subtreeSize[par];
        childrenS[par][0] -= curRootDP[i][0];
        childrenS[par][1] -= curRootDP[i][1];
        childrenS[par][1] %= mod;
        childrenS[par][0] %= mod;
        if (losingChildrenCount[par] == 0)
        {
            curRootDP[par][1] = p(N, mod - 1) + childrenS[par][0];
            curRootDP[par][0] = p(0, 1) + childrenS[par][1];
            curRootDP[par][0] %= mod;
        }
        else if (losingChildrenCount[par] == 1)
        {
            ll losingChild = *losingChildren[par].begin();
            curRootDP[par][1] = p((N * (subtreeSize[par] - subtreeSize[losingChild])) % mod, 0);
            curRootDP[par][1] += curRootDP[losingChild][0];
            curRootDP[par][0] = curRootDP[losingChild][1];
        }
        else
        {
            curRootDP[par][0] = p(0, 0);
            curRootDP[par][1] = p((N * subtreeSize[par]) % mod, 0);
        }
        curRootDP[par][1] %= mod;
        childrenS[i][0] += curRootDP[par][0];
        childrenS[i][1] += curRootDP[par][1];
        if (losingChildrenCount[i] == 0)
        {
            curRootDP[i][1] = p(N, mod - 1) + childrenS[i][0];
            curRootDP[i][0] = p(0, 1) + childrenS[i][1];
            curRootDP[i][0] %= mod;
        }
        else if (losingChildrenCount[i] == 1)
        {
            ll losingChild = *losingChildren[i].begin();
            curRootDP[i][1] = p((N * (subtreeSize[i] - subtreeSize[losingChild])) % mod, 0);
            curRootDP[i][1] += curRootDP[losingChild][0];
            curRootDP[i][0] = curRootDP[losingChild][1];
        }
        else
        {
            curRootDP[i][0] = p(0, 0);
            curRootDP[i][1] = p((N * subtreeSize[i]) % mod, 0);
        }
        curRootDP[i][1] %= mod;
    }
    isWinningAsRoot[i] = losingChildrenCount[i] > 0;
    res[i][0] = curRootDP[i][0];
    res[i][1] = curRootDP[i][1];
    for (ll x : graph[i])
    {
        rerootSolve(x, i, graph, res, visited, subtreeSize, childrenS, curRootDP, isWinningAsRoot, losingChildrenCount, losingChildren);;
    }
    if (par != -1)
    {
        if (losingChildrenCount[par] == 0)
        {
            losingChildrenCount[i]--;
            losingChildren[i].erase(par);
        }
        if (losingChildrenCount[i] == 0)
        {
            losingChildrenCount[par]++;
            losingChildren[par].insert(i);
        }
        subtreeSize[i] -= subtreeSize[par];
        subtreeSize[par] += subtreeSize[i];
        childrenS[par] = parChildrenS;
        childrenS[i] = myChildrenS;
        curRootDP[par] = parCurRootDP;
        curRootDP[i] = myCurRootDP;
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    long long N, D;
    cin >> N >> D;
    vv graph(N);
    for (ll i=  0; i < N - 1; i++)
    {
        long long a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    vvp amountOfWaysToGet(N, vp(2, p(0, 0)));
    vb isWinning(N), visited(N, false);
    v losingChildrenCount(N, 0);
    vector<set<ll>> losingChildren(N);
    getWinningStates(0, graph, isWinning, visited, losingChildrenCount, losingChildren);
    v subtreeSize(N, 0);
    getSubtreeSize(0, graph, subtreeSize);
    vvp dp(N, vp(2, p(0, 0)));
    visited = vb(N, false);
    vvp childrenS(N, vp(2, p(0, 0)));
    solve(0, graph, isWinning, dp, visited, subtreeSize, childrenS);
    vb winAsRoot(N, false); visited = vb(N, false);
    rerootSolve(0, -1, graph, amountOfWaysToGet, visited, subtreeSize, childrenS, dp, winAsRoot, losingChildrenCount, losingChildren);
    ll winningCount = 0, losingCount = 0;
    for (auto x : winAsRoot)
    {
        if (x) winningCount++;
    }
    p sum = p(0, 0);
    for (auto x : amountOfWaysToGet) sum += x[1];
    sum %= mod;
    ll weighted = winningCount, absolute = 1;
    for (ll i = 1; i < D; i++)
    {
        weighted = sum.f * absolute + sum.s * weighted;
        weighted %= mod;
        absolute *= (N * N) % mod;
        absolute %= mod;
    }
    cout << (weighted * amountOfWaysToGet[0][1].s + absolute * amountOfWaysToGet[0][1].f) % mod << "\n";
    return 0;
}

Compilation message

startrek.cpp: In function 'int main()':
startrek.cpp:264:26: warning: unused variable 'losingCount' [-Wunused-variable]
  264 |     ll winningCount = 0, losingCount = 0;
      |                          ^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 1090 ms 348 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 162 ms 61572 KB Output is correct
13 Correct 174 ms 91984 KB Output is correct
14 Correct 115 ms 35916 KB Output is correct
15 Correct 113 ms 36180 KB Output is correct
16 Correct 131 ms 36420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 2 ms 1116 KB Output is correct
22 Correct 2 ms 1112 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 162 ms 61572 KB Output is correct
13 Correct 174 ms 91984 KB Output is correct
14 Correct 115 ms 35916 KB Output is correct
15 Correct 113 ms 36180 KB Output is correct
16 Correct 131 ms 36420 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 2 ms 1112 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 1372 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 158 ms 62736 KB Output is correct
37 Correct 195 ms 93184 KB Output is correct
38 Correct 107 ms 37064 KB Output is correct
39 Correct 121 ms 37204 KB Output is correct
40 Correct 145 ms 37488 KB Output is correct
41 Correct 180 ms 78764 KB Output is correct
42 Correct 159 ms 84976 KB Output is correct
43 Correct 99 ms 32644 KB Output is correct
44 Correct 128 ms 37608 KB Output is correct
45 Correct 126 ms 37204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Execution timed out 1090 ms 348 KB Time limit exceeded
6 Halted 0 ms 0 KB -