QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#523639 | #5041. Fire | Andyqian7 | WA | 1ms | 5940kb | C++20 | 3.4kb | 2024-08-18 15:30:16 | 2024-08-18 15:30:16 |
Judging History
answer
#include <bits/stdc++.h>
#define lowbit(x) x & -x
#define rep(i, s, e) for (int i = s; i <= e; i++)
using namespace std;
const int MAX = 1e5 + 10;
int n, m, k, a[MAX], f[MAX], g[MAX], sz[MAX];
vector<int> nei[MAX], sons[MAX];
void build(int s)
{
sz[s] = 1;
for (int n : nei[s])
{
if (sz[n])
continue;
build(n);
sz[s] += sz[n];
sons[s].push_back(n);
}
}
struct node
{
int dp, sz;
friend bool operator<(const node &x, const node &y)
{
return x.sz > y.sz;
}
};
void dfs(int s)
{
for (int son : sons[s])
{
dfs(son);
}
int l = -1, r = 1e9;
while (l < r)
{
int mid = (l + r + 1) / 2;
vector<node> v;
m = sons[s].size();
for (int son : sons[s])
{
v.push_back({f[son], sz[son]});
}
sort(v.begin(), v.end(), [](const node &x, const node &y)
{ return x.dp + 2 * x.sz > y.dp + 2 * y.sz; });
priority_queue<node> Q;
int tmp = mid + 2 * (sz[s] - 1) + 1, pos = 0, sum = 0; // sum of 2*sz not inclusive
bool sign = 1;
rep(i, 1, m)
{
for (; pos < m && sum + v[pos].dp + 2 * v[pos].sz >= tmp; pos++)
{
Q.push(v[pos]);
}
if (Q.empty())
{
sign = 0;
break;
}
auto [dp, sz] = Q.top();
Q.pop();
sum += 2 * sz;
}
if (sign)
l = mid;
else
r = mid - 1;
}
f[s] = min(a[s], l);
if (sons[s].empty())
{
g[s] = a[s];
return;
}
l = -1, r = 1e9;
while (l < r)
{
int mid = (l + r + 1) / 2;
int ma = -1e9, mapo = -1;
for (int son : sons[s])
{
if (g[son] + 2 * sz[son] >= mid + 2 * sz[s] - 1 && sz[son] > ma)
{
mapo = son;
ma = sz[son];
}
}
bool sign = 1;
if (mapo == -1)
sign = 0;
if (a[s] + k - mid - 2 * (sz[s] - ma) < 0)
sign = 0;
vector<node> v;
for (int son : sons[s])
{
if (son != mapo)
{
v.push_back({f[son], sz[son]});
}
}
sort(v.begin(), v.end(), [](const node &x, const node &y)
{ return x.dp + 2 * x.sz > y.dp + 2 * y.sz; });
priority_queue<node> Q;
m = v.size();
int tmp = mid + 2 * (sz[s] - 1 - ma) + 1, pos = 0, sum = 0; // sum of 2*sz not inclusive
rep(i, 1, m)
{
for (; pos < m && sum + v[pos].dp + 2 * v[pos].sz >= tmp; pos++)
{
Q.push(v[pos]);
}
if (Q.empty())
{
sign = 0;
break;
}
auto [dp, sz] = Q.top();
Q.pop();
sum += 2 * sz;
}
if (sign)
l = mid;
else
r = mid - 1;
}
g[s] = min(a[s], l);
}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> k;
rep(i, 1, n - 1)
{
int x, y;
cin >> x >> y;
nei[x].push_back(y), nei[y].push_back(x);
}
build(1);
rep(i, 1, n) cin >> a[i];
dfs(1);
cout << g[1];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5640kb
input:
3 1 1 2 1 3 4 3 5
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
3 1 1 2 1 3 2 10 10
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5940kb
input:
2 0 1 2 10 10
output:
8
result:
ok 1 number(s): "8"
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 5680kb
input:
3 0 1 2 3 1 10 8 10
output:
6
result:
wrong answer 1st numbers differ - expected: '5', found: '6'