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;
}