QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#161968#7103. Red Black Treeucup-team008#AC ✓875ms23368kbC++175.8kb2023-09-03 03:13:242023-09-03 03:13:24

Judging History

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

  • [2023-09-03 03:13:24]
  • 评测
  • 测评结果:AC
  • 用时:875ms
  • 内存:23368kb
  • [2023-09-03 03:13:24]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


struct LCA {
  vector<int> depth;
  vector<vector<int>> anc;
  int n;
  int d;
  vector<int> startt, treesz;
  vector<ll> treedist;
  vector<ll> truedist;
  LCA(const vector<vector<array<int, 2>>>& tree, const vector<int>& redv) {
    auto dfs = y_combinator([&](auto self, int curr, int par, int& id) -> void {
      startt[curr] = id++;
      for(auto [out, w]: tree[curr]) {
        if(out == par) continue;
        treedist[out] = treedist[curr] + w;
        updmin(truedist[out], truedist[curr] + w);
        anc[0][out] = curr;
        depth[out] = depth[curr] + 1;
        self(out, curr, id);
        treesz[curr] += treesz[out];
      }
    });
    n = tree.size();
    startt.assign(n, -1);
    treesz.assign(n, 1);
    depth.assign(n, 1e9);
    treedist.assign(n, 1e18);
    truedist.assign(n, 1e18);
    for(int out: redv) truedist[out] = 0;
    depth[0] = 0;
    treedist[0] = 0;
    d = 1;
    while((1<<d) <= n) d++;
    anc.resize(d);
    for(auto& x: anc) x.assign(n, -1);
    {
      int id = 0;
      dfs(0, -1, id);
    }
    for(int k = 1; k < d; k++) for(int i = 0; i < n; i++) {
      if(anc[k-1][i] == -1) anc[k][i] = -1;
      else anc[k][i] = anc[k-1][anc[k-1][i]];
    }
  }
  int getLCA(int a, int b) {
    if(depth[a] < depth[b]) swap(a, b);
    for(int k = d-1; k >= 0; k--) {
      if(depth[a] - depth[b] >= (1 << k)) a = anc[k][a];
    }
    assert(depth[a] == depth[b]);
    for(int k = d-1; k >= 0; k--) {
      if(anc[k][a] != anc[k][b]) {
        a = anc[k][a];
        b = anc[k][b];
      }
    }
    if(a != b) {
      return anc[0][a];
    }
    return a;
  }
};

void solve(LCA& lca, vector<int>& query) {
  ll ans = lca.truedist[query[1]];
  int root = query[0];
  ll maxseen = lca.treedist[query[0]];
  for(int i = 1; i < sz(query); i++) {
    root = lca.getLCA(root, query[i]);
    updmax(maxseen, lca.treedist[query[i]]);
    ll cand = maxseen - lca.treedist[root];
    if(i+1 < sz(query)) updmax(cand, lca.truedist[query[i+1]]);
    updmin(ans, cand);
  }
  cout << ans << "\n";
}

void rsolve() {
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> redv(k);
  for(auto& x: redv) {
    cin >> x;
    x--;
  }
  vector<vector<array<int, 2>>> graph(n);
  for(int i = 1; i < n; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--; b--;
    graph[a].pb({b, w});
    graph[b].pb({a, w});
  }
  LCA lca(graph, redv);
  while(q--) {
    int numv;
    cin >> numv;
    vector<int> query(numv);
    for(auto& x: query) {
      cin >> x;
      x--;
    }
    sort(all(query), [&](int a, int b) {
      return lca.truedist[a] > lca.truedist[b];
    });
    if(numv == 1 || lca.truedist[query[0]] == 0) {
      cout << "0\n";
      continue;
    }
    solve(lca, query);
  }
}
void solve() {
  int t;
  cin >> t;
  while(t--) rsolve();
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3488kb

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: 875ms
memory: 23368kb

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