QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#605788 | #7103. Red Black Tree | kian2009 | TL | 0ms | 12452kb | C++14 | 2.1kb | 2024-10-02 19:37:06 | 2024-10-02 19:37:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10, MAXBIT = 18;
ll t, n, m, k, q, par[MAXN], near[MAXN], h[MAXN], dp[MAXN][MAXBIT], dist[MAXN][MAXBIT];
int cnt, st[MAXN], ed[MAXN];
vector<pair<int, int>> adj[MAXN];
vector<int> num;
bool red[MAXN];
void setup() {
for (int i = 0; i < n; i++) {
red[i] = near[i] = cnt = 0;
adj[i].clear();
}
}
void input() {
cin >> n >> m >> q;
setup();
for (int i = 0; i < m; i++) {
int u;
cin >> u;
red[--u] = true;
}
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[--u].push_back({--v, w});
adj[v].push_back({u, w});
}
}
void calcDp(int u) {
dp[u][0] = par[u];
for (int i = 1; i < MAXBIT; i++) {
dp[u][i] = dp[dp[u][i - 1]][i - 1];
dist[u][i] = dist[u][i - 1] + dist[dp[u][i - 1]][i - 1];
}
}
void dfs(int u, int p, ll d) {
st[u] = cnt;
cnt++;
par[u] = p;
if (!red[u])
near[u] = near[p] + d;
dist[u][0] = d;
calcDp(u);
for (auto x : adj[u])
if (x.first != p) {
h[x.first] = h[u] + 1;
dfs(x.first, u, x.second);
}
ed[u] = cnt;
cnt++;
}
bool check(ll x) {
int v = -1, ma = -1;
for (auto u : num)
if (near[u] > x) {
ll w = u, sum = 0;
for (int i = MAXBIT - 1; i >= 0; i--)
if (sum + dist[w][i] <= x) {
sum += dist[w][i];
w = dp[w][i];
}
if (v == -1 || h[w] > ma) {
v = w;
h[w] = ma;
}
}
if (v == -1)
return true;
for (auto u : num)
if (near[u] > x) {
if (!(st[v] <= st[u] && ed[v] >= st[u]))
return false;
}
return true;
}
ll findAns() {
ll l = -1, r = 1e14;
while (r - l > 1) {
ll mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
return r;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> t;
for (int i = 0; i < t; i++) {
input();
dfs(0, 0, 0);
for (int j = 0; j < q; j++) {
cin >> k;
num.clear();
for (int l = 0; l < k; l++) {
int u;
cin >> u;
num.push_back(--u);
}
cout << findAns() << endl;
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12452kb
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:
ok 7 lines
Test #2:
score: -100
Time Limit Exceeded
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 317070839 317070839 255904892 317070839 1265145897 1265145897 1072765445 538333404 455103436 285643094 285643094 285643094 317919339 0 476837157 691421476 605409472 479058444 371688030 303203698 476229270 910180170 910180170 919185207 121535083 181713164 181713164 181713164 181...