답안 #495512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495512 2021-12-19T08:19:05 Z LittleCube Star Trek (CEOI20_startrek) C++14
100 / 100
135 ms 18604 KB
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

const ll MOD = 1000000007;

ll N, D, dp[100005], pre[100005], subtree[100005], crit[100005], L, K, dpL[100005], Npow[100005];
vector<int> E[100005];

void dfs(int k)
{
    for (int i : E[k])
        if (i != pre[k])
        {
            pre[i] = k;
            dfs(i);
            if (dp[i] == 0)
                dp[k]++;
        }
}

ll fastpow(ll b, ll p)
{
    ll a = 1;
    while (p > 0)
    {
        if (p & 1)
            a = a * b % MOD;
        b = b * b % MOD;
        p >>= 1;
    }
    return a;
}

void dfs2(int k, bool w)
{
    //cout << "dfs2 " << k << '\n';
    if (w && dp[k] == 1)
        for (int i : E[k])
        {
            if (i != pre[k])
            {
                if (dp[i] == 0)
                {
                    dfs2(i, false);
                    subtree[k] += subtree[i];
                }
                else
                    dfs2(i, true);
            }
        }
    else if (w)
        for (int i : E[k])
        {
            if (i != pre[k])
                dfs2(i, dp[i] != 0);
        }
    else if (!w)
    {
        subtree[k]++;
        for (int i : E[k])
            if (i != pre[k])
            {
                dfs2(i, true);
                subtree[k] += subtree[i];
            }
    }
}

void reroot(int k)
{
    //cout << "reroot " << k << '\n';
    //for (int i = 1; i <= N; i++)
    //    cout << subtree[i] << " \n"[i == N];
    //for (int i = 1; i <= N; i++)
    //    cout << dp[i] << " \n"[i == N];
    int sum = 0;
    if (dp[k] == 0)
        crit[k] = 1;
    for (int i : E[k])
    {
        if (dp[k] == 1 && dp[i] == 0)
            crit[k] += subtree[i];
        else if (dp[k] == 0)
            crit[k] += subtree[i];

        if (dp[k] == 2 && dp[i] == 0)
            sum += subtree[i];
        else if (dp[k] == 1 && dp[i] == 1)
            sum += subtree[i];
    }
    swap(subtree[k], crit[k]);
    for (int i : E[k])
        if (i != pre[k])
        {
            if (dp[k] == 1 && dp[i] == 0)
            {
                ll tmp = sum + 1;
                swap(tmp, subtree[k]);
                dp[k]--, dp[i]++;
                reroot(i);
                dp[k]++;
                swap(tmp, subtree[k]);
            }
            else if (dp[k] >= 2 && dp[i] == 0)
            {
                ll tmp = (dp[k] == 2 ? sum - subtree[i] : 0);
                swap(tmp, subtree[k]);
                dp[k]--;
                reroot(i);
                dp[k]++;
                swap(tmp, subtree[k]);
            }
            else if (dp[k] == 0)
            {
                ll tmp = subtree[k] - subtree[i];
                swap(tmp, subtree[k]);
                dp[i]++;
                reroot(i);
                swap(tmp, subtree[k]);
            }
            else
                reroot(i);
        }
    swap(subtree[k], crit[k]);
}

signed main()
{
    cin >> N >> D;
    for (int i = 1; i < N; i++)
    {
        int u, v;
        cin >> u >> v;
        E[u].emplace_back(v);
        E[v].emplace_back(u);
    }

    dfs(1);
    dfs2(1, dp[1] != 0);
    reroot(1);
    //for (int i = 1; i <= N; i++)
    //    cout << crit[i] << " \n"[i == N];
    //for (int i = 1; i <= N; i++)
    //    cout << dp[i] << " \n"[i == N];
    for (int i = 1; i <= N; i++)
        if (dp[i] == 0)
            L++, K = (K - crit[i] + MOD) % MOD;
        else
            K = (K + crit[i] + MOD) % MOD;
    ll res = L * (((fastpow(N, D * 2) - fastpow(K, D) + MOD) % MOD) * fastpow((N * N - K + MOD) % MOD, MOD - 2) % MOD) % MOD;
    if (dp[1] == 0)
        cout << (res * crit[1]) % MOD << '\n';
    else
        cout << (fastpow(N, D * 2) - (res * crit[1] % MOD) + MOD) % MOD << '\n';
}
/*
7 1
1 2
2 3
3 4
4 5
5 6
6 7
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 101 ms 13408 KB Output is correct
13 Correct 121 ms 18604 KB Output is correct
14 Correct 83 ms 9280 KB Output is correct
15 Correct 110 ms 9192 KB Output is correct
16 Correct 98 ms 9120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 3 ms 2636 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 2 ms 2656 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 1 ms 2636 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 4 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 3 ms 2764 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2656 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 101 ms 13408 KB Output is correct
13 Correct 121 ms 18604 KB Output is correct
14 Correct 83 ms 9280 KB Output is correct
15 Correct 110 ms 9192 KB Output is correct
16 Correct 98 ms 9120 KB Output is correct
17 Correct 3 ms 2636 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 2 ms 2656 KB Output is correct
22 Correct 2 ms 2648 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2648 KB Output is correct
25 Correct 1 ms 2636 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 4 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2656 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 112 ms 13412 KB Output is correct
37 Correct 127 ms 18476 KB Output is correct
38 Correct 84 ms 9320 KB Output is correct
39 Correct 107 ms 9184 KB Output is correct
40 Correct 104 ms 9184 KB Output is correct
41 Correct 135 ms 16276 KB Output is correct
42 Correct 101 ms 17148 KB Output is correct
43 Correct 67 ms 8512 KB Output is correct
44 Correct 94 ms 9136 KB Output is correct
45 Correct 99 ms 9160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 3 ms 2764 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 101 ms 13408 KB Output is correct
20 Correct 121 ms 18604 KB Output is correct
21 Correct 83 ms 9280 KB Output is correct
22 Correct 110 ms 9192 KB Output is correct
23 Correct 98 ms 9120 KB Output is correct
24 Correct 3 ms 2636 KB Output is correct
25 Correct 3 ms 2652 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 2 ms 2656 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2648 KB Output is correct
32 Correct 1 ms 2636 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 4 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2764 KB Output is correct
39 Correct 3 ms 2764 KB Output is correct
40 Correct 2 ms 2636 KB Output is correct
41 Correct 2 ms 2656 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 112 ms 13412 KB Output is correct
44 Correct 127 ms 18476 KB Output is correct
45 Correct 84 ms 9320 KB Output is correct
46 Correct 107 ms 9184 KB Output is correct
47 Correct 104 ms 9184 KB Output is correct
48 Correct 135 ms 16276 KB Output is correct
49 Correct 101 ms 17148 KB Output is correct
50 Correct 67 ms 8512 KB Output is correct
51 Correct 94 ms 9136 KB Output is correct
52 Correct 99 ms 9160 KB Output is correct
53 Correct 118 ms 18596 KB Output is correct
54 Correct 109 ms 16616 KB Output is correct
55 Correct 63 ms 8136 KB Output is correct
56 Correct 105 ms 13548 KB Output is correct
57 Correct 95 ms 9260 KB Output is correct
58 Correct 97 ms 9256 KB Output is correct
59 Correct 104 ms 9200 KB Output is correct
60 Correct 98 ms 9284 KB Output is correct