题目背景

Samcompu拥有大量的"水"资源!!

题目描述

Samcompu需要制定一个水计划。这个计划的主要目的就是为了避开老师监视的时间来水。

老师在中途会离开机房T次,第i次将会离开tim_i秒。Samcompu划水的时候可不是随便乱水的。他可是拥有"水"资源的。在他的库存中有N个可以水的网站。Samcompu拥有一种黑科技,他可以几乎不耗任何时间在网站与网站之间跳转并且把跳转的网页的信息秒存。也就是说,Samcompu并不需要在每一次跳转的时候花费时间去浏览网页。当然,这只局限于N个网站之间的N-1个跳转方式(保证每一个网站都可以跳转到另外的所有网站)。对于第i种跳转方式,第u_i个网站到第v_i个网站的跳转存在一个危险程度w_i,这个危险值可能会造成电脑卡死,如果Samcompu不能及时处理,那么就会完美地被老师发现。

值得一提的是,在被查水表很多次后,Samcompu总结出了一个规律:

老师走得越久,能够保证在被老师发现之前处理好电脑卡死的危险程度的上限就越高。简单来说,两者就是成正比的关系,比例系数为1。

可惜的是,Samcompu的黑科技并不稳定,在老师第i次离开的时候,第K_i个跳转方式就不可用了。

当然,每一次水都可以从任意一个网站开始,也可以从任意一个网站结束。

现在Samcompu想知道,对于第i次老师离开机房时,他能够有多少种不同的安全的水的方案。两种水的方案不同当且仅当这两种水的方案的第一个网站或者最后一个网站不同。

(补充说明: 一个安全的水的方案当且仅当当前是老师第j次离开教室时跳转的路径中不存在一个跳转方式i使得timj​⩽wi​,每一次水完后不可用的跳转方式就会恢复。)

输入输出格式

输入格式:

第一行两个正整数T, N

接下来N−1行,每一行三个正整数u_i, v_i​, w_i

接下来T行,每一行两个正整数m_i, K_i

输出格式:

T行,每行一个正整数表示有多少中安全的水的方案。

输入输出样例

输入样例#1:

  1. 3 5
  2. 1 2 1
  3. 1 3 2
  4. 3 4 1
  5. 3 5 3
  6. 1 1
  7. 2 2
  8. 3 3
输出样例#1:

  1. 0
  2. 4
  3. 6

说明

第一次连接1和2的边不可用,当前能经过的边的危险程度需要<1,并没有合法的方案。

第二次连接1和3的边不可用,当前能经过的边的危险程度需要<2,合法的方案有 (1,2) (2,1) (3,4) (4,3) 共四种。

第三次连接3和4的边不可用,当前能经过的边的危险程度需要<3,合法的方案有 (1,2) (1,3) (2,1) (2,3) (3,1) (3,2) 共六种。

提醒:本题计算答案按照点对的方式计算.也就是说,如果起点和终点一样,则只看做同一种方案.特别的,(x,y)(x,y)和(y,x)(y,x) (x≠y)算作两种不同的方案.

数据范围:

Subtask 1(30 pts):

1⩽Ki​⩽N⩽10^3 1⩽timi​,wi​⩽103

Subtask 2(50 pts):

1⩽T⩽5∗10^3 1⩽Ki​⩽N⩽10^4 1⩽timi​,wi​⩽10^3

Subtask 3(20 pts):

1⩽T⩽10^4 1⩽Ki​⩽N⩽10^4 1⩽timi​,wi​⩽10^3

数据保证不同的K_iKi​最多只有10^3个。

温馨提醒:由于出题人数据比较毒瘤,所以请尽量卡常。

分析:

我只想说出题人真的考虑了我的语文水平了吗。。。

事实上本题就是按并查集的方式加边,然后再在合并的时候进行计算。

至于失效直接保存即可。

CODE:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. const int M=;
  8. int t,n,tot,ans[M],fa[M],now[M],sl[M],last;
  9. int fr(){
  10. char c=getchar();int ans=;
  11. while (c<''||c>'') c=getchar();
  12. while (c>=''&&c<='') ans=(ans<<)+(ans<<)+(c^),c=getchar();
  13. return ans;
  14. }
  15. int found(int x){
  16. if (fa[x]==x) return x;
  17. return fa[x]=found(fa[x]);
  18. }
  19. struct node{
  20. int tim,K,num;
  21. }ask[M];
  22. struct node2{
  23. int from,to,adj,num;
  24. }e[M];
  25. bool cmp(node x,node y){
  26. if (x.K==y.K) return x.tim<y.tim;
  27. return x.K<y.K;
  28. }
  29. bool cmp2(node2 x,node2 y){
  30. return x.adj<y.adj;
  31. }
  32. void add(int u,int v,int w,int id){
  33. e[++tot].from=u;e[tot].to=v;
  34. e[tot].adj=w;e[tot].num=id;
  35. return;
  36. }
  37. int count(int x){
  38. return x*(x-);
  39. }
  40. int main(){
  41. t=fr(),n=fr();
  42. for (int i=;i<n;i++){
  43. int u=fr(),v=fr(),w=fr();
  44. add(u,v,w,i);
  45. }
  46. for (int i=;i<=t;i++) ask[i].tim=fr(),ask[i].K=fr(),ask[i].num=i;
  47. sort(ask+,ask++t,cmp);
  48. sort(e+,e+n,cmp2);
  49. for (int i=;i<n;i++) now[e[i].num]=i;
  50. for (int i=;i<=t;i++){
  51. if (i==||ask[i].K!=ask[i-].K){
  52. for (register int j=;j<=n;j++) fa[j]=j,sl[j]=;
  53. last=;ans[ask[i].num]=;
  54. }
  55. else ans[ask[i].num]=ans[ask[i-].num];
  56. while (last<n&&e[last].adj<ask[i].tim){
  57. if (last!=now[ask[i].K]&&found(e[last].from)!=found(e[last].to)){
  58. int now1=found(e[last].from),now2=found(e[last].to);
  59. ans[ask[i].num]+=count(sl[now1]+sl[now2])-count(sl[now1])-count(sl[now2]);
  60. sl[now2]+=sl[now1];sl[now1]=;
  61. fa[now1]=now2;
  62. }
  63. last++;
  64. }
  65. }
  66. for (int i=;i<=t;i++) printf("%d\n",ans[i]);
  67. return ;
  68. }

Samcompu Loves Water的更多相关文章

  1. 【洛谷】CYJian的水题大赛【第二弹】解题报告

    点此进入比赛 T1: JerryC Loves Driving 第一题应该就是一道水分题(然而我只水了130分),我的主要做法就是暴力模拟,再做一些小小的优化(蠢得我自己都不想说了). My Code ...

  2. CYJian的水题大赛2 解题报告

    这场比赛是前几天洛谷上 暮雪﹃紛紛dalao的个人公开赛,当时基本上都在水暴力分......也没有好好写正解(可能除了T1) 过了几天颓废的日子之后,本蒟蒻觉得应该卓越一下了qwq,所以就打算写一个解 ...

  3. Microsoft Loves Linux

    微软新任CEO纳德拉提出的“Microsoft Loves Linux”,并且微软宣布.NET框架的开源,近期Microsoft不但宣布了Linux平台的SQL Server,还宣布了Microsof ...

  4. [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  5. [LeetCode] Trapping Rain Water II 收集雨水之二

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  7. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  9. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

随机推荐

  1. Leetcode_415字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: ①num1 和num2 的长度都小于 5100.②num1 和num2 都只包含数字 0-9.③num1 和num2 都不 ...

  2. mybatis之返回值总结

    mybatis框架让我们能在编程中只需要编写一个接口,然后再编写mapper映射文件,无需编写接口的实现类就可以实现从数据库检索数据.这是mybatis通过动态代理,把mapper映射文件的内容转化为 ...

  3. LeetCode Array Easy121. Best Time to Buy and Sell Stock

    Description Say you have an array for which the ith element is the price of a given stock on day i. ...

  4. Ubuntu16.04+cuda9.0安装教程

    1.安装NVIDIA驱动 首先去官网(http://www.nvidia.cn/Download/index.aspx?lang=cn)查找适配自己电脑GPU的驱动,我的电脑驱动版本如下: 执行如下语 ...

  5. idae父子项目Test执行报Result Maps collection already contains value for xxx

    现象:同一个springmvc工程使用eclipse和idea用Tomcat启动都没问题,但是如果走单元测试使用到了@ContextConfiguration这个spring的上下文注解idea出问题 ...

  6. shell同时输出多行信息

  7. secureCRT The remote system refused the connection.解决办法

    使用远程登录工具SecureCRT登陆ubuntu的时候遇到了这个问题: secureCRT The remote system refused the connection 这个问题的原因是是Ubu ...

  8. php操作redis--列表篇

    常用函数:lpush/rpush/lpop/rpop/lrange/lrem等 应用场景:关注列表,粉丝列表,发送缓冲队列等 特点:可理解为数组操作,插入和删除数据按照一定的规律排序,数据可重复 连接 ...

  9. 【leetcode】538. Convert BST to Greater Tree

    题目如下: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the orig ...

  10. 【基础】Pipeline

    1. 参考的优秀文章 Request/Response protocols and RTT 2. 来源 原来,系统中一个树结构的数据来源是Redis,由于数据增多.业务复杂,查询速度并不快.究其原因, ...