QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#884020 | #7588. Monster Hunter | WTR2007 | WA | 0ms | 5760kb | C++20 | 2.2kb | 2025-02-05 20:54:21 | 2025-02-05 20:54:29 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define MULT_TEST 1
using namespace std;
typedef long double ldb;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int N = 100005;
int f[N], x[N], y[N];
bool vis[N];
vector<int> e[N];
struct node {
int a, b, id;
bool operator < (const node &T) const {
return max(a, a + T.a - b) > max(T.a, a + T.a - T.b);
}
};
struct dsu {
vector<int> fa;
dsu(int n) : fa(n + 2) { iota(fa.begin(), fa.end(), 0); };
inline int Find(int r) {
while (r != fa[r]) r = fa[r] = fa[fa[r]];
return r;
}
inline bool Merge(int x, int y) {
x = Find(x); y = Find(y);
if (x == y) return true;
else return fa[y] = x, false;
}
};
inline int read() {
int w = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
w = (w << 1) + (w << 3) + ch - 48;
ch = getchar();
}
return w * f;
}
inline void DFS(int x) {
for (auto u : e[x]) {
if (u == f[x]) continue;
f[u] = x, DFS(u);
}
}
inline void Solve() {
int n;
n = read();
priority_queue<node> q;
q.push((node){0, 0, 1});
for (int i = 2; i <= n; i++) {
x[i] = read(), y[i] = read();
q.push((node){x[i], y[i], i});
}
for (int i = 1; i < n; i++) {
int u = read(), v = read();
e[u].push_back(v), e[v].push_back(u);
}
DFS(1);
dsu A(n);
while (!q.empty()) {
auto [a, b, id] = q.top(); q.pop();
if (vis[id]) continue;
vis[id] = 1;
if (id == 1) continue;
int top = A.Find(f[id]);
x[top] = max(x[top], x[top] - y[top] + x[id]);
y[top] = max(y[top], y[top] - x[id] + y[id]);
q.push((node){x[top], y[top], top});
A.Merge(top, id);
}
printf("%lld\n", x[1]);
for (int i = 1; i <= n; i++) e[i].clear(), vis[i] = 0;
}
signed main() {
int _ = 1;
#if MULT_TEST
_ = read();
#endif
while (_--) Solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5760kb
input:
1 4 2 6 5 4 6 2 1 2 2 3 3 4
output:
5
result:
wrong answer 1st numbers differ - expected: '3', found: '5'