QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163908 | #6738. Cover | ucup-team1209# | RE | 3ms | 11608kb | C++20 | 2.7kb | 2023-09-04 16:30:28 | 2023-09-04 16:30:29 |
Judging History
answer
#include<bits/stdc++.h>
using u64 = unsigned long long;
using std::cin;
using std::cout;
const int mod = 998244353;
using ll = long long;
const int N = 100005;
int n, m, k;
struct edge { int to, nxt; } e[N << 1];
int h[N], num;
void link(int x, int y) {
e[++num] = {y, h[x]}, h[x] = num;
e[++num] = {x, h[y]}, h[y] = num;
}
int st[20][N], dfn[N], cnt;
int min(int x, int y) {
return dfn[x] < dfn[y] ? x : y;
}
void dfs0(int x, int fa = 0) {
st[0][cnt] = fa, dfn[x] = ++cnt;
for(int i = h[x];i;i = e[i].nxt) if(e[i].to != fa) {
dfs0(e[i].to, x);
}
}
int lca(int x, int y) {
if(dfn[x] > dfn[y]) std::swap(x, y);
const int lg = std::__lg(dfn[y] - dfn[x]);
if(x == y) return x;
return min(st[lg][dfn[x]], st[lg][dfn[y] - (1 << lg)]);
}
struct qy { int a, b; ll w; };
std::vector<qy> v[N];
ll dp[N];
ll sdp[N];
int anc[N]; ll w[N];
int find(int x) {
if(x == anc[x]) return x;
int t = find(anc[x]);
w[x] += w[anc[x]];
anc[x] = t;
return x;
}
ll ask(int x) {
find(x);
return w[x] + dp[x];
}
int id[N];
void up(ll & x, ll y) {
if(x < y) x = y;
}
void dfs1(int x, int fa = 0) {
int cc = 0;
std::vector<ll> all;
for(int i = h[x];i;i = e[i].nxt) if(e[i].to != fa) {
dfs1(e[i].to, x);
id[e[i].to] = cc++;
sdp[x] += dp[e[i].to];
all.push_back(dp[e[i].to]);
}
std::vector<qy> pb;
for(auto [a, b, w] : v[x]) {
if(b == x) std::swap(a, b);
if(a == x) {
ll res = ask(b);
up(all[id[find(b)]], res + w);
} else {
ll res = ask(a) + ask(b);
pb.push_back({ id[find(a)], id[find(b)], res + w });
}
}
{
std::vector<ll> dp(1 << cc);
for(int i = 1;i < 1 << cc;++i) {
dp[i] = dp[i & (i - 1)] + all[__builtin_ctz(i)];
}
for(auto [a, b, w] : pb) {
int s = (1 << cc) - 1, t = (1 << a) | (1 << b);
s ^= t;
for(int i = s;i;i = (i - 1) & s) {
up(dp[i | t], dp[i] + w);
}
up(dp[t], w);
}
::dp[x] = dp[(1 << cc) - 1];
}
for(int i = h[x];i;i = e[i].nxt) if(e[i].to != fa) {
anc[e[i].to] = x;
w[e[i].to] = sdp[x] - dp[e[i].to];
}
}
int deg[N];
int main() {
std::ios::sync_with_stdio(false), cin.tie(0);
#ifdef zqj
freopen("$.in", "r", stdin);
#endif
cin >> n >> m >> k;
for(int i = 1, x, y;i < n;++i) {
cin >> x >> y;
link(x, y);
++ deg[x]; ++ deg[y];
}
int r = std::min_element(deg + 1, deg + n + 1) - deg;
dfs0(r);
for(int i = 1;i < 20;++i)
for(int j = 1;j + (1 << i) - 1 < n;++j)
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
for(int i = 1, a, b, w;i <= m;++i) {
cin >> a >> b >> w;
v[lca(a, b)].push_back({a, b, w});
}
for(int i = 1;i <= n;++i) anc[i] = i;
dfs1(r);
cout << dp[r] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 11608kb
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
Runtime Error
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...