QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#404015#7588. Monster HunterAndycipationRE 1ms3532kbC++202.3kb2024-05-03 08:35:182024-05-03 08:35:19

Judging History

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

  • [2024-05-03 08:35:19]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3532kb
  • [2024-05-03 08:35:18]
  • 提交

answer

/*
 * author:  ADMathNoob
 * created: 05/02/24 19:03:49
 * problem: https://qoj.ac/problem/7588#tab-statement
 */

/*
Comments about problem:


*/

#include <bits/stdc++.h>

using namespace std;

#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  
  auto SolveTestCase = [&]() -> void {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 1; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      --x; --y;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    
    vector<int> pv(n);
    function<void(int, int)> Dfs = [&](int v, int p) {
      pv[v] = p;
      for (int u : g[v]) {
        if (u == p) {
          continue;
        }
        Dfs(u, v);
      }
    };
    Dfs(0, -1);
    
    auto Cmp = [&](int i, int j) {
      // bool goodI = (b[i] >= a[i]);
      // bool goodJ = (b[j] >= a[j]);
      // if (goodI != goodJ) {
      //   return goodI;
      // }
      // if (goodI) {
      //   return (a[i] < a[j]);
      // }
      // return (b[i] > b[j]);
      
      int need_ij = max(a[i], a[i] - b[i] + a[j]);
      int need_ji = max(a[j], a[j] - b[j] + a[i]);
      if (need_ij != need_ji) {
        return need_ij < need_ji;
      }
      return i < j;
    };
    
    set<int, decltype(Cmp)> s(Cmp);
    for (int i = 1; i < n; i++) {
      s.insert(i);
    }
    vector<int> head(n);
    iota(head.begin(), head.end(), 0);
    function<int(int)> Get = [&](int x) {
      if (x == head[x]) return x;
      return head[x] = Get(head[x]);
    };
    for (int it = 0; it < n - 1; it++) {
      int v = *s.begin();
      s.erase(s.find(v));
      int p = Get(pv[v]);
      if (p != 0) {
        s.erase(s.find(p));
      }
      int newAp = max(a[p], a[p] - b[p] + a[v]);
      int newBp = -a[p] + b[p] - a[v] + b[v] + newAp;
      a[p] = newAp;
      b[p] = newBp;
      head[v] = p;
      if (p != 0) {
        s.insert(p);
      }
    }
    // assert(s.empty());
    cout << a[0] << '\n';
  };
  
  {
    int tt;
    cin >> tt;
    for (int qq = 1; qq <= tt; qq++) {
      SolveTestCase();
    }
  }
  
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3532kb

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
Runtime Error

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: