QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#789156#7588. Monster HunterMaraschinoML 4ms37408kbC++143.1kb2024-11-27 19:25:372024-11-27 19:25:38

Judging History

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

  • [2024-11-27 19:25:38]
  • 评测
  • 测评结果:ML
  • 用时:4ms
  • 内存:37408kb
  • [2024-11-27 19:25:37]
  • 提交

answer

#include <bits/stdc++.h>
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
#define ep emplace
#define eb emplace_back
#define all(x) x.begin(), x.end()
FILE *fin, *fout, *ferr;
i32 read() {
  i32 t = 0, f = 0;
  char ch = fgetc(fin);
  for (; !isdigit(ch); ch = fgetc(fin)) f ^= (ch == '-');
  for (; isdigit(ch); ch = fgetc(fin)) t = (t << 1) + (t << 3) + (ch ^ 48);
  return f ? -t : t;
}
template<typename T1, typename T2>
constexpr bool chkmax(T1 &a, T2 b) { return a > b ? false : (a = b, true); } 
template<typename T1, typename T2>
constexpr bool chkmin(T1 &a, T2 b) { return a > b ? (a = b, true) : false; } 
namespace Solution_Of_C24204 {
  bool _1;
  static const i32 N = 1000005;
  i32 n;
  struct Node {
    i64 a, b, c, d;
    Node() = default;
    Node(i64 a, i64 b, i64 c, i32 d): a(a), b(b), c(c), d(d) {}
    bool operator<(const Node &a) const {
      if ((this->c <= 0) != (a.c <= 0)) return this->c > a.c;
      if (this->c < 0) return this->b != a.b ? this->b > a.b : (this->a != a.a ? this->a < a.a : this->d < a.d);
      if (this->c >= 0) return this->a != a.a ? this->a < a.a : (this->b != a.b ? this->b > a.b : this->d < a.d); 
      assert(0);
      return true;
    }
  };
  i64 a[N], b[N];
  i32 fa[N], fat[N], vis[N];
  std::vector<i32> e[N];
  std::set<Node> s;
  bool flag;
  bool _2;
  i32 find(i32 x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
  void dfs(i32 x, i32 f) {
    fat[x] = f;
    for (i32 y : e[x])
      if (y != f)
        dfs(y, x);
    return void();
  }
  void solve() {
    n = read();
    for (i32 i = 2; i <= n; ++i) a[i] = read(), b[i] = read();
    for (i32 i = 1; i <= n; ++i) vis[i] = false;
    vis[1] = true;
    for (i32 i = 1; i <= n; ++i) fa[i] = i;
    for (i32 i = 1; i < n; ++i) {
      static i32 u, v;
      u = read(), v = read();
      e[u].eb(v), e[v].eb(u);
    }
    dfs(1, 0);
    for (i32 i = 2; i <= n; ++i) s.ep(Node(a[i], b[i], b[i] - a[i], i));
    i64 now = 0, ans = 0;
    if (flag) exit(0);
    for (i32 i = 1; i < n; ++i) {
      auto [A, B, C, D] = *s.begin();
      s.erase(s.begin());
      i32 u = find(fat[D]);
      if (vis[u]) {
        now += A;
        ans = std::max(ans, now);
        now -= B;
        vis[D] = true;
      } else {
        fa[D] = u;
        s.erase(s.find({a[u], b[u], b[u] - a[u], u}));
        if (A > b[u]) {
          a[u] = a[u] + A - b[u];
          b[u] = B;
        } else {
          a[u] = a[u];
          b[u] = B + b[u] - A;
        }
        s.ep(a[u], b[u], b[u] - a[u], u);
      }
    }
    fprintf(fout, "%lld\n", ans);
    return void();
  }
  void main() {
    fin = stdin, fout = stdout, ferr = stderr;
    fprintf(ferr, "%.2lf MB memory\n", 1.0 * (&_1 - &_2) / 1024 / 1024);
    i64 Start_Time = clock();
    i32 t = read();
    while (t--) {
      solve();
      flag = true;
    }
    i64 End_Time = clock();
    fprintf(ferr, "%lld ms time\n", End_Time - Start_Time);
    return void();
  }
}
signed main() { return Solution_Of_C24204::main(), 0; }

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 37408kb

input:

1
4
2 6
5 4
6 2
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Memory Limit Exceeded

input:

10
50000
112474 198604
847262 632597
871374 962220
971398 284540
10360 695245
43233 569941
64019 203468
598469 997911
41841 657552
333198 936119
546370 180011
58831 901040
959767 396595
710441 277461
429299 209831
494164 138327
393982 581911
993164 617488
108113 160310
55841 611360
301395 291074
149...

output:


result: