Submission #1758488


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v)) os<<*i<<(i==end(v)-1?"":"\n"); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
    is>>p.first>>p.second; return is; }
template <class T> bool chmax(T &a,const T &b) {if(a<b) {a=b;return 1;} return 0;}
template <class T> bool chmin(T &a,const T &b) {if(b<a) {a=b;return 1;} return 0;}
template <class T> using heap = priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

struct RMQ {
    int n;
    vector<pii> data;
    RMQ(int n_=0) {
        n=1;
        while(n<n_) n<<=1;
        data.resize(n<<1,{INF,-1});
    }
    void update(int i,int x) {
        i+=n-1;
        data[i]={x,i-(n-1)};
        while(i>0) {
            i=(i-1)/2;
            data[i]=min(data[i*2+1],data[i*2+2]);
        }
    }
    pii query(int a,int b) {
        return query(a,b,0,0,n);
    }
    pii query(int a,int b,int k,int l,int r) {
        if(r<=a or b<=l) return {INF,-1};
        if(a<=l and r<=b) return data[k];
        pii ml=query(a,b,k*2+1,l,(r+l)/2);
        pii mr=query(a,b,k*2+2,(r+l)/2,r);
        return min(ml,mr);
    }
};
signed main() {
    int n;
    cin>>n;
    
    vector<vector<int>> g(n);
    REP(i,n-1) {
        int x,y;
        cin>>x>>y;
        x--,y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    vector<int> id(n);
    vector<int> vs(n*2);
    vector<int> depth(n*2);
    RMQ rmq(n*2);

    int k=0;
    function<void(int,int,int)> dfs=[&](int v,int p,int d) {
        id[v]=k;
        vs[k]=v;
        depth[k]=d;
        rmq.update(k,d);
        k++;
        REP(i,SZ(g[v])) {
            if(g[v][i]!=p) {
                dfs(g[v][i],v,d+1);
                vs[k]=v;
                depth[k]=d;
                rmq.update(k,d);
                k++;
            }
        }
    };

    dfs(0,-1,0);

    auto lca=[&](int u,int v) {
        return vs[rmq.query(min(id[u],id[v]),max(id[u],id[v])+1).second];
    };

    int Q;
    cin>>Q;
    while(Q--) {
        int a,b;
        cin>>a>>b;
        a--,b--;
        int l=lca(a,b);
        int dist=(depth[id[a]]-depth[id[l]])+(depth[id[b]]-depth[id[l]]);
        cout<<dist+1<<endl;
    }
    return 0;
}

Submission Info

Submission Time
Task D - 閉路
User algon
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3423 Byte
Status AC
Exec Time 153 ms
Memory 29440 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 12
AC × 27
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt
Subtask2 subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 1 ms 256 KB
subtask0_sample02.txt AC 1 ms 256 KB
subtask0_sample03.txt AC 1 ms 256 KB
subtask1_01.txt AC 52 ms 28800 KB
subtask1_02.txt AC 134 ms 28800 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 2 ms 384 KB
subtask1_06.txt AC 2 ms 512 KB
subtask1_07.txt AC 58 ms 18432 KB
subtask1_08.txt AC 59 ms 18304 KB
subtask1_09.txt AC 59 ms 18432 KB
subtask1_10.txt AC 55 ms 18432 KB
subtask1_11.txt AC 60 ms 18432 KB
subtask1_12.txt AC 58 ms 18432 KB
subtask2_01.txt AC 88 ms 29440 KB
subtask2_02.txt AC 103 ms 29312 KB
subtask2_03.txt AC 28 ms 512 KB
subtask2_04.txt AC 41 ms 512 KB
subtask2_05.txt AC 53 ms 768 KB
subtask2_06.txt AC 53 ms 768 KB
subtask2_07.txt AC 153 ms 18688 KB
subtask2_08.txt AC 150 ms 18688 KB
subtask2_09.txt AC 139 ms 18688 KB
subtask2_10.txt AC 149 ms 18816 KB
subtask2_11.txt AC 136 ms 18816 KB
subtask2_12.txt AC 133 ms 18816 KB