QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#134740#2443. Dense Subgraphckiseki#WA 336ms19764kbC++233.4kb2023-08-04 19:14:132023-08-04 19:14:16

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-04 19:14:16]
  • 评测
  • 测评结果:WA
  • 用时:336ms
  • 内存:19764kb
  • [2023-08-04 19:14:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int cnt = sizeof...(T);
    (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
    cerr << "\e[1;32m[ " << s << " ] = [ ";
    for (int f = 0; L != R; L++)
        cerr << (f++ ? ", " : "") << *L;
    cerr << " ]\e[0m\n";
}
#else 
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

namespace {

constexpr int kN = 35000 + 5;
constexpr int kMod = 1'000'000'007;
constexpr int kD = 1 << 5;

constexpr int add(int a, int b) {
    return a + b >= kMod ? a + b - kMod : a + b;
}
constexpr int mul(int64_t a, int64_t b) {
    return static_cast<int>(a * b % kMod);
}

int a[kN];
vector<int> g[kN];

void pre(int u, int f) {
    vector<int> cur;
    for (int v : g[u]) {
        if (v == f)
            continue;
        cur.push_back(v);
        pre(v, u);
    }
    g[u] = cur;
}

int sm[kN][kD];
int dp[kN][kD + 1];
bool bad[kN][kD];

int dfs(int u) {
    const int d = int(g[u].size());
    const int d2 = 1 << d;
    sm[u][0] = 1;
    for (int S = 1; S < d2; ++S) {
        sm[u][S] = a[u];
        for (int i = 0; i < d; ++i) {
            if ((S >> i) & 1) {
                sm[u][S] += a[g[u][i]];
                bad[u][S] |= bad[u][S ^ (1 << i)];
            }
        }
        if (sm[u][S] > 0)
            bad[u][S] = true;
    }

    dp[u][kD] = 1;
    for (int v : g[u]) {
        dp[u][kD] = mul(dp[u][kD], dfs(v));
    }
    vector<int> tot(g[u].size(), 1);
    for (int i = 0; i < d; ++i) {
        const int v = g[u][i];
        const int vd2 = 1 << int(g[v].size());
        for (int j = 0; j < vd2; ++j) {
            if (bad[v][j])
                continue;
            if (sm[v][j] + a[u] > 0)
                continue;
            tot[i] = add(tot[i], dp[v][j]);
        }
    }
    int ret = dp[u][kD];
    for (int S = 0; S < d2; ++S) {
        if (bad[u][S])
            continue;
        dp[u][S] = 1;
        for (int j = 0; j < d; ++j)
            if ((S >> j) & 1)
                dp[u][S] = mul(dp[u][S], tot[j]);
            else
                dp[u][S] = mul(dp[u][S], dp[g[u][j]][kD]);
        ret = add(ret, dp[u][S]);
    }
    return ret;
}

} // namespace

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int n, x;
        cin >> n >> x;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            a[i] -= x;
        }
        for (int i = 1; i < n; ++i) {
            int u, v;
            cin >> u >> v;
            u -= 1, v -= 1;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        pre(0, 0);
        cout << dfs(0) << '\n';
        for (int i = 0; i < n; ++i) {
            memset(dp[i], 0, sizeof(dp[i]));
            memset(sm[i], 0, sizeof(sm[i]));
            memset(bad[i], 0, sizeof(bad[i]));
            g[i].clear();
        }
    }
    return 0;
}
/*
1
5 0
1 1 1 1 1
1 2
2 3
3 4
4 5
bad[0][1] = true (2)
bad[1][1] = true (2)
bad[2][1] = true (2)
bad[3][1] = true (2)
dp[4][No] = 1
dp[3][No] = 2
dp[3][1] = 0
dp[2][No] = 3
dp[2][1] = 0
dp[1][No] = 4
dp[1][1] = 0
dp[0][No] = 5
dp[0][1] = 0
6
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 336ms
memory: 19764kb

input:

30
10 11086
10189 24947 2265 9138 27104 12453 15173 3048 30054 2382
8 1
1 4
5 10
10 4
3 5
2 10
9 7
6 10
7 1
15 9664
4127 24649 13571 8586 34629 8644 3157 33133 3713 32646 29412 8108 13583 21362 23735
14 9
7 1
15 12
10 15
2 6
3 11
9 1
1 11
6 12
4 10
13 15
8 15
12 11
5 3
20 29310
21738 9421 8412 4617 ...

output:

450
5364
19726535
294740511
118460261
16516561
982546920
693779621
854416779
298356159
126350193
54491238
363658212
870363550
143654403
245576466
397536613
227932396
500991977
425027811
21969226
537701709
809639814
260026240
623226811
403072730
435549359
928409206
113357133
174333568

result:

wrong answer 1st lines differ - expected: '320', found: '450'