BZOJ又不给题面...

Luogu的翻译看不下去...

题意简述

有一个$n$个节点的树,边有权值,定义两个节点之间的距离为两点之间的路径上的最小边权

给你$Q$个询问,问你与点$v$的距离超过$k$的点有多少个

$n,Q<=100000$

题解

很妙的做法。

并查集+离线

显然可以把询问离线,按K值排序

处理距离的话可以使用并查集,并不需要带权,只需要把边也按权值排序,用并查集维护。

具体做法:对每个点维护一个$siz$数组表示与它联通的节点数目,用类似双指针的方法把符合规则的边的两端点并起来,$siz$也顺便并起来就好

对于每个询问的答案就是把大于k的边并起来之后的$siz[v]-1$

#include<set>
#include<cstdio>
#include<algorithm> #define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a=read()
#define out(a) write(a)
#define outn(a) out(a),putchar('\n') #define I_int int
inline I_int read() {
I_int x = , f = ; char c = getchar() ;
while( c < '' || c > '' ) { if( c == '-' ) f = - ; c = getchar() ; }
while( c >= '' && c <= '' ) { x = x * + c - '' ; c = getchar() ; }
return x * f ;
}
char F[ ] ;
inline void write( I_int x ) {
I_int tmp = x > ? x : -x ;
if( x < ) putchar( '-' ) ;
int cnt = ;
while( tmp > ) {
F[ cnt ++ ] = tmp % + '' ;
tmp /= ;
}
while( cnt > ) putchar( F[ -- cnt ] ) ;
}
#undef I_int }
using namespace io ; using namespace std ; #define N 100010 struct edge {
int u , v , w ;
bool operator < ( const edge &x ) const {
return w > x.w ;
}
} a[ N ] ; struct node {
int k , v , id ;
bool operator < ( const node &x ) const {
return k > x.k ;
}
} b[ N ] ; int f[ N ] , siz[ N ] ;
int n , m , ans[ N ] ; int find( int x ) {
if( f[ x ] == x ) return x ;
else return f[ x ] = find( f[ x ] ) ;
} int main() {
n = read() ; m = read() ;
for( int i = ; i < n ; i ++ ) {
a[ i ] = (edge) { read() , read() , read() } ;
}
for( int i = ; i <= m ; i ++ ) {
b[ i ] = (node) { read() , read() , i } ;
}
sort( a + , a + n + ) ; sort( b + , b + m + ) ;
int j = ;
for( int i = ; i <= n ; i ++ ) siz[ i ] = , f[ i ] = i ;
for( int i = ; i <= m ; i ++ ) {
while( j < n && a[ j ].w >= b[ i ].k ) {
int x = find( a[ j ].u ) , y = find( a[ j ].v ) ;
if( x != y ) {
f[ y ] = x ;
siz[ x ] += siz[ y ] ;
}
j ++ ;
}
ans[ b[ i ].id ] = siz[ find( b[ i ].v ) ] - ;
}
for( int i = ; i <= m ; i ++ ) {
printf( "%d\n" , ans[ i ] ) ;
}
return ;
}

BZOJ5188: [Usaco2018 Jan]MooTube 并查集+离线处理的更多相关文章

  1. 【BZOJ5188】 [Usaco2018 Jan]MooTube

    BZOJ5188 [Usaco2018 Jan]MooTube 突然发现BZOJ没有题目,放题面. 题意翻译 题面描述 在业余时间,Farmer John创建了一个新的视频共享服务,他将其命名为Moo ...

  2. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. poj 2528 Mayor's posters 线段树 || 并查集 离线处理

    题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...

  4. ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)

    Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...

  5. Bzoj5188/洛谷P4185 [Usaco2018 Jan]MooTube(并查集)

    题面 Bzoj 洛谷 题解 最暴力的方法是直接判两个点之间的路径最小值是否\(\geq k\),用\(Dijkstra\)可以做到该算法最快效率,但是空间复杂度始终是\(O(n^2)\)的,会\(ML ...

  6. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  7. BZOJ-1015 StarWar星球大战 并查集+离线处理

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Submit: 4105 Solved: 1826 [Submit ...

  8. 【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  9. luogu4185 [USACO18JAN]MooTube (并查集)

    类似于NOI2018d1t1的离线做法,把询问存下来,排个序,然后倒着给并查集加边,每次询问并查集联通块大小 #include<bits/stdc++.h> #define ll long ...

随机推荐

  1. IO流(5)判断功能

    判断功能: * public boolean isDirectory():判断是否是目录 * public boolean isFile():判断是否是文件 * public boolean exis ...

  2. mysql5.6编译遇到错误

    -- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)CMake Error at cmake/readline ...

  3. redhat7:用户、组和权限

    用户: 任何用户被分配一个独特的用户id号(UID)  (UID 0标识root用户    用户账号通常从UID 1000开始(在redhat6及之前的版本,从500开始))  . 用户名和UID信息 ...

  4. PHPExcel使用-使用PHPExcel导出文件

    导出步骤: 1. 新建一个excel表格 ------------> 实例化PHPExcel类 2. 创建sheet(内置表)-------------> ( 1>. createS ...

  5. Git warning:LF will be replaced by CRLF in readme.txt的原因与解决方案

    今天用Git bash遇到的问题,看了几个回答之后发现一个比较有价值的,给大家分享一下,其他很多的回答都有很或多或少存在一些弊端. 原回答地址在stackoverflow上,附上链接--http:// ...

  6. 前m大的数(哈希入门)&&sort

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 普通方法(625ms) #include <stdio.h> #include <str ...

  7. webbench,简单、实用!

    官网:http://home.tiscali.cz/~cz210552/webbench.html   1.下载并安装 # wget http://home.tiscali.cz/~cz210552/ ...

  8. linux locate

    locate命令查找文件比find速度快很多,locate是在linux下实现快速查找文件的工具.相应的windows下有everything功能也很强大. [root@wuzhigang lib]# ...

  9. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries

    地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...

  10. Python 成对处理数据 zip()

    当你想成对处理数据的时候zip() 函数是很有用的.比如,假设你头列表和一个值列表,就像下面这样: headers = ['name', 'shares', 'price'] values = ['A ...