hihoCoder 1167高等理論計算機科學(LCA)

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章