hihoCoder 1167高等理論電腦科學(LCA)

來源:互聯網
上載者:User

標籤:

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

 

  

 

hihoCoder 1167高等理論電腦科學(LCA)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.