QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#186183 | #6425. Harmonious Rectangle | shimeming | RE | 0ms | 0kb | C++20 | 2.3kb | 2023-09-23 11:28:06 | 2023-09-23 11:28:06 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) {cerr << #x << " = " << x << '\n'; }
#define X first
#define Y second
#define pb push_back
#define int long long
const int MAXN = 1e5+5;
int n;
ll a[MAXN];
int t[MAXN];
vector<vector<int>> G;
vector<ll> dp;
ll dfs(int ind, int p) {
assert(n!=1);
if (ind != 0 && G[ind].size() == 1) {
return 0;
}
if (dp[ind] != 0) return dp[ind];
ll rt = -1;
ll sum = 0, maxn = -1, maxn31 = -1, maxn32 = -1;
vector<pair<int, ll>> sub;
for (int i : G[ind]) {
if (i == p) continue;
int tmp = dfs(i, ind);
sub.pb({i, tmp});
sum += tmp;
if (maxn == -1 || a[i] > a[maxn]) maxn = i;
if (t[i] == 3) {
if (maxn32 == -1 || a[i] >= a[maxn32]) {
maxn32 = i;
if (maxn31 == -1 || a[maxn32] >= a[maxn31]) swap(maxn31, maxn32);
}
}
}
rt = sum + a[maxn];
// debug(ind);
// debug(rt);
if (G[ind].size() > 2) {
// debug(ind);
int s1 = -1, s2 = -1;
auto diff = [&](int k) { return a[sub[k].X]-sub[k].Y; };
for (int i = 0; i < (int)sub.size(); i++) {
if (s2 == -1 || diff(i) >= diff(s2)) {
s2 = i;
if (s1 == -1 || diff(s2) >= diff(s1)) swap(s1, s2);
}
}
if (maxn31 != -1) {
// debug(maxn31);
// debug(sub[s1].X);
// debug(sub[s2].X);
if (sub[s1].X != maxn31) {
rt = max(rt, sum+a[maxn31]+a[sub[s1].X]-sub[s1].Y);
} else {
rt = max(rt, sum+a[maxn31]+a[sub[s2].X]-sub[s2].Y);
if (maxn32 != -1) {
rt = max(rt, sum+a[maxn32]+a[sub[s1].X]-sub[s1].Y);
}
}
}
}
dp[ind] = rt;
return rt;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
while (T--) {
cin >> n;
G.resize(n+1);
dp.resize(n+1);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> t[i];
for (int i = 1; i <= n-1; i++) {
int x, y; cin >> x >> y;
G[x].pb(y);
G[y].pb(x);
}
if (n == 1) {
cout << a[1] << '\n';
continue;
}
t[1] = 2;
G[0].pb(1);
G[1].pb(0);
dfs(0, -1);
cout << dp[0] << '\n';
G.clear();
dp.clear();
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
3 1 4 2 2 3 3