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. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  2. API系列一:REST和RESTful认识

    序言 最近工作的项目一直使用API,就想趁這个机会,把API的知识点进行一次梳理和总结,顺便提升一下自己对API全新的认识 Web API 是ASP.NET平台新加的一个特性,它可以简单快速地创建We ...

  3. AJPFX讲解java单例模式

    单例设计模式概述:      单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己自己创建自己的唯一实例. 3 ...

  4. iOS Programming View and View Hierarchy 视图和视图等级

    iOS Programming  View and View Hierarchy 视图和视图等级 1.1(1)File → New → Project.. From the iOS section, ...

  5. 什么是LambdaExpression,如何转换成Func或Action(2)

    序言 在上一篇中,我们认识了什么是表达式树.什么是委托,以及它们的关系.可能是我功力不好,貌似大家都不怎么关注,没有讲解出不同角度的问题. 学习一种新技术,是枯燥的过程,只有在你掌握后并能运用时才能从 ...

  6. python学习日记-01

    一. 熟悉 在正式介绍python之前,了解下面两个基本操作对后面的学习是有好处的: (1)基本的输入输出 可以在Python中使用+.-.*./直接进行四则运算. >>> 1+3* ...

  7. col - 过滤掉输入中的反向换行符

    SYNOPSIS(总览) col [-bfx ] [ Fl l Ar num ] DESCRIPTION(描述) Col 过滤掉反向(以及半反向)换行符(LF: line feed or NL: ne ...

  8. /etc/auto.master - automounter的主映射文件

    描述(DESCRIPTION) 当机器启动自动挂载器时, autofs(8) 脚本就会查寻 auto.master 这个主映射文件.文件中的每行分别指明,一个挂载点以及与对应的需要被挂载的文件系统.通 ...

  9. mysql.connector.errors.IntegrityError: 1048 (23000): Column 'request_url' cannot be null

    箭头指向的2的方向 少了一个request_url的值 调试: 直接 执行sql语句 是没问题的, 这样就知道问题是出在代码的逻辑 处理了: INSERT INTO response_time (nu ...

  10. CAD使用DeleteXData删除数据(com接口)

    主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 c#代码实现如下 ...