Aizu 2677 Breadth-First Search by Foxpower(LCA + BFS)

题目链接

题目大致意思就是给出含有n节点的树,1为树根。从1开始bfs搜索整棵树,不过有顺序。深度小的比大的优先,
同一深度的其优先值为其父节点在上一层的搜索顺序,如果同属一个父节点,那么就是节点编号小的优先。然后求出路径总和sum。比如这次搜索到了u,下次需要搜索到v,
那么sum += dis(u, v)。树上的dis就是lca了,因为边权为1.
然后剩下的就是模拟这个过程求解了。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ALL(v) (v).begin(), (v).end()
#define lowbit(x) ((x)&(-x))
#define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin())
#define BitOne(x) __builtin_popcount(x)
#define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define Rep(i, l, r) for (int i = l;i <= r;++i)
#define Rrep(i, r, l) for (int i = r;i >= l;--i)
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5 + 123;
const int LOGN = 20;
int head[maxn], nxt[maxn*2], pnt[maxn*2], ecnt;
inline void addedge(int u,int v) {
    pnt[ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt++;
    pnt[ecnt] = u, nxt[ecnt] = head[v], head[v] = ecnt++;
}
// vector<vector<int> > G;
int n;
int fa[maxn];
struct node {
    int pre, u;
    //pre为其父节点的优先值,pre相同时就是u小的优先
    //同一层访问的优先顺序
    bool operator < (const node& rhs) const {
        return pre < rhs.pre || (pre == rhs.pre && u < rhs.u);
    }
};
struct _LCA {
    int dep[maxn];
    int fa[LOGN][maxn];
    void dfs(int u,int pre, int depth) {
        fa[0][u] = pre;dep[u] = depth;
        for (int i = head[u];~i;i = nxt[i]) {
            if (pnt[i] == pre) continue;
            dfs(pnt[i], u, depth + 1);
        }
    }
    inline void Build(int n) {
        for (int k = 0;k < LOGN - 1;++k) {
            for (int u = 1;u <= n;++u) {
                if (fa[k][u] == -1) fa[k + 1][u] = -1;
                else fa[k + 1][u] = fa[k][ fa[k][u] ];
            }
        }
    }
    inline int upslope(int u,int p) {
        for (int k = 0;k < LOGN - 1;++k)
            if ((p>>k) & 1) u = fa[k][u];
        return u;
    }
    inline int LCA(int u,int v) {
        if (dep[u] < dep[v]) swap(u, v);
        u = upslope(u, dep[u] - dep[v]);
        if (u == v) return u;
        for (int k = LOGN - 1;k >= 0;--k) {
            if (fa[k][u] != fa[k][v]) 
                u = fa[k][u], v = fa[k][v];
        }
        return fa[0][u];
    }
    LL get_dis(int u, int v) {
        int lca = LCA(u, v);
        return (LL)dep[u] + (LL)dep[v] - 2LL * (LL)dep[lca];
    }
}lca;
inline void bfs_init() {
    LL sum = 0;
    vector<node> vec;
    for (int i = head[1];~i;i = nxt[i]) {
        int v = pnt[i];
        vec.push_back(node{
   
   1, v});
    }
    int last = 1;//上一层访问的最后一个点的编号
    while(!vec.empty()) {
        vec.push_back(node{-1, last});
        sort(ALL(vec));
        int size = vec.size();
        for (int i = 1;i < size;++i) {
            sum += (LL)lca.get_dis(vec[i].u, vec[i - 1].u);
        }
        vector<node> temp;
        Rep(i, 1, size - 1) {
            int u = vec[i].u;
            for (int j = head[u];~j;j = nxt[j]) {
                int v = pnt[j];
                if (v == fa[u]) continue;
                temp.push_back(node{i + 1, v});
            }
        }
        last = vec.back().u;
        vec.clear();
        vec = temp;
    }
    cout << sum << endl;
}

int main(int argc, const char * argv[])
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // ios::sync_with_stdio(false);
    // cout.sync_with_stdio(false);
    // cin.sync_with_stdio(false);

    while(~scanf("%d", &n)) {
        // G.clear();
        // G.resize(n + 2);
        memset(head, -1, sizeof head), ecnt = 0;
        Rep(i, 2, n) {
            scanf("%d", &fa[i]);
            addedge(i, fa[i]);
        }
        lca.dfs(1, -1, 0);
        lca.Build(n);
        bfs_init();
    }

    // showtime;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KIJamesQi/article/details/52281566