HDU 6074 Phone Call (LCA+并查集, 2017 Multi-Univ Training Contest 4)

29 篇文章 0 订阅
7 篇文章 0 订阅

Problem

There are n houses in Bytetown, labeled by 1,2,…,n. In each house, there is a person living here. Little Q lives in house 1. There are n−1 bidirectional streets connecting these houses, forming a tree structure. In this problem, S(u,v) denotes the house set containing all the houses on the shortest path from house u to house v.

The Bytetown’s phone line network consists of m different lines. The i-th line can be expressed as 5 integers ai,bi,ci,di,wi , which means for every two different houses u and v from set S(ai,bi)S(ci,di) , u and v can have a phone call costing wi dollars.

Little Q is now planning to hold a big party in his house, so he wants to make as many as possible people known. Everyone known the message can make several phone calls to others to spread the message, but nobody can leave his house.

Please write a program to figure out the maximum number of people that can join the party and the minimum total cost to reach that maximum number. Little Q should be counted in the answer.

Idea

LCA + 并查集

先根据所给的边做一遍 LCA 的初始化。

将电话连线按照 wi 从小到大排序,每次选择代价最小的组,分别求 LCAab=lca(ai,bi) LCAcd=lca(ci,di) ai,bi,ci,di 分别向上递归直到对应的 LCA 。利用并查集维护最小生成树的点集,并将其作为新边记录到边集中(这个边集在代码中用 vector<pair<int, int> > g[N]; 维护)。同时应更新遍历到的每个点的父节点 (取当前父节点与 LCA 中深度较小的作为该点新的父节点),防止重复操作的情况。

在处理完所有连接后,从 1 点跑一遍搜索查找所有连通的点,统计点数与边权和。

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
int T, n, m, grp[N], fa[N], pot;
long long ans;
vector<pair<int, int> > g[N];
struct Node {
    int a, b, c, d, w;
} pc[N];
bool cmp(Node a, Node b) {
    return a.w < b.w;
}
int find(int x) {   return grp[x] = x == grp[x] ? x : find(grp[x]); }
bool Union(int x, int y) {
    int fx = find(x),   fy = find(y);
    if(fx == fy)    return false;
    if(fx > fy) swap(fx, fy);
    grp[fy] = fx;
    return true;
}

struct Edge { 
    int nxt, to, w;
} e[N*2];
int head[N], cnt;
void addedge(int u, int v) {
    e[++cnt].nxt = head[u];
    e[cnt].to = v;
    head[u] = cnt;
}
const int maxh = 20;
int dep[N];
int anc[N][20];
void dfs(int rt) {
    static int Stack[N];
    int top = 0;
    dep[rt] = 1;
    for(int i=0;i<maxh;i++)
        anc[rt][i] = rt;
    Stack[++top] = rt;
    while(top) {
        int x = Stack[top];
        if(x != rt) {
            for(int i=1, y;i<maxh;i++)
                y = anc[x][i-1],    anc[x][i] = anc[y][i-1];
        }
        for(int &i=head[x];i;i=e[i].nxt) {
            int y = e[i].to;
            if(y != anc[x][0]) {
                dep[y] = dep[x] + 1;
                anc[y][0] = x;
                Stack[++top] = y;
            }
        }
        while(top && head[Stack[top]] == 0) top--;
    }
}
void swim(int &x, int H) {
    for(int i=0;H;i++) {
        if(H & 1)   x = anc[x][i];
        H /= 2;
    }
}
int lca(int x, int y) {
    int i;
    if(dep[x] > dep[y]) swap(x, y);
    swim(y, dep[y]-dep[x]);
    if(x == y)  return x;
    for(;;) {
        for(i=0;anc[x][i] != anc[y][i];i++);
        if(i == 0)  return anc[x][0];
        x = anc[x][i-1];
        y = anc[y][i-1];
    }
    return -1;
}
void kruscal(int x, int LCA, int w){
    int tmp;
    while(dep[x] > dep[LCA]) {
        if(Union(x, LCA)) {
            g[LCA].push_back(make_pair(x, w));
            g[x].push_back(make_pair(LCA, w));
        }
        tmp = x;
        x = fa[x];
        fa[tmp] = dep[x] > dep[LCA] ? LCA : x;
    }   
}
void search(int rt, int fa) {
    pot++;
    for(int i=0;i<g[rt].size();i++)
    {
        if(g[rt][i].first == fa)    continue;
        ans += g[rt][i].second;
        search(g[rt][i].first, rt);
    }
}
int main()
{
    scanf("%d", &T);
    while(T-- && scanf("%d %d", &n, &m)!=EOF)
    {
        cnt = 0;
        memset(head, 0, sizeof(head));
        for(int i=1;i<=n;i++) {
            grp[i] = i;
            g[i].clear();
        }
        for(int i=1, u, v;i<n;i++)
        {
            scanf("%d %d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        dfs(1);
        for(int i=1;i<=n;i++)
            fa[i] = anc[i][0];
        fa[1] = 0;
        for(int i=1;i<=m;i++)
            scanf("%d %d %d %d %d", &pc[i].a, &pc[i].b, &pc[i].c, &pc[i].d, &pc[i].w);
        sort(pc+1, pc+m+1, cmp);
        for(int i=1;i<=m;i++)
        {
            int LCA_ab = lca(pc[i].a, pc[i].b);
            kruscal(pc[i].a, LCA_ab, pc[i].w);
            kruscal(pc[i].b, LCA_ab, pc[i].w);
            int LCA_cd = lca(pc[i].c, pc[i].d);
            kruscal(pc[i].c, LCA_cd, pc[i].w);
            kruscal(pc[i].d, LCA_cd, pc[i].w);

            if(Union(LCA_ab, LCA_cd)) {
                g[LCA_ab].push_back(make_pair(LCA_cd, pc[i].w));
                g[LCA_cd].push_back(make_pair(LCA_ab, pc[i].w));
            }
        }

        ans = 0;    pot = 0;
        search(1, 0);
        printf("%d %lld\n", pot, ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值