QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#128979 | #6738. Cover | ckiseki# | WA | 1323ms | 41380kb | C++20 | 4.7kb | 2023-07-21 17:33:18 | 2023-07-21 17:33:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [";
while (L != R) cerr << " " << *L++;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
namespace {
constexpr int lowbit(int x) { return x & (-x); }
class BIT {
int n;
vector<int64_t> a;
int64_t query(int p) {
int64_t r = 0;
while (p) {
r += a[p];
p -= lowbit(p);
}
return r;
}
public:
void init(int n_) {
a.assign(n = n_, 0);
}
void add(int p, int64_t v) {
while (p < n) {
a[p] += v;
p += lowbit(p);
}
}
int64_t query(int l, int r) {
return query(r) - query(l);
}
} bit;
constexpr int kN = 100000 + 5;
constexpr int kLog = 20;
vector<int> g[kN];
int tin[kN], tout[kN], tc;
int pa[kN][kLog];
int chain[kN], chains;
int head[kN];
void dfs(int u, int f) {
pa[u][0] = f;
for (int i = 1; i < kLog; ++i)
pa[u][i] = pa[pa[u][i - 1]][i - 1];
tin[u] = tc++;
for (int v : g[u]) {
if (v == f)
continue;
dfs(v, u);
if (lowbit(chain[u]) < lowbit(chain[v]))
chain[u] = chain[v];
}
if (chain[u] == 0)
chain[u] = ++chains;
tout[u] = tc;
head[chain[u]] = u;
}
bool anc(int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
int lca(int u, int v) {
if (anc(u, v)) return u;
for (int i = kLog - 1; i >= 0; --i) {
if (not anc(pa[u][i], v))
u = pa[u][i];
}
return pa[u][0];
}
vector<tuple<int, int, int64_t>> path[kN];
size_t pos[kN];
int64_t dp[kN][13];
uint32_t tomask(int x, int y) {
uint32_t r = 0;
if (x != -1)
r |= 1u << x;
if (y != -1)
r |= 1u << y;
return r;
}
int64_t calc(int u, int p) {
int64_t ret = dp[u][12];
while (chain[u] != chain[p]) {
int f = head[chain[u]];
ret += bit.query(tin[u] + 1, tin[f] + 1);
ret += dp[f][pos[pa[f][0]]];
u = pa[f][0];
}
ret += bit.query(tin[u] + 1, tin[p] + 1);
return ret;
}
void solve(int u) {
tin[u] = tc++;
for (size_t i = 0; i < g[u].size(); ++i) {
pos[g[u][i]] = i;
solve(g[u][i]);
}
auto oid = [u](int x) {
int r = -1;
for (int i = 0; i < int(g[u].size()); ++i)
if (anc(g[u][i], x))
r = i;
return r;
};
for (auto &[x, y, w] : path[u]) {
w += calc(x, u);
w += calc(y, u);
x = oid(x);
y = oid(y);
}
array<int64_t, 1 << 12> val = {};
if (not g[u].empty()) {
const size_t k = g[u].size();
const size_t k2 = 1u << k;
for (auto [x, y, w] : path[u]) {
auto mask = tomask(x, y);
for (size_t S = 0; S < k2; ++S) {
if ((S & mask) == 0)
val[S | mask] = max(val[S | mask], val[S] + w);
}
}
for (size_t S = 0; S < k2; ++S) {
for (size_t i = 0; i < k; ++i) {
if ((S >> i) & 1)
continue;
dp[u][i] = max(dp[u][i], val[S]);
}
}
dp[u][12] = *max_element(val.begin(), val.end());
bit.add(tin[u] + 1, dp[u][0]);
}
tout[u] = tc;
}
} // namespace
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
u -= 1, v -= 1;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, 0);
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
u -= 1, v -= 1;
path[lca(u, v)].emplace_back(u, v, w);
}
for (int i = 0; i < n; ++i) {
int fi = -1;
vector<int> cur;
for (int j : g[i]) {
if (j != pa[i][0]) {
if (chain[j] == chain[i])
fi = j;
else
cur.push_back(j);
}
}
if (fi != -1) {
cur.push_back(fi);
reverse(cur.begin(), cur.end());
}
g[i] = cur;
}
tc = 0;
bit.init(n + 1);
solve(0);
int64_t ans = 0;
for (auto r : dp[0])
ans = max(ans, r);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 10316kb
input:
5 7 3 1 2 1 3 2 4 2 5 3 2 8 5 4 10 3 1 2 1 2 7 2 1 2 1 2 1 5 2 3
output:
19
result:
ok 1 number(s): "19"
Test #2:
score: -100
Wrong Answer
time: 1323ms
memory: 41380kb
input:
100000 500000 12 2 1 3 2 4 2 5 2 6 5 7 2 8 5 9 3 10 2 11 2 12 5 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 12 25 2 26 2 27 2 28 2 29 2 30 15 31 30 32 23 33 26 34 22 35 30 36 26 37 3 38 3 39 3 40 3 41 3 42 3 43 3 44 3 45 3 46 3 47 20 48 21 49 4 50 4 51 4 52 4 53 4 54 4 55 4 56 4 57 4 5...
output:
733377996023
result:
wrong answer 1st numbers differ - expected: '660925834533', found: '733377996023'