QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#158582#7103. Red Black Treeucup-team1479#AC ✓822ms63580kbC++144.0kb2023-09-02 16:43:212023-09-02 16:43:21

Judging History

你现在查看的是最新测评结果

  • [2023-09-02 16:43:21]
  • 评测
  • 测评结果:AC
  • 用时:822ms
  • 内存:63580kb
  • [2023-09-02 16:43:21]
  • 提交

answer

#include <bits/stdc++.h>
#define file_in(x) (freopen(#x".in", "r", stdin))
#define file_out(x) (freopen(#x".out", "w", stdout))
#define ll long long
#define int long long
#define vi vector
#define pr pair <int, int>
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define db double

using namespace std;

char _c; bool _f; template <class T> void IN(T &x) {
  _f = x = 0; while (_c = getchar(), !isdigit(_c)) {if (_c == '-') _f = 1;}
  while (isdigit(_c)) {x = x * 10 + _c - '0', _c = getchar();} if (_f) x = -x;
}

template <class T> void _write(T x) {
  if (x < 0) return putchar('-'), _write(-x);
  if (x > 9) _write(x / 10);
  putchar('0' + x % 10);
}
template <class T> void write(T x) {_write(x), putchar('\n');}
template <class T> void write_s(T x) {_write(x), putchar(' ');}
template <class first, class... rest> void write(first fir, rest... res) {
  write_s(fir), write(res...);
}
#define debug(...) (_debug(#__VA_ARGS__, __VA_ARGS__))
template <class T> void _debug(const char *format, T x) {
  cerr << format << " = " << x << endl;
}
template <class first, class... rest>
void _debug(const char *format, first fir, rest... res) {
  while (*format != ',') cerr << *format++;
  cerr << " = " << fir << ",", _debug(format + 1, res...);
}

bool START;

const int N = 2e5 + 5;
const ll inf = 1e18;

int t_c, n, m, q, dep[N], lg[N], d[N], tot, f[25][N], lc[N], md[N];
ll dis[N], val[N], F[N], s1[N], s2[N];
bool r[N];
vi <int> b;

struct edge {
  int y, w;
};

vi <edge> G[N];

bool END;

void add_e(int x, int y, int w) {G[x].pb((edge) {y, w}), G[y].pb((edge) {x, w});}

void dfs(int x, int fa, ll mxd, int anc) {
  dep[x] = dep[fa] + 1, f[0][x] = fa, val[x] = (r[x] ? 0 : (dis[x] - mxd)), md[x] = anc;
  for (int i = 1; i <= lg[n]; ++i) f[i][x] = f[i - 1][f[i - 1][x]];
  for (auto e : G[x]) {
    int y = e.y, w = e.w; if (y == fa) continue;
    dis[y] = dis[x] + w;
    if (r[y]) dfs(y, x, dis[y], y);
    else dfs(y, x, mxd, anc);
  }
}

int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = lg[n]; i >= 0; --i) if (dep[x] - (1 << i) >= dep[y]) x = f[i][x];
  if (x == y) return x;
  for (int i = lg[n]; i >= 0; --i) if (f[i][x] != f[i][y]) x = f[i][x], y = f[i][y];
  return f[0][x];
}

bool cmp(int x, int y) {return dep[x] < dep[y];}

signed main() {
  for (int i = 2; i < N; ++i) lg[i] = lg[i >> 1] + 1;
  IN(t_c);
  while (t_c--) {
    IN(n), IN(m), IN(q);
    for (int j = 0; j <= lg[n]; ++j) for (int i = 0; i <= n; ++i) f[j][i] = 0;
    for (int i = 1; i <= n; ++i) r[i] = 0, G[i].clear();

    for (int i = 1, x; i <= m; ++i) IN(x), r[x] = 1;
    for (int i = 1, x, y, w; i < n; ++i) IN(x), IN(y), IN(w), add_e(x, y, w);
    dfs(1, 0, 0, 1);
    while (q--) {
      int k, x; IN(k), b.clear();
      ll mxv = 0; int p = 0;
      while (k--) {
	IN(x), b.pb(x);
	if (val[x] >= mxv) mxv = val[x], p = x;
      }
      tot = 0; int tmp = 0;
      for (auto x : b) {
	lc[x] = lca(x, p), F[lc[x]] = s1[lc[x]] = s2[lc[x]] = 0;
	if (dep[lc[x]] >= dep[md[p]]) d[++tot] = lc[x];
	else tmp = max(tmp, val[x]);
      }
      sort(d + 1, d + 1 + tot, cmp), tot = unique(d + 1, d + 1 + tot) - (d + 1);
      for (int i = 1; i <= tot; ++i) F[d[i]] = s1[d[i]] = s2[d[i]] = 0;
      for (auto x : b) {
	if (dep[lc[x]] < dep[md[p]]) continue;
	F[lc[x]] = max(F[lc[x]], val[x]);
	if (md[x] == md[p]) s2[lc[x]] = max(s2[lc[x]], val[x]);
	else s1[lc[x]] = max(s1[lc[x]], val[x]);
      }
      for (int i = tot - 1; i; --i) s1[d[i]] = max(s1[d[i]], s1[d[i + 1]]), s2[d[i]] = max(s2[d[i]], s2[d[i + 1]]);
      ll ans = inf, mx = 0;
      for (int i = 1; i <= tot; ++i)
	ans = min(ans, max(mx, max(s1[d[i]], s2[d[i]] - (dis[d[i]] - dis[md[p]])))), mx = max(mx, F[d[i]]);
      ans = max(ans, tmp);
      write(ans);
    }
  }
  return 0;
}
/*
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
*/

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 26040kb

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: 822ms
memory: 63580kb

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