QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#585900 | #7103. Red Black Tree | feng | AC ✓ | 987ms | 49832kb | C++20 | 2.8kb | 2024-09-23 22:46:17 | 2024-09-23 22:46:17 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define PII pair<int,int>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
struct edge {
int v, w;
};
vector<edge> tr[maxn];
bool sta[maxn];
int dep[maxn], f[maxn][30], d[maxn];
template<typename T>inline void readT(T& x) {
bool f = 1; x = 0; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') f = !f; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
x = (f ? x : -x); return;
}
template<typename T>
inline void writeT(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) writeT(x / 10);
putchar(x % 10 + '0'); return;
}
void ini(int u, int fa) {
dep[u] = dep[fa] + 1;
f[u][0] = fa;
for (int i = 1; i <= 21; ++i) {
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (edge e : tr[u]) {
int v = e.v;
int w = e.w;
if (v == fa) continue;
ini(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 21; i >= 0; --i) {
if (dep[f[u][i]] < dep[v]) continue;
u = f[u][i];
}
if (u == v) return u;
for (int i = 21; i >= 0; --i) {
if (f[u][i] == f[v][i]) continue;
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
int ans[maxn];
void dfs(int u, int d, int fa) {
if (!sta[u]) ans[u] = d;
else ans[u] = 0;
for (edge e : tr[u]) {
int v = e.v;
int w = e.w;
if (v == fa) continue;
if (sta[u]) dfs(v, w, u);
else dfs(v, d + w, u);
}
}
void dfs1(int u, int dd, int fa) {
d[u] = dd;
for (edge e : tr[u]) {
int v = e.v;
int w = e.w;
if (v == fa) continue;
dfs1(v, dd + w, u);
}
}
vector<int> t;
bool cmp(int a, int b) {
return ans[a] > ans[b];
}
void solve() {
int n, m, q;
readT(n);readT(m);readT(q);
for (int i = 1; i <= n; ++i) {
tr[i].clear();
sta[i] = false;
}
for (int i = 1; i <= m; ++i) {
int a; readT(a);
sta[a] = true;
}
for (int i = 1; i < n; ++i) {
int u, v, w;
readT(u); readT(v); readT(w);
tr[u].push_back({ v,w });
tr[v].push_back({ u,w });
}
ini(1, 0);
dfs(1, 0, 0);
dfs1(1, 0, 0);
while (q--) {
int k; readT(k);
t.clear();
int sum = 0;
for (int i = 0; i < k; i++) {
int a; readT(a);
t.push_back(a);
sum = max(sum, ans[a]);
}
if (t.size() <= 1) {
cout << "0" << endl;
continue;
}
sort(t.begin(), t.end(), cmp);
int res = ans[t[1]];
int an = t[0];
int mmax = d[t[0]];
for (int i = 1; i < t.size(); ++i) {
an = lca(an, t[i]);
mmax = max(mmax, d[t[i]]);
int temp = 0;
if (i != t.size() - 1) {
temp = max(mmax - d[an], ans[t[i + 1]]);
}
else temp = mmax - d[an];
res = min(temp, res);
}
cout << res << endl;
}
}
signed main() {
//ios::sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
int T; readT(T);
while (T--) {
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7728kb
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: 0
Accepted
time: 987ms
memory: 49832kb
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 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
ok 577632 lines
Extra Test:
score: 0
Extra Test Passed