QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125106 | #5418. Color the Tree | savacska | WA | 22ms | 19368kb | C++23 | 3.7kb | 2023-07-16 01:45:19 | 2023-07-16 01:45:20 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;
const int H = 17;
struct Update
{
int pos, lef, rig;
};
vector <int> g[100005];
int cost[100005], metka[100005];
vector <ll> dp[100005];
vector <Update> updates[100005];
int sp[H + 3][100005], logs[100005];
int get_min(int lef, int rig)
{
int step = logs[rig - lef + 1];
return min(sp[step][lef], sp[step][rig - (1 << step) + 1]);
}
void actualize(int v, int len)
{
len = min(len, (int) dp[v].size());
//cout << "Len: " << len << '\n';
while (!updates[v].empty() && (int) dp[v].size() - updates[v].back().pos <= len)
{
Update upd = updates[v].back();
//cout << "AAAA: " << v << ' ' << upd.pos << ' ' << upd.lef << ' ' << upd.rig << '\n';
updates[v].pop_back();
int L = upd.lef - upd.pos, R = upd.rig - upd.pos;
dp[v][upd.pos] = min(dp[v][upd.pos], (ll) get_min(L, R));
upd.pos--;
if (upd.pos >= 0)
{
if (!updates[v].empty() && updates[v].back().pos == upd.pos) updates[v].back().rig = upd.rig;
else updates[v].pb(upd);
}
}
return;
}
void write(int v)
{
cout << "Write: " << v << '\n';
cout << "Metka = " << metka[v] << '\n';
cout << "DP: ";
for (ll x : dp[metka[v]]) cout << x << ' ';
cout << '\n';
for (const auto &[pos, lef, rig] : updates[metka[v]]) cout << pos << ' ' << lef << ' ' << rig << '\n';
cout << "-----------\n";
return;
}
void dfsik(int v, int pr)
{
metka[v] = v;
int mx1 = 0, mx2 = 0;
for (int u : g[v])
{
if (u == pr) continue;
dfsik(u, v);
if (mx1 == 0 || (int) dp[metka[mx1]].size() <= (int) dp[metka[u]].size()) mx2 = mx1, mx1 = u;
else if (mx2 == 0 || (int) dp[metka[mx2]].size() <= (int) dp[metka[u]].size()) mx2 = u;
}
if (mx1 == 0)
{
dp[metka[v]].pb(cost[0]);
//write(v);
return;
}
metka[v] = metka[mx1];
if (mx2 == 0)
{
dp[metka[v]].pb(cost[0]);
updates[metka[v]].pb({(int) dp[metka[v]].size() - 2, (int) dp[metka[v]].size() - 1, (int) dp[metka[v]].size() - 1});
//write(v);
return;
}
int len = (int) dp[metka[mx2]].size();
for (int u : g[v])
{
if (u == pr) continue;
actualize(metka[u], len);
}
for (int u : g[v])
{
if (u == pr || u == mx1) continue;
for (int i = 0; i < (int) dp[metka[u]].size(); i++)
dp[metka[v]][i] += dp[metka[u]][i];
}
dp[metka[v]].pb(cost[0]);
updates[metka[v]].pb({(int) dp[metka[v]].size() - 2, (int) dp[metka[v]].size() - 1, (int) dp[metka[v]].size() - 1});
//write(v);
return;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int test;
cin >> test;
for (int rep = 1; rep <= test; rep++)
{
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> cost[i];
logs[1] = 0;
for (int i = 2; i <= n; i++) logs[i] = logs[i / 2] + 1;
for (int i = 0; i < n; i++) sp[0][i] = cost[i];
for (int step = 1; step <= H; step++)
for (int i = 0; i < n; i++)
{
if (i + (1 << (step - 1)) < n) sp[step][i] = min(sp[step - 1][i], sp[step - 1][i + (1 << (step - 1))]);
else sp[step][i] = sp[step - 1][i];
}
for (int i = 1; i <= n; i++) g[i].clear(), dp[i].clear(), updates[i].clear();
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
g[u].pb(v), g[v].pb(u);
}
dfsik(1, -1);
actualize(metka[1], n);
ll ans = 0;
for (ll x : dp[metka[1]]) ans += x;
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 19020kb
input:
3 4 10 15 40 1 1 2 2 3 2 4 5 10 5 1 100 1000 1 2 2 3 2 4 4 5 4 1000 200 10 8 1 2 2 3 3 4
output:
35 17 1218
result:
ok 3 number(s): "35 17 1218"
Test #2:
score: -100
Wrong Answer
time: 22ms
memory: 19368kb
input:
3000 54 43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44 1 2 1 3 2 4 3 5 4 6 2 7 4 8 1 9 3 10 1 11 8 12 2 13 9 14 1 15 1 16 15 17 15 18 7 19 1 20 19 21 13 22 10 23 17 24 9 25 9 26 24 27 8 28...
output:
184 177 192 230 148 246 212 104 100 81 172 68 162 130 159 142 153 230 130 186 138 171 78 282 174 144 192 211 81 186 121 235 84 240 229 244 173 170 146 116 102 72 180 168 217 210 182 97 198 59 56 22 115 148 186 132 99 226 53 158 163 141 159 119 222 73 151 239 80 340 142 84 118 147 252 224 157 158 121...
result:
wrong answer 1st numbers differ - expected: '180', found: '184'