QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#231498 | #7103. Red Black Tree | ngpin04 | WA | 736ms | 30632kb | C++14 | 4.1kb | 2023-10-29 13:18:29 | 2023-10-29 13:18:30 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define bit(n) (1LL << (n))
#define getbit(x, i) (((x) >> (i)) & 1)
#define pii pair<int, int>
#define ALL(x) x.begin(), x.end()
using namespace std;
const int M = 5e5 + 5;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;
const long long ooo = 1e18;
const double pi = acos(-1);
template<typename T1, typename T2> bool mini(T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
typedef long long ll;
vector <int> adj[N];
long long dp[N];
pair <int, int> cand[N];
int anc[N][17];
int red[N];
int ver[N];
int fr[N];
int to[N];
int h[N];
int w[N];
int n,m,q;
void dfs(int u, int p) {
if (red[u] == -1)
red[u] = red[p];
anc[u][0] = p;
for (int ind : adj[u]) {
int v = fr[ind] ^ to[ind] ^ u;
if (v == p)
continue;
dp[v] = dp[u] + w[ind];
h[v] = h[u] + 1;
dfs(v, u);
}
}
int getlca(int u, int v) {
if (h[u] < h[v])
swap(u, v);
for (int i = 16; i >= 0; i--)
if (h[u] - bit(i) >= h[v])
u = anc[u][i];
if (u == v)
return v;
for (int i = 16; i >= 0; i--)
if (anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return anc[v][0];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("file.inp", "r",stdin);
#endif
auto solve = [&]() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n; i++)
red[i] = -1;
for (int i = 1; i <= m; i++) {
int v; cin >> v;
v--;
red[v] = v;
}
for (int i = 0; i < n; i++)
adj[i].clear();
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v >> w[i];
u--, v--;
fr[i] = u, to[i] = v;
adj[u].push_back(i);
adj[v].push_back(i);
}
dp[0] = h[0] = 0;
dfs(0, -1);
for (int j = 1; j < 17; j++)
for (int i = 0; i < n; i++) {
int p = anc[i][j - 1];
if (p == -1)
anc[i][j] = -1;
else
anc[i][j] = anc[p][j - 1];
}
while (q--) {
int k; cin >> k;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
x--;
ver[i] = x;
}
sort(ver + 0, ver + k, [&](int i, int j) {
return dp[i] - dp[red[i]] < dp[j] - dp[red[j]];
});
int sz = 0;
long long mx = 0;
for (int i = 0; i < k; i++) {
int c = getlca(ver[i], ver[k - 1]);
if (red[c] != red[ver[k - 1]] || red[c] != red[ver[i]])
maxi(mx, dp[ver[i]] - dp[red[ver[i]]]);
else {
cand[sz++] = {c, dp[ver[i]] - dp[red[ver[i]]]};
}
}
sort(cand, cand + sz, [&](pair <int, int> a, pair <int, int> b) {
pair <int, int> pa = {-h[a.fi], a.se};
pair <int, int> pb = {-h[b.fi], b.se};
return pa > pb;
});
long long ans = ooo;
for (int i = 0; i < sz;) {
int j = i;
while (j < sz && cand[i].fi == cand[j].fi)
j++;
// cerr << cand[i].fi << " " << cand[i].se << "\n";
int p = cand[i].fi;
mini(ans, max(mx, dp[ver[k - 1]] - dp[p]));
for (int k = i; k < j; k++)
maxi(mx, cand[i].se);
i = j;
}
cout << ans << "\n";
}
};
int t; cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17524kb
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
Wrong Answer
time: 736ms
memory: 30632kb
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:
wrong answer 59th lines differ - expected: '2307445864', found: '2094962422'