Submission #230694


Source Code Expand

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class LowestCommonAncestor
{
private:
    vector<vector<int> > to;
    vector<int> depth;
    void dfs(const vector<vector<int> >& edges, int curr, int prev, int cnt)
    {
        depth[curr] = cnt;
        if(prev != -1){
            to[curr].push_back(prev);
            int j = prev;
            unsigned k = 0;
            while(k < to[j].size()){
                j = to[j][k];
                to[curr].push_back(j);
                ++ k;
            }
        }
        for(unsigned i=0; i<edges[curr].size(); ++i){
            int next = edges[curr][i];
            if(next != prev)
                dfs(edges, next, curr, cnt + 1);
        }
    }
    int climb(int curr, int dist)
    {
        int i = 0;
        while(dist > 0){
            if(dist % 2 == 1)
                curr = to[curr][i];
            dist /= 2;
            ++ i;
        }
        return curr;
    }
public:
    LowestCommonAncestor(const vector<vector<int> >& edges, int root)
    {
        int n = edges.size();
        to.assign(n, vector<int>());
        depth.resize(n);
        dfs(edges, root, -1, 0);
    }
    int query(int a, int b)
    {
        pair<int, int> dist(0, 0);
        int diff = depth[a] - depth[b];
        if(diff < 0){
            b = climb(b, -diff);
            dist.second += -diff;
        }
        else{
            a = climb(a, diff);
            dist.first += diff;
        }

        if(a == b)
            return dist.first + dist.second;

        int x = to[a].size() - 1;
        while(x >= 0){
            int y = x / 2;
            if(to[a][y] == to[b][y]){
                x = y - 1;
            }
            else{
                a = to[a][y];
                b = to[b][y];
                dist.first += 1 << y;
                dist.second += 1 << y;
                -- x;
            }
        }

        return dist.first + dist.second + 2;
    }
};

int main()
{
    int n;
    cin >> n;
    vector<vector<int> > edges(n);
    for(int i=0; i<n-1; ++i){
        int x, y;
        cin >> x >> y;
        edges[x-1].push_back(y-1);
        edges[y-1].push_back(x-1);
    }
    LowestCommonAncestor lca(edges, 0);

    int q;
    cin >> q;
    while(--q >= 0){
        int a, b;
        cin >> a >> b;
        cout << (lca.query(a-1, b-1) + 1) << endl;
    }

    return 0;
}

Submission Info

Submission Time
Task D - 閉路
User mamekin
Language C++ (G++ 4.6.4)
Score 0
Code Size 2808 Byte
Status WA
Exec Time 733 ms
Memory 22312 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 0 / 30 0 / 70
Status
AC × 3
AC × 8
WA × 4
AC × 14
WA × 13
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 26 ms 916 KB
subtask0_sample02.txt AC 25 ms 796 KB
subtask0_sample03.txt AC 24 ms 924 KB
subtask1_01.txt AC 258 ms 22300 KB
subtask1_02.txt AC 257 ms 22312 KB
subtask1_03.txt AC 26 ms 924 KB
subtask1_04.txt AC 28 ms 920 KB
subtask1_05.txt WA 28 ms 1052 KB
subtask1_06.txt AC 28 ms 880 KB
subtask1_07.txt AC 224 ms 13732 KB
subtask1_08.txt AC 219 ms 13220 KB
subtask1_09.txt WA 226 ms 13720 KB
subtask1_10.txt AC 234 ms 15012 KB
subtask1_11.txt WA 240 ms 16040 KB
subtask1_12.txt WA 233 ms 14620 KB
subtask2_01.txt AC 623 ms 22308 KB
subtask2_02.txt AC 630 ms 22312 KB
subtask2_03.txt AC 314 ms 796 KB
subtask2_04.txt WA 345 ms 808 KB
subtask2_05.txt WA 389 ms 1052 KB
subtask2_06.txt WA 377 ms 924 KB
subtask2_07.txt WA 657 ms 13732 KB
subtask2_08.txt WA 666 ms 13224 KB
subtask2_09.txt WA 709 ms 13608 KB
subtask2_10.txt WA 733 ms 15004 KB
subtask2_11.txt WA 728 ms 16032 KB
subtask2_12.txt WA 714 ms 14632 KB