QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134744 | #2443. Dense Subgraph | ckiseki# | AC ✓ | 330ms | 21228kb | C++23 | 3.6kb | 2023-08-04 19:24:59 | 2023-08-04 19:25:03 |
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());
for (int i = 0; i < d; ++i) {
const int v = g[u][i];
const int vd2 = 1 << int(g[v].size());
vector<bool> badbad(vd2);
for (int j = 0; j < vd2; ++j) {
badbad[j] = badbad[j] | bad[v][j];
badbad[j] = badbad[j] | bool(sm[v][j] + a[u] > 0);
for (int k = 0; k < int(g[v].size()); ++k)
if ((j >> k) & 1)
badbad[j] = badbad[j] | bool(badbad[j ^ (1 << k)]);
if (badbad[j])
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
*/
详细
Test #1:
score: 100
Accepted
time: 330ms
memory: 21228kb
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:
320 3312 1048576 60461799 663660496 831386303 528321417 945912389 820338255 58962594 643816787 354510769 532631871 280169661 533022884 475656636 892230988 381315031 40006857 376652471 288541404 546717513 976236489 615823216 419541488 840899550 546169620 634600456 825179565 169850560
result:
ok 30 lines