uasco MooTube (并查集)

题面:

In his spare time, Farmer John has created a new video-sharing service, which he names MooTube. On MooTube, Farmer John’s cows can record, share, and discover many amusing videos. His cows already have posted N videos (1≤N≤100,000 ), conveniently numbered 1…N . However, FJ can’t quite figure out how to help his cows find new videos they might like.

FJ wants to create a list of “suggested videos” for every MooTube video. This way, cows will be recommended the videos most relevant to the ones they already watch.

FJ devises a metric of “relevance,” which determines, as the name suggests, how relevant two videos are to each other. He picks N−1 pairs of videos and manually computes their pairwise relevance. Then, FJ visualizes his videos as a network, where each video is a node and the N−1 pairs of videos he manually considered are connected. Conveniently, FJ has picked his N−1 pairs so that any video can be reached from any other video along a path of connections in exactly one way. FJ decides that the relevance of any pair of videos should be defined as the minimum relevance of any connection along this path.

Farmer John wants to pick a value K so that next to any given MooTube video, all other videos with relevance at least K to that video will be suggested. However, FJ is worried that too many videos will be suggested to his cows, which could distract them from milk production! Therefore, he wants to carefully set an appropriate value of K. Farmer John would like your help answering a number of questions about the suggested videos for certain values of K.

INPUT
The first line of input contains N and Q (1≤Q≤100,000 ).The next N−1 lines each describe a pair of videos FJ manually compares. Each line includes three integers pi , qi , and riri (1≤pi,qi≤N,1≤ri≤1,000,000,000 ), indicating that videos pi and qi are connected with relevance ri .

The next Q lines describe Farmer John’s Q questions. Each line contains two integers, kiki and vi (1≤ki≤1,000,000,000,1≤vi≤N ), indicating that FJ’s ith question asks how many videos will be suggested to viewers of video vi if K=k.

OUTPUT
Output Q lines. On line ii, output the answer to FJ’s ith question.

SAMPLE INPUT:
4 3
1 2 3
2 3 2
2 4 4
1 2
4 1
3 1

SAMPLE OUTPUT:
3
0
2


题意:

一棵n个点的树,每条边有一个权值。有q个询问,形式:k v,表示从k点出发,在只能通过边权大于等于k的限制下,可以访问多少个其他节点。

思路:

简单版数据范围很小,可以直接On*q暴力过,这个加强版就不行了。观察所有的询问,可以发现如果先询问一个大的k,然后再询问一个小的k,如果按照暴力dfs的方法,会有很多重复的搜索,然后考虑如何避免这些重复的搜索。类似最小生成树库鲁斯卡尔的写法,先把所有的边按照权值排序,优先权值大的。利用并查集来合并当前已经可以合并的点,查询的时候答案就是当前点所在的树的大小。

参考代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;;
struct node{
    int u,v,w;
    bool operator <(const node &a)const{
        return w>a.w;
    }
}edge[N];

struct node2{
    int k,v,id;
    bool operator < (const node2 &a)const{
        return k>a.k;
    }
}q[N];


int fa[N],sz[N];

int findfa(int x){
    return fa[x]=fa[x]==x?x:findfa(fa[x]);
}

int ans[N];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    }
    sort(edge+1,edge+n);
    for(int i=1;i<=n;i++){
        fa[i]=i;
        sz[i]=1;
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&q[i].k,&q[i].v);
        q[i].id=i;
    }
    sort(q+1,q+1+m);
    int j=1;
    for(int i=1;i<=m;i++){
        int x=q[i].v;
        int k=q[i].k;
        int fu=0,fv=0,fx=0;
        for(;j<n;j++){
            int u=edge[j].u;
            int v=edge[j].v;
            int w=edge[j].w;
            if(w<k)break;
            fu=findfa(u),fv=findfa(v);
            if(fu!=fv){
                sz[fu]+=sz[fv];
                fa[fv]=fu;
            }
        }
        fx=findfa(x);
        ans[q[i].id]=sz[fx]-1;
    }
    for(int i=1;i<=m;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值