QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789059 | #7588. Monster Hunter | Maraschino | WA | 2ms | 8236kb | C++14 | 2.9kb | 2024-11-27 19:11:27 | 2024-11-27 19:11:27 |
Judging History
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 = 100005;
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 _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);
}
void main() {
fin = stdin, fout = stdout, ferr = stderr;
fprintf(ferr, "%.2lf MB memory\n", 1.0 * (&_1 - &_2) / 1024 / 1024);
i64 Start_Time = clock();
n = read();
for (i32 i = 2; i <= n; ++i) a[i] = read(), b[i] = read();
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);
s.ep(0, 0, 0, 1);
for (i32 i = 2; i <= n; ++i) {
s.ep(Node(a[i], b[i], b[i] - a[i], i));
}
i64 now = 0, ans = 0;
for (i32 i = 1; i <= n; ++i) {
auto [A, B, C, D] = *s.begin();
s.erase(s.begin());
i32 u = D == 1 ? 1 : 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);
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: 0
Wrong Answer
time: 2ms
memory: 8236kb
input:
1 4 2 6 5 4 6 2 1 2 2 3 3 4
output:
0
result:
wrong answer 1st numbers differ - expected: '3', found: '0'