QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#134742 | #2443. Dense Subgraph | ckiseki# | WA | 308ms | 19744kb | C++23 | 3.4kb | 2023-08-04 19:18:00 | 2023-08-04 19:18:00 |
Judging History
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] = a[u];
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: 308ms
memory: 19744kb
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 70955965 913828964 536513795 526014083 610528261 688981161 923253933 628414152 829024675 428298398 966163780 134764987 576027762 256146050 176214378 324605399 748830903 456404434 531820113 731779971 164062115 106595811 351573627 458358206 500281102 729404163 871001108
result:
wrong answer 1st lines differ - expected: '320', found: '450'