QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#423833 | #7103. Red Black Tree | HHH666 | WA | 0ms | 26144kb | C++14 | 4.0kb | 2024-05-28 17:54:20 | 2024-05-28 17:54:20 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define i1 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define all(v) v.begin(), v.end()
using namespace std;
template<typename types>
void read(types &x) {
x = 0; char c = getchar(); int f = 1;
while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f; return;
}
void read() {}
template<typename types, typename... Args>
void read(types &x, Args&... args) {
read(x), read(args...);
return;
}
template<typename types>
void write(types x) {
if (x < 0) putchar('-'), x = -x;
types k = x / 10;
if (k) write(k);
putchar(x - k * 10 + '0');
return;
}
void print() { puts(""); }
template<typename types, typename... Args>
void print(types x, Args... args) {
write(x), putchar(' '), print(args...);
return;
}
const int MAXN = 2e5 + 10;
const int MAXM = MAXN * 2;
const int LGN = 21;
int n, m, q;
bool b[MAXN], vis[MAXN];
ll dis[MAXN], dd[MAXN];
int head[MAXN], to[MAXM], w[MAXM], nxt[MAXM], idx = 1;
int dfn[MAXN], timecnt;
int depth[MAXN], pa[MAXN], mn[MAXN][LGN], lg[MAXN];
vector<int> to2[MAXN];
ll f[MAXN], g[MAXN], h[MAXN];
void addedge(int x, int y, int z) {
nxt[++idx] = head[x];
to[idx] = y;
w[idx] = z;
head[x] = idx;
return;
}
int cmp(int x, int y) { return depth[x] < depth[y] ? x : y; }
void dfs(int x, int p, ll d) {
dfn[x] = ++timecnt;
if (b[x]) d = 0;
dis[x] = d, pa[x] = p, depth[x] = depth[p] + 1;
mn[dfn[x]][0] = x;
for (int i = head[x]; i; i = nxt[i]) {
int y = to[i];
if (y == p) continue;
dd[y] = dd[x] + w[i];
dfs(y, x, d + w[i]);
}
return;
}
int query(int l, int r) {
int k = lg[r - l + 1];
return cmp(mn[l][k], mn[r - (1 << k) + 1][k]);
}
int lca(int x, int y) {
if (x == y) return x;
if (dfn[x] > dfn[y]) swap(x, y);
return pa[query(dfn[x] + 1, dfn[y])];
}
void dp(int x, ll t) {
f[x] = g[x] = h[x] = 0;
for (int y : to2[x]) {
dp(y, t);
f[x] += f[y];
if (!h[y]) continue;
if (dis[y] < dd[y] - dd[x]) {
if (dis[y] + g[y] > t) ++f[x];
}
else {
if (dd[y] - dd[x] + g[y] > t) ++f[x];
else g[x] = max(g[x], dd[y] - dd[x] + g[y]), h[x] = true;
}
}
h[x] |= vis[x];
if (b[x]) h[x] = false;
return;
}
void solve(vector<int> &v) {
for (int i : v) vis[i] = !b[i];
v.eb(1);
auto cmp = [&] (int x, int y) { return dfn[x] < dfn[y]; };
sort(all(v), cmp);
for (int i = 0, t = v.size(); i + 1 < t; ++i)
v.eb(lca(v[i], v[i + 1]));
sort(all(v), cmp), v.erase(unique(all(v)), v.end());
for (int i : v) to2[i].clear();
for (int i = 1; i < v.size(); ++i) to2[lca(v[i - 1], v[i])].eb(v[i]);
ll l = 0, r = 1e14;
while (l < r) {
ll mid = (l + r) >> 1;
dp(1, mid);
if (f[1] <= 1) r = mid;
else l = mid + 1;
}
print(l);
for (int i : v) vis[i] = false;
return;
}
void solve() {
read(n, m, q);
timecnt = 0;
fill(b + 1, b + n + 1, false);
fill(head + 1, head + n + 1, 0);
while (m--) {
int x; read(x);
b[x] = true;
}
for (int i = 1; i < n; ++i) {
int x, y, z; read(x, y, z);
addedge(x, y, z), addedge(y, x, z);
}
dfs(1, 0, 0);
for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for (int k = 1; k <= lg[n]; ++k)
for (int i = 1; i + (1 << k) - 1 <= n; ++i) mn[i][k] = cmp(mn[i][k - 1], mn[i + (1 << k - 1)][k - 1]);
while (q--) {
vector<int> v;
int k; read(k);
while (k--) { int x; read(x), v.eb(x); }
solve(v);
}
return;
}
int main() {
int t; read(t);
while (t--) solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 26144kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
wrong answer 1st lines differ - expected: '4', found: '4 '