QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#159332 | #7103. Red Black Tree | ucup-team203# | AC ✓ | 1458ms | 33728kb | C++17 | 4.3kb | 2023-09-02 17:47:37 | 2023-09-02 17:47:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 1e5 + 5;
// const int M = 2e7 + 5;
// const int mod = 998244353;
// const int mod = 1000000007;
int n, m, q, fa[N][18], col[N], dep[N], lst[N], dfn[N], dfs_tot;
i64 dis[N];
vector<pair<int, int>> adj[N];
void dfs1(int cur, int f)
{
dfn[cur] = ++dfs_tot;
dep[cur] = dep[f] + 1;
if (col[cur])
lst[cur] = cur;
for (auto [i, val] : adj[cur])
{
if (i == f)
continue;
fa[i][0] = cur, lst[i] = lst[cur];
for (int j = 1; j <= 17; j++)
fa[i][j] = fa[fa[i][j - 1]][j - 1];
dis[i] = dis[cur] + val;
dfs1(i, cur);
}
}
int findlca(int x, int y)
{
if (x == y)
return x;
if (dep[x] < dep[y])
swap(x, y);
for (int i = 17; i >= 0; i--)
if (dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if (x == y)
return x;
for (int i = 17; i >= 0; i--)
if (fa[x][i] != fa[y][i])
tie(x, y) = tie(fa[x][i], fa[y][i]);
return fa[x][0];
}
bool flag[N];
vector<int> G[N];
int k, h[N], sta[N], top;
i64 ans, mxval[N], mxdep[N], rval[N];
void dfs2(int cur)
{
mxdep[cur] = mxval[cur] = rval[cur] = 0;
if (flag[cur] && !col[cur])
mxdep[cur] = dis[cur], mxval[cur] = dis[cur] - dis[lst[cur]];
for (int i : G[cur])
{
dfs2(i);
if (!col[cur] && !col[i] && lst[i] == lst[cur])
{
mxdep[cur] = max(mxdep[i], mxdep[cur]);
mxval[cur] = max(mxval[i], mxval[cur]);
rval[cur] = max(rval[cur], rval[i]);
}
else
{
mxval[cur] = max(mxval[i], mxval[cur]);
rval[cur] = max(rval[cur], mxval[i]);
}
}
}
void dfs3(int cur, i64 val)
{
ans = min(ans, max({val, mxdep[cur] - dis[cur], rval[cur]}));
if (flag[cur] && !col[cur])
val = max(val, dis[cur] - dis[lst[cur]]);
int siz = G[cur].size();
vector<i64> pre(siz + 2), suf = pre;
for (int j = 0; j < siz; j++)
pre[j + 1] = max(pre[j], mxval[G[cur][j]]);
for (int j = siz - 1; j >= 0; j--)
suf[j + 1] = max(suf[j + 2], mxval[G[cur][j]]);
for (int j = 0; j < siz; j++)
{
int i = G[cur][j];
dfs3(i, max({val, pre[j], suf[j + 2]}));
}
}
void solve()
{
cin >> n >> m >> q;
for (int i = 1, x; i <= m; i++)
{
cin >> x;
col[x] = 1;
}
for (int i = 1, u, v, w; i < n; i++)
{
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs1(1, 0);
while (q--)
{
cin >> k;
for (int j = 1; j <= k; j++)
{
cin >> h[j], flag[h[j]] = 1;
if (!col[h[j]])
ans = max(ans, dis[h[j]] - dis[lst[h[j]]]);
}
sort(h + 1, h + k + 1, [&](int a, int b)
{ return dfn[a] < dfn[b]; });
sta[top = 1] = 1;
G[1].clear();
for (int i = 1; i <= k; i++)
{
if (h[i] == 1)
continue;
int f = findlca(h[i], sta[top]);
if (f != sta[top])
{
while (dfn[f] < dfn[sta[top - 1]])
{
G[sta[top - 1]].push_back(sta[top]);
top--;
}
if (dfn[f] > dfn[sta[top - 1]])
{
G[f].clear();
G[f].push_back(sta[top]);
sta[top] = f;
}
else
{
G[f].push_back(sta[top]);
top--;
}
}
sta[++top] = h[i];
G[h[i]].clear();
}
for (int i = 1; i < top; i++)
G[sta[i]].push_back(sta[i + 1]);
dfs2(1);
dfs3(1, 0);
cout << ans << '\n';
ans = 0;
for (int i = 1; i <= k; i++)
flag[h[i]] = 0;
}
dfs_tot = 0;
for (int i = 1; i <= n; i++)
adj[i].clear(), col[i] = 0;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
solve();
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 13884kb
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: 1458ms
memory: 33728kb
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