답안 #999446

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999446 2024-06-15T13:53:42 Z yoav_s Star Trek (CEOI20_startrek) C++17
100 / 100
241 ms 93008 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;
    }
}

vv multiply(vv a, vv b)
{
    vv res(a.size(), v(b[0].size()));
    for (ll i=  0; i < a.size(); i++)
    {
        for (ll j = 0; j < b[0].size(); j++)
        {
            for (ll k = 0; k < a[0].size(); k++) res[i][j] += (a[i][k] * b[k][j]) % mod;
            res[i][j] %= mod;
        }
    }
    return res;
}

vv exponent(vv a, ll exp)
{
    if (exp == 0) return {{1,0},{0,1}};
    vv res = exponent(a, exp / 2);
    res = multiply(res, res);
    if (exp % 2 == 1) res = multiply(res, a);
    return res;
}

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;
    vv matrix{{sum.s, sum.f},{0, (N * N) % mod}};
    vv cur = {{weighted}, {absolute}};
    vv exponented = exponent(matrix, D - 1);
    vv res = multiply(exponented, cur);
    cout << (res[0][0] * amountOfWaysToGet[0][1].s + res[1][0] * amountOfWaysToGet[0][1].f) % mod << "\n";
    return 0;
}

Compilation message

startrek.cpp: In function 'vv multiply(vv, vv)':
startrek.cpp:240:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  240 |     for (ll i=  0; i < a.size(); i++)
      |                    ~~^~~~~~~~~~
startrek.cpp:242:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  242 |         for (ll j = 0; j < b[0].size(); j++)
      |                        ~~^~~~~~~~~~~~~
startrek.cpp:244:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  244 |             for (ll k = 0; k < a[0].size(); k++) res[i][j] += (a[i][k] * b[k][j]) % mod;
      |                            ~~^~~~~~~~~~~~~
startrek.cpp: In function 'int main()':
startrek.cpp:287:26: warning: unused variable 'losingCount' [-Wunused-variable]
  287 |     ll winningCount = 0, losingCount = 0;
      |                          ^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 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 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 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 0 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1368 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 1 ms 348 KB Output is correct
2 Correct 0 ms 344 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 0 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1368 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 178 ms 61780 KB Output is correct
13 Correct 184 ms 91880 KB Output is correct
14 Correct 113 ms 35916 KB Output is correct
15 Correct 141 ms 36168 KB Output is correct
16 Correct 145 ms 36432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 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 0 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1368 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 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 1 ms 1116 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 1 ms 600 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 1 ms 348 KB Output is correct
2 Correct 0 ms 344 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 0 ms 348 KB Output is correct
7 Correct 1 ms 1116 KB Output is correct
8 Correct 1 ms 1368 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 178 ms 61780 KB Output is correct
13 Correct 184 ms 91880 KB Output is correct
14 Correct 113 ms 35916 KB Output is correct
15 Correct 141 ms 36168 KB Output is correct
16 Correct 145 ms 36432 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 1116 KB Output is correct
27 Correct 1 ms 1116 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 768 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 170 ms 61704 KB Output is correct
37 Correct 203 ms 92024 KB Output is correct
38 Correct 137 ms 36092 KB Output is correct
39 Correct 145 ms 36064 KB Output is correct
40 Correct 163 ms 36268 KB Output is correct
41 Correct 180 ms 77600 KB Output is correct
42 Correct 174 ms 84052 KB Output is correct
43 Correct 132 ms 31944 KB Output is correct
44 Correct 110 ms 36456 KB Output is correct
45 Correct 121 ms 35948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 1116 KB Output is correct
15 Correct 1 ms 1368 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 178 ms 61780 KB Output is correct
20 Correct 184 ms 91880 KB Output is correct
21 Correct 113 ms 35916 KB Output is correct
22 Correct 141 ms 36168 KB Output is correct
23 Correct 145 ms 36432 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 1116 KB Output is correct
34 Correct 1 ms 1116 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 768 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 1 ms 860 KB Output is correct
39 Correct 2 ms 1372 KB Output is correct
40 Correct 1 ms 600 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 170 ms 61704 KB Output is correct
44 Correct 203 ms 92024 KB Output is correct
45 Correct 137 ms 36092 KB Output is correct
46 Correct 145 ms 36064 KB Output is correct
47 Correct 163 ms 36268 KB Output is correct
48 Correct 180 ms 77600 KB Output is correct
49 Correct 174 ms 84052 KB Output is correct
50 Correct 132 ms 31944 KB Output is correct
51 Correct 110 ms 36456 KB Output is correct
52 Correct 121 ms 35948 KB Output is correct
53 Correct 241 ms 93008 KB Output is correct
54 Correct 199 ms 81664 KB Output is correct
55 Correct 69 ms 29644 KB Output is correct
56 Correct 191 ms 63276 KB Output is correct
57 Correct 137 ms 37736 KB Output is correct
58 Correct 128 ms 37716 KB Output is correct
59 Correct 173 ms 37204 KB Output is correct
60 Correct 128 ms 37052 KB Output is correct