QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#619862#2443. Dense SubgraphPlentyOfPenalty#AC ✓304ms16040kbC++203.0kb2024-10-07 15:41:452024-10-07 15:41:46

Judging History

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

  • [2024-10-07 15:41:46]
  • 评测
  • 测评结果:AC
  • 用时:304ms
  • 内存:16040kb
  • [2024-10-07 15:41:45]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
#ifdef memset0
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define endl '\n'
#define log(...) (void(0))
#endif
using namespace std;
using ll = long long;
using lf = long double;
using ull = unsigned long long;
const int N = 35010, M = 5;
const int INF = 1e9;
const int mod = 1e9 + 7;
int T, n, u, v, a[N];
int f[N][(1 << M) + 10], g[N], t[N][2], sz[N];
int lb[(1 << M) + 10], ans;
int mx[(1 << M) + 10];
ll sum[(1 << M) + 10];
vector<int> e[N], s[N];
int Check(int x, int y, int z) { return a[x] + a[y] + a[z] <= 0; }
void Merge(int x, int y) {
  // log("Merge %d %d\n", x, y);
  t[y][0] = t[y][1] = 0;
  sum[0] = a[y] + a[x];
  for (int i = 0; i < (1 << sz[y]); ++i) {
    if (i) {
      sum[i] = sum[i - (1 << lb[i])] + max(0, a[s[y][lb[i]]]);
    }
    (t[y][0] += f[y][i]) >= mod ? t[y][0] -= mod : 0;
    if (sum[i] <= 0) (t[y][1] += f[y][i]) >= mod ? t[y][1] -= mod : 0;
  }
  // log("CHK %d %d=%d\n", x, y, Check(x, y, 0));
  // log("T %d 0=%d,1=%d\n", y, t[y][0], t[y][1]);
}
void dfs(int x, int fa) {
  // log("DFS %d\n", x);
  sz[x] = 0;
  for (int i : e[x])
    if (i != fa) {
      ++sz[x];
      s[x].push_back(i);
      dfs(i, x);
      Merge(x, i);
    }
  assert(0 <= sz[x] && sz[x] <= 5);
  // log("SOLVE %d\n", x);
  g[x] = 1;
  for (int i : s[x]) {
    g[x] = 1ll * g[x] * (g[i] + t[i][0]) % mod;
  }
  sum[0] = a[x];
  mx[0] = -INF;
  for (int i = 0; i < (1 << sz[x]); ++i) {
    if (i) {
      sum[i] = sum[i - (1 << lb[i])] + max(0, a[s[x][lb[i]]]);
      mx[i] = max(mx[i - (1 << lb[i])], a[s[x][lb[i]]]);
    }
    // log("TG %d=%d\n", i, tg[i]);
    f[x][i] = (sum[i] + (mx[i] <= 0 ? mx[i] : 0) <= 0);
    for (int j = 0; j < sz[x] && f[x][i]; ++j) {
      if (i & (1 << j)) {
        f[x][i] = 1ll * f[x][i] * t[s[x][j]][1] % mod;
      } else {
        f[x][i] = 1ll * f[x][i] * g[s[x][j]] % mod;
      }
    }
  }
  // for (int i = 0; i < (1 << sz[x]); ++i) {
  //   log("F %d %d=%d\n", x, i, f[x][i]);
  // }
  // log("G %d=%d\n", x, g[x]);
}
#define clear(a) memset(a, 0, sizeof(a))
int main() {
#ifdef memset0
  freopen("F.in", "r", stdin);
  // freopen("F0.txt", "r", stdin);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin >> T;
  for (int i = 1; i < (1 << M); ++i) lb[i] = ((i & 1) ? 0 : lb[i >> 1] + 1);
  // for (int i = 1; i < (1 << M); i++) lb("lb[%d] = %d\n", i, lb[i]);
  while (T--) {
    clear(a);
    clear(f);
    clear(g);
    clear(t);
    clear(sz);
    // log("-------------------T=%d\n", T);
    cin >> n >> v;
    for (int i = 1; i <= n; ++i) cin >> a[i], a[i] -= v;
    for (int i = 1; i <= n; ++i) {
      vector<int>().swap(e[i]);
      vector<int>().swap(s[i]);
    }
    for (int i = 1; i < n; ++i) {
      cin >> u >> v;
      e[u].push_back(v), e[v].push_back(u);
    }
    dfs(1, 0);
    ll ans = g[1];
    for (int i = 0; i < (1 << sz[1]); ++i) {
      ans += f[1][i];
    }
    cout << (ans % mod) << endl;
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 304ms
memory: 16040kb

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