QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#501468#7607. The Doubling Game 2pandapythonerWA 0ms3868kbC++233.6kb2024-08-02 19:10:102024-08-02 19:10:10

Judging History

你现在查看的是最新测评结果

  • [2024-08-02 19:10:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3868kb
  • [2024-08-02 19:10:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;


#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < n; i += 1)
#define len(a) ((int)(a).size())


const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 1e9 + 7;


int n;
vector<vector<int>> g;
vector<ll> dp;
vector<vector<ll>> dpup, dpdown;

void dfs(int v, int p) {
    rep(i, len(g[v])) {
        if (g[v][i] == p) {
            g[v].erase(g[v].begin() + i);
            break;
        }
    }
    if (g[v].empty()) {
        dp[v] = 1;
        dpup[v] = { 1 };
        dpdown[v] = { 1 };
        return;
    }
    for (auto to : g[v]) {
        dfs(to, v);
    }
    sort(all(g[v]), [&](int a, int b) {
        return max(len(dpup[a]), len(dpdown[a])) < max(len(dpup[b]), len(dpdown[b]));
        });
    vector<vector<ll>> curdp(2, vector<ll>());
    curdp[0] = { 1 };
    curdp[1] = { 0 };
    int curlen = 0;
    for (auto to : g[v]) {
        int newlen = max(curlen, max(len(dpup[to]), len(dpdown[to])));
        vector<vector<ll>> newdp(2, vector<ll>((1 << newlen), 0));
        rep(b, 2) rep(mask, (1 << curlen)) {
            newdp[b][mask] = (newdp[b][mask] + curdp[b][mask] * dp[to]) % mod;
        }
        rep(b, 2) {
            for (int old_mask = 0; old_mask < (1 << curlen); old_mask += 1) {
                for (int i = 0; i < len(dpup[to]); i += 1) {
                    if ((old_mask >> i) & 1) continue;
                    if (b == 1 and (1 << i) > old_mask) continue;
                    int new_mask = old_mask + (1 << i);
                    newdp[b][new_mask] = (newdp[b][new_mask] + curdp[b][old_mask] * dpup[to][i]) % mod;
                }
            }
        }
        for (int i = 0; i < len(dpdown[to]); i += 1) {
            for (int old_mask = 0; old_mask < (1 << curlen) and old_mask < (1 << i); old_mask += 1) {
                int new_mask = old_mask + (1 << i);
                newdp[1][new_mask] = (newdp[1][new_mask] + curdp[0][old_mask] * dpdown[to][i]) % mod;
            }
        }
        curlen = newlen;
        curdp = newdp;
    }
    for (int mask = 0; mask < (1 << curlen); mask += 1) {
        if (mask + 1 == (1 << __lg(mask + 1))) {
            int i = __lg(mask + 1);
            dp[v] = (dp[v] + curdp[0][mask] + curdp[1][mask]) % mod;
            if (curdp[0][mask] != 0) {
                dpup[v].resize(max(len(dpup[v]), i + 1), 0);
                dpup[v][i] = (dpup[v][i] + curdp[0][mask]) % mod;
                dpdown[v].resize(max(len(dpdown[v]), i + 1), 0);
                dpdown[v][i] = (dpdown[v][i] + curdp[0][mask]) % mod;
            }
            continue;
        }
        assert(mask > 0);
        int i = __lg(mask);
        int diff = (1 << i) - 1 - mask;
        assert(diff != 0);
        if (diff != (1 << __lg(diff))) continue;
        int j = __lg(diff);
        if (curdp[0][mask] != 0 or curdp[1][mask] != 0) {
            dpdown[v].resize(max(len(dpdown[v]), j + 1), 0);
            dpdown[v][j] = (dpdown[v][j] + curdp[0][mask] + curdp[1][mask]) % mod;
        }
    }
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n;
    g.assign(n, vector<int>());
    rep(i, n - 1) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dp.assign(n, 0);
    dpup.assign(n, vector<ll>());
    dpdown.assign(n, vector<ll>());
    dfs(0, -1);
    cout << dp[0] << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

5
1 2
1 3
1 4
4 5

output:

21

result:

ok single line: '21'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3868kb

input:

128
11 32
116 81
65 4
117 47
5 81
104 30
61 8
82 59
95 20
92 29
29 127
97 39
123 33
59 128
115 33
83 67
74 16
77 33
64 73
124 123
8 127
61 51
101 122
35 90
119 116
112 27
81 93
109 123
54 1
119 100
116 16
65 47
67 27
22 105
76 87
36 39
27 96
72 31
91 123
21 105
118 12
110 48
121 72
14 115
24 16
106 ...

output:

321490425

result:

wrong answer 1st lines differ - expected: '508800953', found: '321490425'