QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303168#5439. Meet in the MiddleyllcmWA 0ms39160kbC++145.0kb2024-01-11 20:02:322024-01-11 20:02:33

Judging History

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

  • [2024-01-11 20:02:33]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:39160kb
  • [2024-01-11 20:02:32]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
#define int long long
using namespace std;
inline int read() {
  int x = 0; bool op = false;
  char c = getchar();
  while(!isdigit(c))op |= (c == '-'), c = getchar();
  while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return op ? -x : x;
}
const int N = 2e5 + 10;
int n, Q;
struct Edge {int v, w, id;};
namespace Tree2 {
  vector<Edge> G[N];
  void add(int u, int v, int w) {G[u].pb({v, w}); G[v].pb({u, w});}
  int cnt;
  int dfn[N], dep[N], st[N][25];
  int cmin(int x, int y) {return dep[x] < dep[y] ? x : y;}
  void dfs(int u, int fa) {
    st[dfn[u] = ++cnt][0] = fa;
    for(auto t : G[u]) {
      int v = t.v, w = t.w;
      if(v == fa)continue;
      dep[v] = dep[u] + w;
      dfs(v, u);
    }
    return ;
  }
  void init() {
    dfs(1, 0);
    // for(int i = 1; i <= n; i++)cout << st[i][0] << endl;
    for(int j = 1; (1 << j) <= n; j++) {
      for(int i = 1; i + (1 << j) - 1 <= n; i++) {
        st[i][j] = cmin(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
      }
    }
    return ;
  }
  int lca(int u, int v) {
    if(u == v)return u;
    int l = dfn[u], r = dfn[v];
    if(l > r)swap(l, r);
    int t = __lg(r - (l++));
    return cmin(st[l][t], st[r - (1 << t) + 1][t]);
  }
  int dist(int u, int v) {return dep[u] + dep[v] - dep[lca(u, v)] * 2;}
};
namespace Tree1 {
  int cM, cnte, cntv;
  struct Diam {
    int x, vx, y, vy;
    int val() {return (x == y ? vx : Tree2::dist(x, y) + vx + vy);}
  };
  Diam operator + (Diam x, Diam y) {
    if(!x.x || !y.y)return (x.x ? x : y);
    Diam z = (x.val() > y.val() ? x : y), tmp;
    if((tmp = (Diam){x.x, x.vx, y.x, y.vx}).val() > z.val())z = tmp;
    if((tmp = (Diam){x.x, x.vx, y.y, y.vy}).val() > z.val())z = tmp;
    if((tmp = (Diam){x.y, x.vy, y.x, y.vx}).val() > z.val())z = tmp;
    if((tmp = (Diam){x.y, x.vy, y.y, y.vy}).val() > z.val())z = tmp;
    return z;
  }
  Diam diam[N][2];
  vector<int> d[N];
  vector<int> side[N], id[N]; 
  vector<Edge> G[N];
  vector<Edge> H[N];
  void add(int u, int v, int w) {G[u].pb({v, w}); G[v].pb({u, w});}
  void add_h(int u, int v, int w) {H[u].pb({v, w, ++cnte}); H[v].pb({u, w, cnte});}
  void dfs(int u, int fa) {
    int lv = 0;
    for(auto t : G[u]) {
      int v = t.v, w = t.w;
      if(v == fa)continue;
      if(!lv)add_h(u, v, w), lv = u;
      else add_h(++cntv, lv, 0), add_h(cntv, v, w), lv = cntv;
      dfs(v, u);
    }
    return ;
  }
  int X, Y, S;
  int sz[N], vis[N];
  void findroot(int u, int fa) {
    sz[u] = 1;
    int mx = 0;
    for(auto t : H[u]) {
      int v = t.v, id = t.id;
      if(v == fa || vis[id])continue;
      findroot(v, u); sz[u] += sz[v];
      mx = max(mx, sz[v]);
    }
    if(max(S - sz[u], mx) * 2 <= S)X = u;
    return ;
  }
  void calc(int u, int fa, int o, int dist) {
    if(u <= n) {
      side[u].pb(o), id[u].pb(cM), d[u].pb(dist);
      diam[cM][o] = diam[cM][o] + (Diam){u, dist, u, dist};
    }
    for(auto t : H[u]) {
      int v = t.v, w = t.w, id = t.id;
      if(v == fa || vis[id])continue;
      calc(v, u, o, dist + w);
    }
    return ;
  }
  void solve(int u) {
    // cout << u << ' ' << S << endl;
    if(S == 1)return ;
    X = Y = 0; findroot(u, 0);
    int mx = 0; Edge e = {0, 0, 0};
    for(auto t : H[X]) {
      int v = t.v, id = t.id;
      if(vis[id])continue;
      int nsz = (sz[v] > sz[u] ? S - sz[u] : sz[v]);
      if(nsz > mx)mx = nsz, Y = v, e = t;
    }
    if(sz[X] < sz[Y])swap(X, Y);
    // cout << "X:" << X << ' ' << Y << endl;
    vis[e.id] = true;
    cM++; diam[cM][0] = {0, 0, 0, 0}; diam[cM][1] = {0, 0, 0, 0};
    calc(X, 0, 0, 0); calc(Y, 0, 1, e.w);
    int lS = S - sz[Y], rS = sz[Y], lu = X, ru = Y;
    S = lS; solve(lu); S = rS; solve(ru);
    return ;
  }
  void init() {
    cntv = n; dfs(1, 0); 
    S = cntv; solve(1);
    return ;
  }
  int query(int x, int y) {
    int lim = d[x].size(), ans = Tree2::dist(x, y);
    for(int i = 0; i < lim; i++) {
      int o = side[x][i];
      Diam nd = diam[id[x][i]][o ^ 1];
      ans = max(ans, Tree2::dist(y, nd.x) + nd.vx + d[x][i]);
      // cout << Tree2::dist(y, nd.y) << ' ' << nd.y << ' ' << nd.vy << ' ' << d[x][i] << endl;
      // cout << x << ' ' << nd.y << ' ' << ans << ' ' << side[x][i] << ' ' << d[x][i] << endl;
      ans = max(ans, Tree2::dist(y, nd.y) + nd.vy + d[x][i]);
    }
    return ans;
  }
};
signed main() {
  n = read(); Q = read();
  for(int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read();
    Tree1::add(u, v, w);
  }
  for(int i = 1; i < n; i++) {
    int u = read(), v = read(), w = read();
    Tree2::add(u, v, w);
  }
  Tree2::init(); 
  cout << Tree2::lca(2, 4) << endl;
  Tree1::init();
  while(Q--) {
    int x = read(), y = read();
    printf("%lld\n", Tree1::query(x, y));
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 39160kb

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:

1
6
4
5
3

result:

wrong answer 1st numbers differ - expected: '6', found: '1'