Market


Time Limit: 2 Seconds      Memory Limit: 65536 KB

There's a fruit market in Byteland. The salesmen there only sell apples.

There are n salesmen in the fruit market and the i-th salesman will sell at most wi apples. Every salesman has an immediate manager pi except one salesman who is the boss of the market. A salesmanA is said to be the superior of another salesman B if at least one of the followings is true:

  • Salesman A is the immediate manager of salesman B.
  • Salesman B has an immediate manager salesman C such that salesman A is the superior of salesman C.

The market will not have a managerial cycle. That is, there will not exist a salesman who is the superior of his/her own immediate manager.

We will call salesman x a subordinate of another salesman y, if either y is an immediate manager of x, or the immediate manager of x is a subordinate to salesman y. In particular, subordinates of the boss are all other salesmen of the market. Let the degree of the boss be 0. Then if the degree of i-th salesman is k, the immediate subordinates of i-th salesman will have degree k + 1.

Today, m buyers come to market for apples. The i-th buyer will buy at most ci apples only from the xi-th salesman and his subordinates whose degree is no larger than xi-th salesman's degree plus di.

The boss wants to know how many apples can be sold in salesmen's best effort (i.e. the maximum number).

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers n and m (1 ≤ nm ≤ 10000) — the number of salesmen and the number of buyers.

The second line contains n integers w1w2, ..., wn (1 ≤ wi ≤ 105). Every wi denotes the number of apples that i-th salesman can sell.

The next line contains n integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th salesman. If pi is -1, that means that the i-th salesman does not have an immediate manager.

Each of the next m lines contains three integers cixi and di (1 ≤ ci ≤ 105, 1 ≤ xi ≤ n, 0 ≤ di ≤ n) — the information of i-th buyer.

It is guaranteed that the total number of salesmen in the input doesn't exceed 105, and the total number of buyers also doesn't exceed 105. The number of test cases in the input doesn't exceed 500.

Output

For each test case, output a single integer denoting the maximum number of apples can be sold.

Sample Input

  1. 1
  2. 4 2
  3. 1 2 3 4
  4. -1 1 2 3
  5. 3 2 1
  6. 5 1 1

Sample Output

  1. 6

Author: LIN, Xi
Source: ZOJ Monthly, October 2015

解题:网络流,关键是,由于点多,导致边更多,直接建图,爆内存。。。

感谢Claris的帮助,在花费了一两天的时间,终于搞定了

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = ,M = ,INF = 0x3f3f3f3f;
  4. int n,m;
  5. struct arc{
  6. int to,flow,next;
  7. arc(int x = ,int y = ,int z = -){
  8. to = x;
  9. flow = y;
  10. next = z;
  11. }
  12. }e[];
  13. int cur[M],h[M],gap[M],head[M],S,T,tot;
  14. void addedge(int u,int v,int flow){
  15. if(!u || !v) return;
  16. e[tot] = arc(v,flow,head[u]);
  17. head[u] = tot++;
  18. e[tot] = arc(u,,head[v]);
  19. head[v] = tot++;
  20. }
  21. int sap(int u,int low){
  22. if(u == T) return low;
  23. int ret = ;
  24. for(int &i = cur[u]; ~i; i = e[i].next){
  25. if(e[i].flow && h[u] == h[e[i].to] + ){
  26. int a = sap(e[i].to,min(low,e[i].flow));
  27. e[i].flow -= a;
  28. e[i^].flow += a;
  29. low -= a;
  30. ret += a;
  31. if(!low) return ret;
  32. }
  33. }
  34. if(!(--gap[h[u]])) h[S] = T;
  35. gap[++h[u]]++;
  36. cur[u] = head[u];
  37. return ret;
  38. }
  39. namespace REDUCE {
  40. int cnt,tot,leaf[N],dep[N],root[N],l[M],r[M],head[N];
  41. int hd[N],num;
  42. struct arc {
  43. int to,next;
  44. arc(int x = ,int y = -) {
  45. to = x;
  46. next = y;
  47. }
  48. } e[N];
  49. struct QU {
  50. int to,L,R,next;
  51. QU(int to = ,int L = ,int R = ,int nxt = -) {
  52. this->to = to;
  53. this->L = L;
  54. this->R = R;
  55. this->next = nxt;
  56. }
  57. } Q[N];
  58. void add(int u,int v) {
  59. e[tot] = arc(v,head[u]);
  60. head[u] = tot++;
  61. }
  62. void addask(int u,int v,int L,int R) {
  63. Q[num] = QU(v,L,R,hd[u]);
  64. hd[u] = num++;
  65. }
  66. int merge(int x,int y,int L,int R) {
  67. if(!x) return y;
  68. if(!y) return x;
  69. int z = ++cnt;
  70. if(L == R) {
  71. addedge(z,x,INF);
  72. addedge(z,y,INF);
  73. return z;
  74. }
  75. int mid = (L + R)>>;
  76. addedge(z,l[z] = merge(l[x],l[y],L,mid),INF);
  77. addedge(z,r[z] = merge(r[x],r[y],mid + ,R),INF);
  78. return z;
  79. }
  80. int build(int L,int R,int pos) {
  81. int x = ++cnt;
  82. if(L == R) return x;
  83. int mid = (L + R)>>;
  84. if(pos <= mid) addedge(x,l[x] = build(L,mid,pos),INF);
  85. if(pos > mid) addedge(x,r[x] = build(mid+,R,pos),INF);
  86. return x;
  87. }
  88. void ask(int root,int L,int R,int lt,int rt,int node) {
  89. if(!root) return;
  90. if(lt <= L && rt >= R) {
  91. addedge(node,root,INF);
  92. return;
  93. }
  94. int mid = (L + R)>>;
  95. if(lt <= mid && l[root]) ask(l[root],L,mid,lt,rt,node);
  96. if(rt > mid && r[root]) ask(r[root],mid + ,R,lt,rt,node);
  97. }
  98. void dfs(int u,int depth) {
  99. dep[u] = depth;
  100. root[u] = build(,n,dep[u]);
  101. leaf[u] = cnt;
  102. for(int i = head[u]; ~i; i = e[i].next)
  103. dfs(e[i].to,depth + );
  104. }
  105. void dfs(int u) {
  106. for(int i = head[u]; ~i; i = e[i].next) {
  107. dfs(e[i].to);
  108. root[u] = merge(root[u],root[e[i].to],,n);
  109. }
  110. for(int i = hd[u]; ~i; i = Q[i].next)
  111. ask(root[u],,n,Q[i].L,min(n,Q[i].R),Q[i].to);
  112. }
  113. void init() {
  114. memset(head,-,sizeof head);
  115. num = tot = cnt = ;
  116. memset(hd,-,sizeof hd);
  117. memset(l,,sizeof l);
  118. memset(r,,sizeof r);
  119. }
  120. }
  121. int have[N],need[N],hs[N];
  122. int main() {
  123. int kase,u,v,w,rt;
  124. scanf("%d",&kase);
  125. memset(head,-,sizeof head);
  126. while(kase--) {
  127. scanf("%d%d",&n,&m);
  128.  
  129. tot = ;
  130. REDUCE::init();
  131. for(int i = ; i <= n; ++i)
  132. scanf("%d",have + i);
  133. for(int i = ; i <= n; ++i) {
  134. scanf("%d",&u);
  135. if(~u) REDUCE::add(u,i);
  136. else rt = i;
  137. }
  138. REDUCE::dfs(rt,);
  139. for(int i = ; i <= m; ++i) {
  140. scanf("%d%d%d",need + i,&u,&v);
  141. REDUCE::addask(u,hs[i] = ++REDUCE::cnt,REDUCE::dep[u],v + REDUCE::dep[u]);
  142. }
  143. REDUCE::dfs(rt);
  144. S = ++REDUCE::cnt;
  145. T = ++REDUCE::cnt;
  146. for(int i = ; i <= m; ++i)
  147. addedge(S,hs[i],need[i]);
  148. for(int i = ; i <= n; ++i)
  149. addedge(REDUCE::leaf[i],T,have[i]);
  150. int maxflow = ;
  151. gap[] = T;
  152. while(h[S] < T) maxflow += sap(S,INF);
  153. printf("%d\n",maxflow);
  154. for(int i = ; i <= T; i++) {
  155. h[i] = gap[i]=;
  156. head[i] = -;
  157. }
  158. }
  159. return ;
  160. }

ZOJ 3910 Market的更多相关文章

  1. ZOJ 3910 Market ZOJ Monthly, October 2015 - H

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  2. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  3. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  4. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  5. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  6. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  7. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  8. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  9. LYDSY模拟赛day2 Market

    /* orz claris,这个题的解法非常巧妙,首先是时间问题,其实这个问题只要离线处理一下就可以了,把物品和询问都按照时间排序,然后看一下能不能满足.然后,因为容量<=10^9,显然是不可能 ...

随机推荐

  1. Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)

    Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...

  2. 字符串处理 BestCoder Round #43 1001 pog loves szh I

    题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...

  3. SpringCloud+MyBatis+Redis整合—— 超详细实例(二)

    2.SpringCloud+MyBatis+Redis redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写 ...

  4. AJPFX关于Java Object类常用方法小总结

    java.lang.Object   java.lang包在使用的时候无需显示导入,编译时由编译器自动导入. Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类. Object类 ...

  5. C#过时方法标记

    1.当遇到过时或废弃的方式 函数怎么办 [Obsolete]特性解决你的困惑 1.1:当方法已经完成相关兼容 可以保留时

  6. [BZOJ1085][SCOI2005]骑士精神 搜索

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...

  7. Angular jsonp 同源策略的问题

    引用:http://www.cnblogs.com/dengzy/p/5388357.html 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决 ...

  8. C#代码规范(简版)

    C#项目代码规范 目的 1.方便代码的交流和维护. 2.不影响编码的效率,不与大众习惯冲突. 3.使代码更美观.阅读更方便. 4.使代码的逻辑更清晰.更易于理解. 在C#中通常使用的两种编码方式如下 ...

  9. C/C++ 运算符重载、数据类型转换

    1.运算符就是“+”.“>>”等符号,对运算符重载实质就是对函数的重载,这样运算符就能在原有基础上增加新功能,不能自己定义新运算符,只能对已有运算符重载,重载运算符后不能改变运算符本身的特 ...

  10. 前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线。

    前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线.