hihoCoder 1167高等理论计算机科学(LCA)

15 篇文章 0 订阅
6 篇文章 0 订阅

clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以。。。

题目链接:http://hihocoder.com/problemset/problem/1167

由于是中文题目,题意不再赘述。

对于任意两条小精灵的活动路径a和b,二者相交的判断条件为b的两个端点的LCA在a的路径上;那么我们可以首先将每个活动路径端点的LCA离线预处理出来,对每个节点LCA值+1。

然后以某个节点(我选择的是节点1)为根进行深搜,算出一条从节点1到节点x的LCA值和,那么任意路径a(假设其两端点分别是A和B)上的节点个数就是sum[A] + sum[B] - 2 * sum[LCA(A,B)]。

最后,对于某些点,如果它是不止一条路径的LCA,那么我们只需要对最终答案乘以C(LCAnum, 2)的组合数就好。

【PS:clj给出的题解中,采用了点分治+LCA的方式,虽然看懂了题意,但是表示对递归分治之后的路径,如何求出其上的LCAnum,并没有多好的想法,还望巨巨能指点一下,Thx~】

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
#define MAXN 100010
struct Edge {
    int to, next;
} edge[MAXN << 1];
struct Node {
    int to, next, num;
} Query[MAXN << 1];
struct node {
    int u, v, lca;
} input[MAXN];
int totEdge, totQuery, n, m;
int headEdge[MAXN], headQuery[MAXN];
int ancestor[MAXN], father[MAXN], LCAnum[MAXN], sum[MAXN];
bool vis[MAXN];
void addEdge(int from, int to) {
    edge[totEdge].to = to;
    edge[totEdge].next = headEdge[from];
    headEdge[from] = totEdge++;
}
void addQuery(int from, int to, int x) {
    Query[totQuery].to = to;
    Query[totQuery].num = x;
    Query[totQuery].next = headQuery[from];
    headQuery[from] = totQuery++;
}
void init() {
    memset(headEdge, -1, sizeof(headEdge));
    memset(headQuery, -1, sizeof(headQuery));
    memset(father, -1, sizeof(father));
    memset(vis, false, sizeof(vis));
    memset(sum, 0, sizeof(sum));
    memset(LCAnum, 0, sizeof(LCAnum));
    totEdge = totQuery = 0;
}
int find_set(int x) {
    if(x == father[x]) return x;
    else return father[x] = find_set(father[x]);
}
void union_set(int x, int y) {
    x = find_set(x); y = find_set(y);
    if(x != y) father[y] = x;
}
void Tarjan(int u) {
    father[u] = u;
    for(int i = headEdge[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(father[v] != -1) continue;
        Tarjan(v);
        union_set(u, v);
    }
    for(int i = headQuery[u]; i != -1; i = Query[i].next) {
        int v = Query[i].to;
        if(father[v] == -1) continue;
        input[Query[i].num].lca = find_set(v);
    }
}
void DFS(int u, int pre) {
    vis[u] = 1;
    sum[u] = sum[pre] + LCAnum[u];
    for(int i = headEdge[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(vis[v]) continue;
        DFS(v, u);
    }
}
int main() {
    init();
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        addEdge(a, b); addEdge(b, a);
    }
    for(int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        input[i].u = a, input[i].v = b;
        addQuery(a, b, i); addQuery(b, a, i);
    }
    Tarjan(1);
    for(int i = 0; i < m; i++)
        LCAnum[input[i].lca]++;
    DFS(1, 0);
    LL ans = 0;
    for(int i = 0; i < m; i++) {
        ans += (sum[input[i].u] + sum[input[i].v] - 2 * sum[input[i].lca]);
    }
    for(int i = 1; i <= n; i++) {
        ans += (LL)LCAnum[i] * (LCAnum[i] - 1) / 2;
    }
    printf("%lld\n", ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值