luogu4185 [USACO18JAN]MooTube Gold

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值