HDU6074 Phone Call(并查集,lca)

本文探讨了一种通过优化电话线路网络来确保信息传播的最大覆盖范围和最小成本的方法。利用并查集和最小生成树思想,文章详细介绍了如何连接不同房屋以达到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Phone Call

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 395    Accepted Submission(s): 163


Problem Description
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  n1  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.



Picture from Wikimedia Commons


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.
 

Input
The first line of the input contains an integer  T(1T15) , denoting the number of test cases.

In each test case, there are  2  integers  n,m(1n,m100000)  in the first line, denoting the number of houses and phone lines.

For the next  n1  lines, each line contains two integers  u  and  v , denoting a bidirectional edge between node  u  and  v .

For the next  m  lines, each line contains  5  integers  ai,bi,ci,di,wi(1ai,bi,ci,din,1wi109) , denoting a phone line.
 

Output
For each test case, print a single line containing two integers, denoting the maximum number of people that can join the party and the minimum total cost to reach that maximum number.
 

Sample Input
  
1 5 2 1 2 1 3 2 4 2 5 1 3 2 4 100 2 2 4 2 10
 

Sample Output
  
4 210
Hint
Step 1 : 1 make a phone call to 2 using line 1, the cost is 100. Step 2 : 1 make a phone call to 3 using line 1, the cost is 100. Step 3 : 2 make a phone call to 4 using line 2, the cost is 10.
 

Source
 

Recommend
liuyiding
 


题意:有一颗树,每次给四个点,a,b,c,d.告诉你a到b路径上的节点可以相互到达同时和同时c到d路径上的节点可以相互到达。且花费为w。

求出1最多可以到达几个节点,并且求出最小花费。

整个过程类似最小生成树的合并过程,所以按照花费从小到大排序,首先把两个端点和其LCA合并,在把两个LCA合并。

并查集维护这个过程。


#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=400000+10;
const int mod=1e9+7;
struct edge{
    int to,next;
}e[MAXN<<1];
int head[MAXN];
int tot=0;
void add(int u,int v){
    e[tot].to=v;
    e[tot].next=head[u];
    head[u]=tot++;
}

int dep[MAXN];
int fir[MAXN];
int que[MAXN<<1];
int p[MAXN];
int st[MAXN][20];
int vis[MAXN];
int cnt=0;

void dfs(int u,int d){
    vis[u]=1;
    que[++cnt]=u;fir[u]=cnt;dep[cnt]=d;
    for(int k=head[u];k!=-1;k=e[k].next){
        int v=e[k].to;
        if(!vis[v]){
            p[v]=u;
            dfs(v,d+1);
            que[++cnt]=u;dep[cnt]=d;
        }
    }
}
void ST(int n){
    for(int i=1;i<=n;i++)
        st[i][0]=i;
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            int a=st[i][j-1],b=st[i+(1<<(j-1))][j-1];
            st[i][j]=dep[a]<dep[b]?a:b;
        }
    }
}
int RMQ(int l,int r){
    int k=0;
    while((1<<(k+1))<=r-l+1){
        k++;
    }
    int a=st[l][k],b=st[r-(1<<k)+1][k];
    return dep[a]<dep[b]?a:b;
}

int lca(int u,int v){
    int x=fir[u],y=fir[v];
    if(x>y)
        swap(x,y);
    int res=RMQ(x,y);
    return que[res];
}


struct node{
    int a,b,c,d;
    long long w;
}s[MAXN];
int cmp(node x,node y){
    return x.w<y.w;
}

int par[MAXN],up[MAXN],cn[MAXN];
long long w[MAXN];
int findFa(int x){
    if(par[x]==x)
        return x;
    else
        return par[x]=findFa(par[x]);
}
int findUp(int x){
    if(up[x]==x)
        return x;
    else
        return up[x]=findUp(up[x]);
}

void Union(int x,int y,long long cost){
    x=findFa(x),y=findFa(y);
    if(x==y)
        return;
    par[x]=y;
    cn[y]+=cn[x];
    w[y]+=w[x]+cost;
}

void merge(int u,int v,long long cost){
    while(1){
        u=findUp(u);
        if(dep[fir[u]]<=dep[fir[v]]){
            //cout<<u<<" "<<v<<" "<<dep[fir[u]]<<" "<<dep[que[fir[v]]]<<endl;
            return;
        }
        Union(u,p[u],cost);
        up[u]=p[u];
    }
}

void solve(node x){
    int LCA=lca(x.a,x.b);
    merge(x.a,LCA,x.w);
    merge(x.b,LCA,x.w);
    LCA=lca(x.c,x.d);
    merge(x.c,LCA,x.w);
    merge(x.d,LCA,x.w);
    Union(x.a,x.c,x.w);
}

void init(int n){
    tot=0;
    cnt=0;
    p[1]=0;
    memset(head,-1,sizeof head);
    memset(vis,0,sizeof vis);
    for(int i=0;i<=n;i++){
        par[i]=up[i]=i;
        cn[i]=1;
        w[i]=0;
    }
}

int main(){
    int t;
    //freopen("1008.in","r",stdin);
    cin>>t;
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0;i<n-1;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs(1,0);
        ST(2*n-1);
        for(int i=0;i<m;i++){
            scanf("%d%d%d%d%lld",&s[i].a,&s[i].b,&s[i].c,&s[i].d,&s[i].w);
        }
        sort(s,s+m,cmp);
        for(int i=0;i<m;i++){
            solve(s[i]);
        }
        printf("%d %lld\n",cn[findFa(1)],w[findFa(1)]);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值