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
4 2
1 2 3 4
-1 1 2 3
3 2 1
5 1 1

Sample Output

6

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

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

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

 #include <bits/stdc++.h>
using namespace std;
const int N = ,M = ,INF = 0x3f3f3f3f;
int n,m;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int cur[M],h[M],gap[M],head[M],S,T,tot;
void addedge(int u,int v,int flow){
if(!u || !v) return;
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int sap(int u,int low){
if(u == T) return low;
int ret = ;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && h[u] == h[e[i].to] + ){
int a = sap(e[i].to,min(low,e[i].flow));
e[i].flow -= a;
e[i^].flow += a;
low -= a;
ret += a;
if(!low) return ret;
}
}
if(!(--gap[h[u]])) h[S] = T;
gap[++h[u]]++;
cur[u] = head[u];
return ret;
}
namespace REDUCE {
int cnt,tot,leaf[N],dep[N],root[N],l[M],r[M],head[N];
int hd[N],num;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[N];
struct QU {
int to,L,R,next;
QU(int to = ,int L = ,int R = ,int nxt = -) {
this->to = to;
this->L = L;
this->R = R;
this->next = nxt;
}
} Q[N];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void addask(int u,int v,int L,int R) {
Q[num] = QU(v,L,R,hd[u]);
hd[u] = num++;
}
int merge(int x,int y,int L,int R) {
if(!x) return y;
if(!y) return x;
int z = ++cnt;
if(L == R) {
addedge(z,x,INF);
addedge(z,y,INF);
return z;
}
int mid = (L + R)>>;
addedge(z,l[z] = merge(l[x],l[y],L,mid),INF);
addedge(z,r[z] = merge(r[x],r[y],mid + ,R),INF);
return z;
}
int build(int L,int R,int pos) {
int x = ++cnt;
if(L == R) return x;
int mid = (L + R)>>;
if(pos <= mid) addedge(x,l[x] = build(L,mid,pos),INF);
if(pos > mid) addedge(x,r[x] = build(mid+,R,pos),INF);
return x;
}
void ask(int root,int L,int R,int lt,int rt,int node) {
if(!root) return;
if(lt <= L && rt >= R) {
addedge(node,root,INF);
return;
}
int mid = (L + R)>>;
if(lt <= mid && l[root]) ask(l[root],L,mid,lt,rt,node);
if(rt > mid && r[root]) ask(r[root],mid + ,R,lt,rt,node);
}
void dfs(int u,int depth) {
dep[u] = depth;
root[u] = build(,n,dep[u]);
leaf[u] = cnt;
for(int i = head[u]; ~i; i = e[i].next)
dfs(e[i].to,depth + );
}
void dfs(int u) {
for(int i = head[u]; ~i; i = e[i].next) {
dfs(e[i].to);
root[u] = merge(root[u],root[e[i].to],,n);
}
for(int i = hd[u]; ~i; i = Q[i].next)
ask(root[u],,n,Q[i].L,min(n,Q[i].R),Q[i].to);
}
void init() {
memset(head,-,sizeof head);
num = tot = cnt = ;
memset(hd,-,sizeof hd);
memset(l,,sizeof l);
memset(r,,sizeof r);
}
}
int have[N],need[N],hs[N];
int main() {
int kase,u,v,w,rt;
scanf("%d",&kase);
memset(head,-,sizeof head);
while(kase--) {
scanf("%d%d",&n,&m); tot = ;
REDUCE::init();
for(int i = ; i <= n; ++i)
scanf("%d",have + i);
for(int i = ; i <= n; ++i) {
scanf("%d",&u);
if(~u) REDUCE::add(u,i);
else rt = i;
}
REDUCE::dfs(rt,);
for(int i = ; i <= m; ++i) {
scanf("%d%d%d",need + i,&u,&v);
REDUCE::addask(u,hs[i] = ++REDUCE::cnt,REDUCE::dep[u],v + REDUCE::dep[u]);
}
REDUCE::dfs(rt);
S = ++REDUCE::cnt;
T = ++REDUCE::cnt;
for(int i = ; i <= m; ++i)
addedge(S,hs[i],need[i]);
for(int i = ; i <= n; ++i)
addedge(REDUCE::leaf[i],T,have[i]);
int maxflow = ;
gap[] = T;
while(h[S] < T) maxflow += sap(S,INF);
printf("%d\n",maxflow);
for(int i = ; i <= T; i++) {
h[i] = gap[i]=;
head[i] = -;
}
}
return ;
}

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. RHEL5.6环境下Oracle10g单主机安装步骤记录

    操作环境:RedHat Enterprise Linux 5.6 安装版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64 ...

  2. android将对象序列化到文件:直接写文件与用Serializable接口的对比

    1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...

  3. Android 线程池系列教程(1)目录

    Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...

  4. Python学习 Day 4 函数 切片 迭代 列表生成式 生成器

    定义函数 def my_abs(x):#求绝对值的my_abs函数 if x >= 0: return x else: return –x def nop():#空函数 pass#占位符 参数检 ...

  5. CFAN:Coarse-to-Fine Auto-Encoder Networks (CFAN) for Real-Time Face Alignment

    作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6801045.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...

  6. 初学者对C++的切身感受

    上周和一同学聊起了当前一些比较流行且运用广范的编程语言,苹果的IOS比起其它语言 来说更加言简意赅,简单明了,并且他现在也打算一直弄IOS.我之前一直是用C语言和 GNU ARM汇编语言,因为这两种语 ...

  7. 迅为IMX6开发板支持4G全网通模块GPS模块

    IMX6开发板特点 处理器:IMX6开发板支持4G全网通模块GPS模块. 核心板配置:2GB DDR3内存  16GB EMMC 存储,扩展引脚多达320个:运行温度-20 ℃到+80 ℃之间. 核心 ...

  8. Python list列表的常用操作方法

    本文主要介绍了Python中列表(List)的详解操作方法,包含创建.访问.删除.排序.切片,乘等操作方法 1.创建列表:把逗号分隔的不同的数据项使用方括号括起来 list = [1,2,3,'Jam ...

  9. C#根據當前DataGridView查詢數據導出Excel

    private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.C ...

  10. April Fools Day Contest 2019: editorial回顾补题

    A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...