結果

問題 No.1212 Second Path
ユーザー SHIJOUSHIJOU
提出日時 2020-09-04 15:27:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,031 ms / 3,000 ms
コード長 5,912 bytes
コンパイル時間 4,900 ms
コンパイル使用メモリ 229,188 KB
実行使用メモリ 211,516 KB
最終ジャッジ日時 2023-08-17 03:51:35
合計ジャッジ時間 43,237 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 471 ms
209,412 KB
testcase_01 AC 990 ms
207,768 KB
testcase_02 AC 1,019 ms
203,456 KB
testcase_03 AC 1,001 ms
204,476 KB
testcase_04 AC 1,025 ms
194,312 KB
testcase_05 AC 1,031 ms
194,656 KB
testcase_06 AC 537 ms
178,000 KB
testcase_07 AC 718 ms
177,416 KB
testcase_08 AC 701 ms
177,576 KB
testcase_09 AC 717 ms
177,476 KB
testcase_10 AC 714 ms
177,448 KB
testcase_11 AC 708 ms
177,472 KB
testcase_12 AC 715 ms
177,660 KB
testcase_13 AC 718 ms
177,644 KB
testcase_14 AC 711 ms
177,528 KB
testcase_15 AC 702 ms
177,628 KB
testcase_16 AC 719 ms
177,604 KB
testcase_17 AC 446 ms
208,608 KB
testcase_18 AC 232 ms
165,924 KB
testcase_19 AC 240 ms
165,812 KB
testcase_20 AC 225 ms
165,728 KB
testcase_21 AC 228 ms
166,100 KB
testcase_22 AC 230 ms
166,048 KB
testcase_23 AC 187 ms
165,648 KB
testcase_24 AC 206 ms
165,880 KB
testcase_25 AC 202 ms
165,872 KB
testcase_26 AC 201 ms
165,892 KB
testcase_27 AC 201 ms
165,688 KB
testcase_28 AC 205 ms
165,840 KB
testcase_29 AC 792 ms
211,384 KB
testcase_30 AC 805 ms
211,516 KB
testcase_31 AC 781 ms
211,392 KB
testcase_32 AC 583 ms
174,596 KB
testcase_33 AC 513 ms
172,484 KB
testcase_34 AC 667 ms
176,636 KB
testcase_35 AC 298 ms
166,312 KB
testcase_36 AC 623 ms
174,680 KB
testcase_37 AC 580 ms
173,868 KB
testcase_38 AC 618 ms
174,416 KB
testcase_39 AC 445 ms
170,560 KB
testcase_40 AC 242 ms
165,924 KB
testcase_41 AC 573 ms
174,320 KB
testcase_42 AC 142 ms
162,748 KB
testcase_43 AC 143 ms
162,752 KB
testcase_44 AC 142 ms
162,808 KB
testcase_45 AC 521 ms
178,300 KB
testcase_46 AC 513 ms
178,152 KB
testcase_47 AC 555 ms
178,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

template<class T>
struct lca {
  int n, root, l;
  vector<vector<int> > to, par;
  vector<vector<T> > co;
  vector<int> dep;
  vector<T> costs;
  lca(int n):n(n), to(n), co(n), dep(n), costs(n) {
    l = 0;
    while((1<<l) < n) ++l;
    par = vector<vector<int> >(n+1, vector<int>(l, n));
  }
  void add(int a, int b, T c=0) {
    to[a].push_back(b); co[a].push_back(c);
    to[b].push_back(a); co[b].push_back(c);
  }
  void dfs(int v, int d=0, T c=0, int p=-1) {
    if(p != -1) par[v][0] = p;
    dep[v] = d;
    costs[v] = c;
    for(int i = 0; i < to[v].size(); ++i) {
      int u = to[v][i];
      if(u == p) continue;
      dfs(u, d+1, c+co[v][i], v);
    }
  }
  void init(int _root=0) {
    root = _root;
    dfs(root);
    for(int i = 0; i < l-1; ++i) {
      for(int v = 0; v < n; ++v) {
        par[v][i+1] = par[par[v][i]][i];
      }
    }
  }
  int lca_(int a, int b) {
    if(dep[a] > dep[b]) swap(a, b);
    int gap = dep[b]-dep[a];
    for(int i = l-1; i >= 0; --i) {
      int len = 1<<i;
      if(gap >= len) {
        gap -= len;
        b = par[b][i];
      }
    }
    if(a == b) return a;
    for(int i = l-1; i >= 0; --i) {
      int na = par[a][i];
      int nb = par[b][i];
      if(na != nb) {
        a = na;
        b = nb;
      }
    }
    return par[a][0];
  }
  int length(int a, int b) {
    int c = lca_(a, b);
    return dep[a]+dep[b]-dep[c]*2;
  }
  T dist(int a, int b) {
    int c = lca_(a, b);
    return costs[a]+costs[b]-costs[c]*2;
  }
};

template<class T>
struct SegTree {
  using FX = function<T(T, T)>;
  int n;
  FX fx;
  const T ex;
  vector<T> dat;
  SegTree(int n_, FX fx_, T ex_):fx(fx_), ex(ex_), n(1) {
    while(n < n_) n <<= 1;
    dat.assign((n<<1)-1, ex);
  }
  inline int chld(int k) {return (k<<1)+1;}
  inline int chrd(int k) {return (k<<1)+2;}
  void update(int i, T x) {
    i += n-1;
    dat[i] = x;
    while(i) {
      i = (i-1)>>1;
      dat[i] = fx(dat[chld(i)], dat[chrd(i)]);
    }
  }
  inline T query(int a, int b) {return query(a, b, 0, 0, n);}
  T query(int a, int b, int k, int l, int r) {
    if(r <= a || b <= l) return ex;
    if(a <= l && r <= b) return dat[k];
    T vl = query(a, b, chld(k), l, (l+r)>>1);
    T vr = query(a, b, chrd(k), (l+r)>>1, r);
    return fx(vl, vr);
  }
  const T &operator[](int idx) const {return dat[idx+n-1];}
};

struct edge {
  int to, cost;
};
#define N 100100

//head

int n;
vector<edge> G[N];
int q;
int x[N], y[N], z[N];
vi dat1[N];
vector<P> dat2[N];
queue<int> info1[N], info2[N];
lca<ll> L(N);
SegTree<int> rng(N, [](const int i, const int j)->int{return min(i, j);}, 0x7fffffff);

void dfs(int i=0, int j=-1) {
  list<P> lst;
  for(edge &e:G[i]) {
    int ne = e.to;
    list<P>::iterator itr = lst.begin();
    while(true) {
      if(itr == lst.end()) {
        if(lst.size() < 3) {
          lst.emplace_back(e.cost, ne);
        }
        break;
      }
      if(itr->first > e.cost) {
        lst.emplace(itr, e.cost, ne);
        if(lst.size() > 3) lst.pop_back();
        break;
      }
      itr = next(itr);
    }
  }
  while(lst.size() < 3) lst.emplace_back(0x7fffffff, -10);
  for(edge &e:G[i]) {
    int ne = e.to;
    if(ne == j) continue;
    list<P>::iterator itr = lst.begin();
    while(itr->second == j or itr->second == ne) itr = next(itr);
    rng.update(L.dep[i], itr->first);
    dfs(ne, i);
  }
  for(int x:dat1[i]) {
    if(x == i) {
      info1[i].emplace(0x7fffffff);
      continue;
    }
    list<P>::iterator itr = lst.begin();
    while(itr->second == j) itr = next(itr);
    if(L.dep[i]-1 == L.dep[x]) {
      info1[i].emplace(itr->first);
    }
    else {
      info1[i].emplace(min(itr->first, rng.query(L.dep[x]+1, L.dep[i])));
    }
  }
  for(P x:dat2[i]) {
    list<P>::iterator itr = lst.begin();
    while(true) {
      if(itr->second == -10) break;
      int u = L.lca_(x.first, itr->second);
      int v = L.lca_(x.second, itr->second);
      if((u != i and u != j) or (v != i and v != j)) {
        itr = next(itr);
        continue;
      }
      break;
    }
    info2[i].emplace(itr->first);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n-1) {
    int u, v, w;
    cin >> u >> v >> w;
    u--; v--;
    L.add(u, v, w);
    G[u].emplace_back((edge){v, w});
    G[v].emplace_back((edge){u, w});
  }
  L.init();
  cin >> q;
  rep(i, q) {
    cin >> x[i] >> y[i];
    x[i]--; y[i]--;
    z[i] = L.lca_(x[i], y[i]);
    dat1[x[i]].emplace_back(z[i]);
    dat1[y[i]].emplace_back(z[i]);
    dat2[z[i]].emplace_back(x[i], y[i]);
  }
  dfs();
  rep(i, q) {
    //cout << P(info1[x[i]].front(), info1[y[i]].front()) << ' ' << info2[z[i]].front() << endl;
    int u = min({info1[x[i]].front(), info1[y[i]].front(), info2[z[i]].front()});
    info1[x[i]].pop();
    info1[y[i]].pop();
    info2[z[i]].pop();
    if(u == 0x7fffffff) cout << "-1\n";
    else cout << u*2+L.dist(x[i], y[i]) << '\n';
    //cout << L.dist(x[i], y[i]) << "\n\n";
  }
}
0