結果

問題 No.2337 Equidistant
ユーザー noya2noya2
提出日時 2023-06-02 22:36:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,208 ms / 4,000 ms
コード長 17,572 bytes
コンパイル時間 5,661 ms
コンパイル使用メモリ 292,856 KB
実行使用メモリ 102,036 KB
最終ジャッジ日時 2024-06-09 00:16:40
合計ジャッジ時間 21,496 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 743 ms
58,364 KB
testcase_12 AC 688 ms
58,276 KB
testcase_13 AC 696 ms
58,408 KB
testcase_14 AC 734 ms
58,364 KB
testcase_15 AC 684 ms
58,404 KB
testcase_16 AC 689 ms
58,400 KB
testcase_17 AC 690 ms
58,504 KB
testcase_18 AC 686 ms
58,268 KB
testcase_19 AC 689 ms
58,396 KB
testcase_20 AC 687 ms
58,268 KB
testcase_21 AC 811 ms
102,036 KB
testcase_22 AC 768 ms
57,496 KB
testcase_23 AC 569 ms
58,272 KB
testcase_24 AC 1,091 ms
87,684 KB
testcase_25 AC 597 ms
58,280 KB
testcase_26 AC 1,208 ms
86,932 KB
testcase_27 AC 634 ms
58,252 KB
testcase_28 AC 620 ms
58,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "c.cpp"
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,m,n) for (int i = m; i < int(n); ++i)
#define reb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using pdd = pair<long double, long double>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
template<class T>istream &operator>>(istream &is,vector<T> &v){for(auto &e:v)is>>e;return is;}
template<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}
template<typename T>bool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));}
template<typename T> T rev(const T& str_or_vec){T res = str_or_vec; reverse(res.begin(),res.end()); return res; }
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<typename T>void uniq(vector<T> &v){sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
template<typename T1, typename T2>void print(pair<T1,T2> a);
template<typename T>void print(vector<T> v);
template<typename T>void print(vector<vector<T>> v);
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
void print(unsigned int a){ printf("%u", a); }
void print(unsigned long long a) { printf("%llu", a); }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
template<typename T1,typename T2>void print(pair<T1,T2> a){print(a.first);print(),print(a.second);}
template<typename T>void print(vector<T> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}}
template<typename T>void print(vector<vector<T>> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}}
void yes(){out("Yes");}
void no (){out("No");}
void yn (bool t){if(t)yes();else no();}
void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);}
void o(){out("!?");}

namespace noya2{

const int INF = 1001001007;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long inf = 2e18;
const long double pi = 3.14159265358979323;
const long double eps = 1e-7;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

} // namespace noya2
using namespace noya2;

using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;
void out(mint a){out(a.val());}
void out(vector<mint> a){vector<ll> b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);}
void out(vector<vector<mint>> a){for (auto v : a) out(v);}

#line 2 "rerooting_new.hpp"

#line 4 "rerooting_new.hpp"

namespace noya2 {

using namespace std;

template<class E, class V, E (*merge)(E, E), E (*e)(), E (*put_edge)(V, int), V (*put_vertex)(E, int)>
struct Rerooting {
    struct edge{
        int to, idx, xdi;
    };
    Rerooting (int _n = 0) : n(_n) { es.resize(n);}
    void add_edge(int u, int v, int idx1, int idx2){
        es[u].push_back({v,idx1,idx2});
        es[v].push_back({u,idx2,idx1});
    }
    vector<V> build(int v = 0){
        root = v;
        outs.resize(n);
        subdp.resize(n);
        in.resize(n), up.resize(n);
        int tnow = 0;
        dfs(root,-1,tnow);
        return subdp;
    }
    vector<V> reroot(){
        reverse_edge.resize(n);
        reverse_edge[root] = e();
        reverse_dp.resize(n);
        answers.resize(n);
        bfs(root);
        return answers;
    }
    V get(int r, int v){
        if (r == v) return answers[r];
        if (!(in[v] < in[r] && up[r] <= up[v])) return subdp[v];
        int le = 0, ri = outs[v].size();
        while (ri - le > 1){
            int md = (le + ri) / 2;
            if (in[es[v][md].to] <= in[r]) le = md;
            else ri = md;
        }
        return reverse_dp[es[v][le].to];
    }
    const vector<edge>& operator[](int idx) const { return es[idx]; }
  private:
    int n, root;
    vector<vector<edge>> es;
    vector<vector<E>> outs;
    vector<E> reverse_edge;
    vector<V> subdp, reverse_dp, answers;
    vector<int> in, up;
    void dfs(int v, int p, int &t){
        E val = e();
        in[v] = t++;
        for (auto &u : es[v]){
            if (u.to == p && u.to != es[v].back().to) swap(u,es[v].back());
            if (u.to == p) continue;
            dfs(u.to,v,t);
            E nval = put_edge(subdp[u.to],u.idx);
            outs[v].emplace_back(nval);
            val = merge(val,nval);
        }
        subdp[v] = put_vertex(val,v);
        up[v] = t;
    }
    void bfs(int v){
        int siz = outs[v].size();
        vector<E> lui(siz+1), rui(siz+1);
        lui[0] = e(), rui[siz] = e();
        for (int i = 0; i < siz; i++) lui[i+1] = merge(lui[i],outs[v][i]);
        for (int i = siz-1; i >= 0; i--) rui[i] = merge(outs[v][i],rui[i+1]);
        for (int i = 0; i < siz; i++){
            reverse_dp[es[v][i].to] = put_vertex(merge(merge(lui[i],rui[i+1]),reverse_edge[v]),v);
            reverse_edge[es[v][i].to] = put_edge(reverse_dp[es[v][i].to],es[v][i].xdi);
            bfs(es[v][i].to);
        }
        answers[v] = put_vertex(merge(lui[siz],reverse_edge[v]), v);
    }
};

} // namespace noya2
#line 2 "Tree-core.hpp"

#line 5 "Tree-core.hpp"

namespace noya2 {

using namespace std;

struct naiveTree { // undirected unweighted tree
    naiveTree (int _n = 0) : n(_n){ init();}
    void add_edge(int u, int v, int id = -1){
        es0[u].emplace_back(v);
        es0[v].emplace_back(u);
        es1[u].emplace_back(v,id);
        es1[v].emplace_back(u,id);
    }
    void remake(int new_n){
        es0.clear(); es1.clear(); vis.clear();
        n = new_n;
        init();
    }
    bool yet(int v){ return vis[v] == 0;}
    void visit(int v) { vis[v]++;}
    void reset(int v = -1){ 
        if (v == -1) fill(vis.begin(),vis.end(),0);
        else vis[v] = 0;
    }
    const vector<int>& operator[](int idx) const { return es0[idx];}
    const vector<pair<int,int>>& operator()(int idx) const {return es1[idx];}
  private:
    int n;
    vector<vector<int>> es0;
    vector<vector<pair<int,int>>> es1;
    vector<int> vis;
    void init(){
        es0.resize(n);
        es1.resize(n);
        vis.resize(n,0);
    }
};

struct usefulTree { // rooted tree
    usefulTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();}
    void add_edge(int u, int v){
        es[u].emplace_back(v);
        es[v].emplace_back(u);
    }
    void remake(int new_n, int new_root = 0){
        es.clear(); vis.clear();
        n = new_n, root = new_root;
        init();
    }
    bool yet(int v){ return vis[v] == 0;}
    void visit(int v) { vis[v]++;}
    void reset(int v = -1){ 
        if (v == -1) fill(vis.begin(),vis.end(),0);
        else vis[v] = 0;
    }
    const vector<int>& operator[](int idx) const { return es[idx];}
    int parent(int v){ return par[0][v];}
    int subtree_size(int v){
        if (sub[v] != -1) return sub[v];
        sub[v] = 1;
        for (int child : es[v]){
            if (child != par[0][v]) sub[v] += subtree_size(child);
        }
        return sub[v];
    }
    int depth(int v){ return dep[v];}
    int lca(int u, int v){
        if (dep[u] > dep[v]) swap(u,v);
        for (int i = 0; i < p2size; i++) if ((dep[v] - dep[u]) >> i & 1) v = par[i][v];
        if (u == v) return u;
        for (int i = p2size-1; i >= 0; i--){
            if (par[i][u] != par[i][v]){
                u = par[i][u];
                v = par[i][v];
            }
        }
        return par[0][u];
    }
    int jump_to_root(int from, int d){
        for (int i = 0; i < p2size; i++){
            if ((d >> i & 1) == 1 && from != -1) from = par[i][from];
        }
        return from;
    }
    int jump(int from, int to, int d){
        int l = lca(from,to);
        if (d <= dep[from] - dep[l]){
            return jump_to_root(from,d);
        }
        d -= dep[from] - dep[l];
        if (d <= dep[to] - dep[l]){
            return jump_to_root(to,dep[to]-dep[l]-d);
        }
        return -1;
    }
    vector<int> path(int from, int to){
        int l = lca(from,to);
        int nf = from, nt = to;
        vector<int> pf = {from}, pt = {to};
        while (nf != l){
            nf = par[0][nf];
            pf.emplace_back(nf);
        }
        while (nt != l){
            nt = par[0][nt];
            pt.emplace_back(nt);
        }
        for (int i = pt.size()-2; i >= 0; i--) pf.emplace_back(pt[i]);
        return pf;
    }
    int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[lca(u,v)];}
    void build(){
        par.clear();
        dep.clear();
        sub.clear();
        p2size = 1;
        int _ni = 1; // _ni = 2^(p2size - 1), n-1 <= 2^(p2size - 1) must be holded
        while (_ni < n-1) p2size++, _ni <<= 1;
        par.resize(p2size,vector<int>(n,-1));
        dep.resize(n,-1);
        sub.resize(n,-1);
        queue<int> que;
        que.push(root);
        dep[root] = 0;
        while (!que.empty()){
            int p = que.front(); que.pop();
            for (int to : es[p]){
                if (dep[to] == -1){
                    par[0][to] = p;
                    dep[to] = dep[p] + 1;
                    que.push(to);
                }
            }
        }
        for (int i = 1; i < p2size; i++){
            for (int v = 0; v < n; v++){
                if (par[i-1][v] == -1) continue;
                par[i][v] = par[i-1][par[i-1][v]];
            }
        }
    }
  private:
    int n, root;
    vector<vector<int>> es;
    vector<int> vis;
    int p2size;
    vector<vector<int>> par;
    vector<int> dep, sub;
    void init(){
        es.resize(n);
        vis.resize(n,0);
    }
};

/* point hld (commutative)

vector<S> a(n); // vertex v has a[v]
hldTree g(n);
segtree<S,op,e> seg(n);
rep(i,n) seg.set(i,a[g.ord(i)]); // pre <-> ord (pre(v) = i, ord(i) = v)

update query :
    int v; S x; cin >> v >> x;
    seg.set(g.pre(v),x);

product query :
    S ans = e();
    auto f = [&](int l, int r){
        ans = op(ans,seg.prod(l,r));
    };
    int u, v; cin >> u >> v;
    ans = e();
    g.path_query(u, v, true, f);
    cout << ans << endl;

*/

/* edge hld (commutative)

vector<S> b(n-1); // edge i has b[i]
vector<S> a(n); // vertex v has a[v]
rep(v,n) a[v] = (v == root ? e() : b[g.edge(v)]);

update query :
    int id; S x; cin >> id >> x; // edge id
    seg.set(g.pre(who(id)),x); // edge <-> who (edge(v) = i, who(i) = v)

*/

struct hldTree {
    hldTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();}
    void add_edge(int u, int v, int id){ // id must be 0 <= id < n
        es[u].emplace_back(v,id);
        es[v].emplace_back(u,id);
    }
    void remake(int new_n, int new_root = 0){
        es.clear(); size.clear(); par.clear(); dep.clear(); up.clear(); down.clear();
        nxt.clear(); order.clear(); edges.clear(); whose.clear();
        n = new_n, root = new_root;
        init();
    }
    void build(){
        dfs_init(root);
        int t = 0;
        dfs_hld(root,t);
    }
    int lca(int u, int v){
        while (nxt[u] != nxt[v]){
            if (down[u] < down[v]) swap(u,v);
            u = par[nxt[u]];
        }
        return dep[u] < dep[v] ? u : v;
    }
    int dist(int u, int v){
        return dep[u] + dep[v] - 2 * dep[lca(u,v)];
    }
    int parent(int v){ return par[v];}
    int depth(int v){ return dep[v];}
    int subtree_size(int v){ return size[v];}
    int pre(int v){ return down[v];}
    int post(int v){ return up[v];}
    int ord(int i){ return order[i];}
    int who(int i){ return whose[i];}
    int edge(int v){ return edges[v];}
    template<typename F>
    void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right).
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            s > t ? f(t,s) : f(s,t);
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            s > t ? f(t,s) : f(s,t);
        }
    }
    template<typename F>
    void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u]
        int l = lca(u,v);
        for (auto &p : ascend(u,l)){
            int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1)
            f(s,t); // le > ri ok
        }
        if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point 
        for (auto &p : descend(l,v)){
            int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1)
            f(s,t); // le > ri ok
        }
    }
    template<typename F>
    void subtree_query(int v, bool vertex, const F &f){
        f(down[v] + (vertex ? 0 : 1), up[v]);
    }
    const vector<pair<int,int>>& operator()(int idx) const { return es[idx];}
  private:
    int n, root;
    vector<vector<pair<int,int>>> es;
    vector<int> size, par, dep, up, down, nxt; // nxt[i] : most shallow vertex in connected component of vertex i
    vector<int> order, edges, whose; // order[i] is ith vertex visited on Euler tour, vertex v has edges[v] (root has no edge), edges^-1 = whose
    void init(){
        es.resize(n);
        size.resize(n,0);
        par.resize(n,root);
        dep.resize(n,0);
        up.resize(n,-1);
        down.resize(n,-1);
        nxt.resize(n,root);
        order.resize(n,-1);
        edges.resize(n,-1);
        whose.resize(n,-1);
    }
    void dfs_init(int cur){
        size[cur] = 1;
        for (auto &e : es[cur]){
            if (e.first == par[cur]){
                if (es[cur].size() >= 2 && e.first == es[cur][0].first){
                    swap(es[cur][0],es[cur][1]); // if cur is not leaf, vs[cur][0] is not cur's parent
                }
                else continue;
            }
            par[e.first] = cur;
            edges[e.first] = e.second;
            whose[e.second] = e.first;
            dep[e.first] = dep[cur] + 1;
            dfs_init(e.first);
            size[cur] += size[e.first];
            if (size[e.first] > size[es[cur][0].first]){
                swap(e,es[cur][0]); // to maximize vs[cur][0]'s subtree_size
            }
        }
    }
    void dfs_hld(int cur, int &tnow){
        down[cur] = tnow++; // down[0,...,n-1] is permutation of 0,...,n-1
        order[down[cur]] = cur; 
        for (auto e : es[cur]){
            if (e.first == par[cur]) continue;
            nxt[e.first] = (e.first == es[cur][0].first ? nxt[cur] : e.first);
            dfs_hld(e.first,tnow);
        }
        up[cur] = tnow; // up[0,...,n-1] is NOT permutation, up[*] <= n
    }
    vector<pair<int,int>> ascend(int u, int v) const { // [u,v), depth[u] > depth[v]
        vector<pair<int,int>> res;
        while (nxt[u] != nxt[v]){
            res.emplace_back(down[u],down[nxt[u]]); // [s1,t1], [s2,t2], ...
            u = par[nxt[u]];
        }
        if (u != v) res.emplace_back(down[u],down[v]+1); // [s,t). v is not in the range (down[] is ordered opposite direction of depth)
        return res;
    }
    vector<pair<int,int>> descend(int u, int v) const { // (u,v], depth[u] < depth[v]
        if (u == v) return {};
        if (nxt[u] == nxt[v]){
            return {pair<int,int>(down[u]+1,down[v])}; // (s,t]. u is not in the range
        }
        vector<pair<int,int>> res = descend(u,par[nxt[v]]);
        res.emplace_back(down[nxt[v]],down[v]); // [s1,t1], [s2,t2], ...
        return res;
    }
};

} // namespace noya2
#line 78 "c.cpp"

int op(int a, int b){
    return a + b;
}
int e(){
    return 0;
}
int pute(int e, int i){
    return e;
}
int putv(int v, int i){
    return v + 1;
}

void solve(){
    int n, q; cin >> n >> q;
    usefulTree g(n);
    Rerooting<int,int,op,e,pute,putv> rg(n);
    rep(i,n-1){
        int u, v; cin >> u >> v; u--, v--;
        g.add_edge(u,v);
        rg.add_edge(u,v,i,i);
    }
    g.build();
    rg.build();
    rg.reroot();
    while (q--){
        int u, v; cin >> u >> v; u--, v--;
        int d = g.dist(u,v);
        if (d % 2 == 1){
            out(0);
            continue;
        }
        int c = g.jump(u,v,d/2);
        out(rg.get(u,c) + rg.get(v,c) - n);
    }
}

int main(){
    fast_io();
    int t = 1; //cin >> t;
    while(t--) solve();
}
0