QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789024 | #7588. Monster Hunter | shengxuanyi | WA | 337ms | 22364kb | C++14 | 2.6kb | 2024-11-27 19:05:13 | 2024-11-27 19:05:14 |
Judging History
answer
#include <algorithm>
#include <stdio.h>
#include <assert.h>
#include <queue>
#define int long long
#define ll long long
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
#define _N 4194304
char obuf[_N], * p3 = obuf; char ibuf[_N], * p1 = ibuf, * p2 = ibuf;
inline void flush() { fwrite(obuf, p3 - obuf, 1, stdout); p3 = obuf; }
inline void putch(char x) { (p3 - obuf < _N) ? (*p3++ = x) : (flush(), *p3++ = x); }
inline char getch() { return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, _N, stdin), p1 == p2) ? EOF : *p1++; }
inline void read() { } template < typename T, typename ... T2 > inline void read(T& x, T2& ... oth)
{
char ch = getch(); while (ch < '0' || ch > '9') { if (ch == '-') { read(x); x = -x; return ; } ch = getch(); }
x = 0; while (ch <= '9' && ch >= '0') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getch(); read(oth...);
}
inline void writeln(ll x, char end = '\n')
{
if (!x) { putch('0'), putch(end); return ; } if (x < 0) x = -x, putch('-'); int len = 0; static char c[20] = { };
while (x) c[len++] = x % 10 ^ 48, x /= 10; while (len--) putch(c[len]); putch(end);
}
const int N = 1e5 + 5;
int n, x, y, a[N], b[N], na, nb, vis[N], idx; struct Edge { int to, next; }; Edge edge[N << 1]; int head[N], edge_size; inline void add_edge(int from, int to) { edge[++edge_size] = { to, head[from] }, head[from] = edge_size; }
bool del[N]; int fa[N]; void dfs(int x) { for (int i = head[x]; i != 0; i = edge[i].next) { if (edge[i].to ^ fa[x]) fa[edge[i].to] = x, dfs(edge[i].to); } }
struct Node { int id, now, a, b; bool operator < (const Node& s) const { bool lg1 = s.b >= s.a, lg2 = b >= a; return (lg1 > lg2 || lg1 == lg2 && lg1 == true && s.a < a || lg1 == lg2 && lg1 == false && s.b > b); } }; std::priority_queue < Node > g; Node pp;
inline int find(int x) { return del[fa[x]] ? fa[x] = find(fa[x]) : fa[x]; }
inline void ___()
{
read(n); for (int i = 2; i <= n; i++) read(a[i]), read(b[i]); for (int i = 1; i < n; i++) read(x), read(y), add_edge(x, y), add_edge(y, x); dfs(1);
for (int i = 2; i <= n; i++) g.push({ i, 0, a[i], b[i] }); while (!g.empty())
{
pp = g.top(), g.pop(), x = pp.id; if ((pp.now ^ vis[x]) || del[x]) continue; y = find(x);
na = max(a[y], a[y] - b[y] + a[x]), nb = b[x] + b[y] - a[x] - a[y] + na; a[y] = na, b[y] = nb, del[x] = true;
if (y > 1) g.push({ y, vis[y] = ++idx, a[y], b[y] });
}
writeln(a[1]); for (int i = 1; i <= n; i++) fa[i] = vis[i] = del[i] = head[i] = 0; for (int i = 1; i <= edge_size; i++) edge[i] = { 0, 0 }; edge_size = 0;
}
signed main()
{
// freopen("tree.in", "r", stdin), freopen("tree.out", "w", stdout);
int T = 1; read(T);
while (T--) ___(); flush();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9704kb
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
Wrong Answer
time: 337ms
memory: 22364kb
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:
63495498 604652243 796269298 796269298 796269298 796269298 796269298 796269298 796269298 796269298
result:
wrong answer 2nd numbers differ - expected: '541156749', found: '604652243'