luogu4185 [USACO18JAN]MooTube Gold

本文介绍了一种视频推荐算法的实现,该算法基于Farmer John创建的MooTube平台,通过计算视频间的相关性来为用户提供个性化推荐。具体算法采用并查集优化处理大量查询,确保了高效的推荐体验。

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

http://www.elijahqi.win/2018/02/02/luogu4185-usaco18janmootube-gold/
题目描述

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
NN

N videos (
1≤N≤100,0001 \leq N \leq 100,000

1≤N≤100,000 ), conveniently numbered
1…N1 \ldots N

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−1N-1

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−1N-1

N−1 pairs of videos he manually considered are connected. Conveniently, FJ has picked his
N−1N-1

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
KK

K so that next to any given MooTube video, all other videos with relevance at least
KK

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
KK

K . Farmer John would like your help answering a number of questions about the suggested videos for certain values of
KK

K .
输入输出格式

输入格式:
The first line of input contains
NN

N and
QQ

Q (
1≤Q≤100,0001 \leq Q \leq 100,000

1≤Q≤100,000 ).

The next
N−1N-1

N−1 lines each describe a pair of videos FJ manually compares. Each line includes three integers
pip_i

pi​ ,
qiq_i

qi​ , and
rir_i

ri​ (
1≤pi,qi≤N,1≤ri≤1,000,000,0001 \leq p_i, q_i \leq N, 1 \leq r_i \leq 1,000,000,000

1≤pi​,qi​≤N,1≤ri​≤1,000,000,000 ), indicating that videos
pip_i

pi​ and
qiq_i

qi​ are connected with relevance
rir_i

ri​ .

The next
QQ

Q lines describe Farmer John’s
QQ

Q questions. Each line contains two integers,
kik_i

ki​ and
viv_i

vi​ (
1≤ki≤1,000,000,000,1≤vi≤N1 \leq k_i \leq 1,000,000,000, 1 \leq v_i \leq N

1≤ki​≤1,000,000,000,1≤vi​≤N ), indicating that FJ’s
ii

i th question asks how many videos will be suggested to viewers of video
viv_i

vi​ if
K=kiK = k_i

K=ki​ .

输出格式:
Output
QQ

Q lines. On line
ii

i , output the answer to FJ’s
ii

i th question.
输入输出样例

输入样例#1: 复制

4 3
1 2 3
2 3 2
2 4 4
1 2
4 1
3 1

输出样例#1: 复制

3
0
2
Farmer John finds that videos one and two have relevance three, that videos two and three have relevance two, and that videos two and four have relevance four. Based on this, videos one and three have relevance min(3, 2) = 2, videos one and four have relevance min(3, 4) = 3, and videos three and four have relevance min(2, 4) = 2.

Farmer John wants to know how many videos will be suggested from video two if K=1 K = 1 , from video one if K=3 K = 3 , and from video one if K=4 K = 4 . We see that with K=1 K = 1 , videos 1, 3, and 4 will be suggested on video two. With K=4 K = 4 , no videos will be suggested from video one. With K=3 K = 3 , however, videos 2 and 4 will be suggested from video one.

题意:每次给一个起点 然后询问和他相连的点的边权比k大的点有几个 路径边权是两点连接的所有路径上最小值

所以我针对询问离线然后 从大到小进行询问 每次把他加入并查集方便统计答案 一开始犹豫 这个大小怎么记因为我没有递归后来认真考虑了下 其实无所谓我只需要直到一个完整块的大小即可

#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
struct node{
    int x,y,z;
}data[N];
struct node1{
    int k,v,id;
}qr[N];
inline bool cmp(const node1 &a,const node1 &b){return a.k>b.k;}
inline bool cmp1(const node &a,const node &b){return a.z>b.z;}
int fa[N],size[N],ans[N],n,q;
inline int find(int x){while(x!=fa[x]) x=fa[x]=fa[fa[x]];return x;}
int main(){
//  freopen("mootube.in","r",stdin);
//  freopen("mootube.out","w",stdout);
    n=read();q=read();
    for (int i=1;i<n;++i) data[i].x=read(),data[i].y=read(),data[i].z=read();
    for (int i=1;i<=q;++i) qr[i].k=read(),qr[i].v=read(),qr[i].id=i;
    sort(qr+1,qr+q+1,cmp);sort(data+1,data+n+1,cmp1);
    for (int i=1;i<=n;++i) fa[i]=i,size[i]=1;int now=1;
    for (int i=1;i<=q;++i){
        while(now<=n&&data[now].z>=qr[i].k){
            int x=find(data[now].x),y=find(data[now].y);++now;
            if (x==y) continue;fa[x]=y;size[y]+=size[x];
        }ans[qr[i].id]=size[find(qr[i].v)]-1;
    }
    for (int i=1;i<=q;++i) printf("%d\n",ans[i]);
    return 0;
}
### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值