#line 1 "Contests/Dummy1/main.cpp" #define aclmodint #define aclsegtree #include using namespace atcoder; #line 1 "library/me/template.cpp" // This is a dummy line #ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #include using namespace std; #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++) #define ALL(a) (a).begin(),(a).end() #define END(...) { print(__VA_ARGS__); return; } #ifdef ONLINE_JUDGE #define DBG(...) {} #else #define DBG(a) { cerr << #a << ": "; dbg(a); } #endif using VI = vector; using VVI = vector; using VVVI = vector; using ll = long long; using VL = vector; using VVL = vector; using VVVL = vector; using VD = vector; using VVD = vector; using VVVD = vector; using VS = vector; using VVS = vector; using VVVS = vector; using VC = vector; using VVC = vector; using VVVC = vector; using P = pair; using VP = vector

; using VVP = vector; using VVVP = vector; using LP = pair; using VLP = vector; using VVLP = vector; using VVVLP = vector; template using PQ = priority_queue; template using GPQ = priority_queue, greater>; constexpr int INF = 1001001001; constexpr ll LINF = 1001001001001001001ll; constexpr int DX[] = {1, 0, -1, 0}; constexpr int DY[] = {0, 1, 0, -1}; #ifdef aclmodint using MI7 = modint1000000007; using V7 = vector; using VV7 = vector; using VVV7 = vector; using MI3 = modint998244353; using V3 = vector; using VV3 = vector; using VVV3 = vector; ostream &operator<<(ostream &os, const modint &x) { os << x.val(); return os; } ostream &operator<<(ostream &os, const MI3 &x) { os << x.val(); return os; } ostream &operator<<(ostream &os, const MI7 &x) { os << x.val(); return os; } istream &operator>>(istream &is, modint &x) { int y; is >> y; x = y; return is; } istream &operator>>(istream &is, MI3 &x) { int y; is >> y; x = y; return is; } istream &operator>>(istream &is, MI7 &x) { int y; is >> y; x = y; return is; } #endif void print() { cout << '\n'; } template void print(const T &t) { cout << t << '\n'; } template void print(const Head &head, const Tail &... tail) { cout << head << ' '; print(tail...); } #ifdef ONLINE_JUDGE template void dbg(const Args &... args) {} #else void dbg() { cerr << '\n'; } template void dbg(const T &t) { cerr << t << '\n'; } template void dbg(const Head &head, const Tail &... tail) { cerr << head << ' '; dbg(tail...); } #endif template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 >& p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != (int) v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } 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 pair> compress(const vector &a) { int n = a.size(); vector x; REP(i, n) x.push_back(a[i]); sort(ALL(x)); x.erase(unique(ALL(x)), x.end()); VI res(n); REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin(); return make_pair(res, x); } template pair, vector> factorial(int n) { vector res(n+1), rev(n+1); res[0] = 1; REP(i, n) res[i+1] = res[i] * (i+1); rev[n] = 1 / res[n]; for(int i = n; i > 0; i--) { rev[i-1] = rev[i] * i; } return make_pair(res, rev); } #ifdef aclsegtree template struct value_size { S value; int size; }; template S min_op(S l, S r) { return min(l, r); }; template S max_op(S l, S r) { return max(l, r); }; template S sum_op(S l, S r) { return l + r; }; template value_size sum_op_size(value_size l, value_size r) { return {l.value + r.value, l.size + r.size}; }; template S min_e() { return numeric_limits::max(); }; template S max_e() { return numeric_limits::min(); }; template S sum_e() { return 0; } template value_size sum_e_size() { return {0, 0}; } template value_size min_e_size() { return {numeric_limits::max(), 0}; } template value_size max_e_size() { return {numeric_limits::min(), 0}; } template S chmin_mapping(F f, S x) { return min(x, f); } template S chmax_mapping(F f, S x) { return max(x, f); } template S add_mapping(F f, S x) { return x + f; } template value_size add_mapping_size(F f, value_size x) { return {x.value + x.size * f, x.size}; } template S change_mapping(F f, S x) { return (f == ID? x : f); } template value_size change_mapping_size(F f, value_size x) { value_size ret = {f * x.size, x.size}; return (f == ID? x : ret); } template S linear_mapping(pair f, S x) { return x * f.first + f.second; } template value_size linear_mapping_size(pair f, value_size x) { return {x.value * f.first + x.size * f.second, x.size}; } template F chmin_composition(F f, F g) { return min(f, g); } template F chmax_composition(F f, F g) { return max(f, g); } template F add_composition(F f, F g) { return f + g; } template F change_composition(F f, F g) { return (f == ID? g : f); } template pair linear_composition(pair f, pair g) { return {f.first * g.first, f.first * g.second + f.second}; } template F chmin_id() { return numeric_limits::max(); } template F chmax_id() { return numeric_limits::min(); } template F add_id() { return 0; } template F change_id() { return ID; } template pair linear_id() { return {1, 0}; } template using RSumQ = segtree, sum_e>; template using RMaxQ = segtree, max_e>; template using RMinQ = segtree, min_e>; template using RAddSumQ = lazy_segtree, sum_op_size, sum_e_size, F, add_mapping_size, add_composition, add_id>; template using RAddMinQ = lazy_segtree, min_e, F, add_mapping, add_composition, add_id>; template using RAddMaxQ = lazy_segtree, max_e, F, add_mapping, add_composition, add_id>; template using RMinMinQ = lazy_segtree, min_e, F, chmin_mapping, chmin_composition, chmin_id>; template using RMaxMaxQ = lazy_segtree, max_e, F, chmax_mapping, chmax_composition, chmax_id>; template using RChangeMinQ = lazy_segtree, min_e, F, change_mapping, change_composition, change_id>; template using RChangeMaxQ = lazy_segtree, max_e, F, change_mapping, change_composition, change_id>; template using RChangeSumQ = lazy_segtree, sum_op_size, sum_e_size, F, change_mapping_size, change_composition, change_id>; template using RLinearMinQ = lazy_segtree, min_e, pair, linear_mapping, linear_composition, linear_id>; template using RLinearMaxQ = lazy_segtree, max_e, pair, linear_mapping, linear_composition, linear_id>; template using RLinearSumQ = lazy_segtree, sum_op_size, sum_e_size, pair, linear_mapping_size, linear_composition, linear_id>; #endif #line 7 "Contests/Dummy1/main.cpp" void solve(){ int n; cin >> n; VVI g(n); REP(i, n - 1) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } VI par(n), gs(n), sg, dps(n), sz(n), top(n); auto dfs1 = [&](auto self, int from, int p) -> void { sz[from] = 1; for(int to : g[from]) { if(to == p) continue; par[to] = from; dps[to] = dps[from] + 1; self(self, to, from); sz[from] += sz[to]; } }; par[0] = -1; dps[0] = 0; dfs1(dfs1, 0, -1); auto dfs2 = [&](auto self, int from, int p) -> void { P mx = {0, -1}; for(int to : g[from]) { if(to == p) continue; chmax(mx, P(sz[to], to)); } if(mx.second == -1) return; gs[mx.second] = sg.size(); sg.push_back(mx.second); top[mx.second] = top[from]; self(self, mx.second, from); for(int to : g[from]) { if(to == p) continue; if(to == mx.second) continue; gs[to] = sg.size(); sg.push_back(to); top[to] = to; self(self, to, from); } }; gs[0] = 0; sg.push_back(0); top[0] = 0; dfs2(dfs2, 0, -1); RAddSumQ seg(vector>(n, {0, 1})); int q; cin >> q; ll ans = 0; REP(_, q) { int a, b; cin >> a >> b; a--; b--; while(top[a] != top[b]) { if(dps[top[a]] < dps[top[b]]) swap(a, b); seg.apply(gs[top[a]], gs[a] + 1, 1ll); ans += seg.prod(gs[top[a]], gs[a] + 1).value; a = par[top[a]]; } if(dps[a] < dps[b]) swap(a, b); seg.apply(gs[b], gs[a] + 1, 1); ans += seg.prod(gs[b], gs[a] + 1).value; } print(ans); } // generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) int main() { // Fasterize input/output script ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(100); // scanf/printf user should delete this fasterize input/output script int t = 1; //cin >> t; // comment out if solving multi testcase for(int testCase = 1;testCase <= t;++testCase){ solve(); } return 0; }