[USACO18JAN]MooTube【并查集】

Description

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 (1N100,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 N1 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 N1 pairs of videos he manually considered are connected. Conveniently, FJ has picked his N1 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 ri (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, ki 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=ki.

Output

Output Q lines. On line i, 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 个音乐,给出N1对音乐的关联度(长度),使得任意两个音乐都可以联通,任意两个音乐的关联度为两个音乐间路径长度的最小值。现有 M 个询问,每个询问为ki,vi。 求与 vi 关联度大于等于 ki 的点有多少个。
样例解释:

uv=>w12=>313=>214=>323=>224=>434=>2


做法:
把每个边按 w 从大到小排序,每个询问的ki从大到小排序(保留位置)
然后对于询问的 ki 在所有边里找到 w>=ki 的边,把这些边的端点合并并查集,记录每个并查集下有多少个点,更新这个询问的答案 ans = num[i]-1 所在并查集的点数-1。然后输出答案。

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
struct node {
    int u, v, w;
    node() {}
    node(int W) { w = W; u = v = 0; }
    bool operator<(const node x) {
        if (w == x.w) {
            return u > x.u;
        }
        return w > x.w;
    }
}ed[MAXN];//边
struct qu {
    int id, v, k, ans;
    bool operator<(const qu x) {
        return k > x.k;
    }
}q[MAXN];//询问
int st[MAXN];//并查集
int num[MAXN];//记录并查集的点的数量
int findf(int x)
{
    if (st[x] == x)
        return x;
    return st[x]=findf(st[x]);
}
void merge(int a, int b)
{
    num[findf(a)] += num[findf(b)];
    st[findf(b)] = findf(a);
}
bool vmp(qu x, qu y)
{
    return x.id < y.id;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < MAXN; i++)
    {
        st[i] = i;
        num[i] = 1;
    }
    for (int i = 0; i < n-1; i++)
    {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        if (u > v)
            swap(v, u);
        ed[i].u = u;
        ed[i].v = v;
        ed[i].w = w;
    }
    sort(ed, ed + n);
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d", &q[i].k, &q[i].v);
        q[i].id = i;
    }
    sort(q, q + m);
    int j = 0;
    for (int i = 0; i < m; i++)
    {
        while (ed[j].w >= q[i].k)
        {
            int u = ed[j].u;
            int v = ed[j].v;
            if (findf(u) != findf(v))
            {
                merge(u, v);
            }
            j++;
        }
        q[i].ans = num[findf(q[i].v)] - 1;
    }
    sort(q, q + m,vmp);
    for (int i = 0; i < m; i++)
    {
        printf("%d\n", q[i].ans);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值