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 ≤ n, m ≤ 10000) — the number of salesmen and the number of buyers.
The second line contains n integers w1, w2, ..., 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 ci, xi 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的帮助,在花费了一两天的时间,终于搞定了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 10010,M = 500000,INF = 0x3f3f3f3f; 4 int n,m; 5 struct arc{ 6 int to,flow,next; 7 arc(int x = 0,int y = 0,int z = -1){ 8 to = x; 9 flow = y; 10 next = z; 11 } 12 }e[3000000]; 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,0,head[v]); 19 head[v] = tot++; 20 } 21 int sap(int u,int low){ 22 if(u == T) return low; 23 int ret = 0; 24 for(int &i = cur[u]; ~i; i = e[i].next){ 25 if(e[i].flow && h[u] == h[e[i].to] + 1){ 26 int a = sap(e[i].to,min(low,e[i].flow)); 27 e[i].flow -= a; 28 e[i^1].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 = 0,int y = -1) { 45 to = x; 46 next = y; 47 } 48 } e[N]; 49 struct QU { 50 int to,L,R,next; 51 QU(int to = 0,int L = 0,int R = 0,int nxt = -1) { 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)>>1; 76 addedge(z,l[z] = merge(l[x],l[y],L,mid),INF); 77 addedge(z,r[z] = merge(r[x],r[y],mid + 1,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)>>1; 84 if(pos <= mid) addedge(x,l[x] = build(L,mid,pos),INF); 85 if(pos > mid) addedge(x,r[x] = build(mid+1,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)>>1; 95 if(lt <= mid && l[root]) ask(l[root],L,mid,lt,rt,node); 96 if(rt > mid && r[root]) ask(r[root],mid + 1,R,lt,rt,node); 97 } 98 void dfs(int u,int depth) { 99 dep[u] = depth; 100 root[u] = build(1,n,dep[u]); 101 leaf[u] = cnt; 102 for(int i = head[u]; ~i; i = e[i].next) 103 dfs(e[i].to,depth + 1); 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],1,n); 109 } 110 for(int i = hd[u]; ~i; i = Q[i].next) 111 ask(root[u],1,n,Q[i].L,min(n,Q[i].R),Q[i].to); 112 } 113 void init() { 114 memset(head,-1,sizeof head); 115 num = tot = cnt = 0; 116 memset(hd,-1,sizeof hd); 117 memset(l,0,sizeof l); 118 memset(r,0,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,-1,sizeof head); 126 while(kase--) { 127 scanf("%d%d",&n,&m); 128 129 tot = 0; 130 REDUCE::init(); 131 for(int i = 1; i <= n; ++i) 132 scanf("%d",have + i); 133 for(int i = 1; i <= n; ++i) { 134 scanf("%d",&u); 135 if(~u) REDUCE::add(u,i); 136 else rt = i; 137 } 138 REDUCE::dfs(rt,1); 139 for(int i = 1; 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 = 1; i <= m; ++i) 147 addedge(S,hs[i],need[i]); 148 for(int i = 1; i <= n; ++i) 149 addedge(REDUCE::leaf[i],T,have[i]); 150 int maxflow = 0; 151 gap[0] = T; 152 while(h[S] < T) maxflow += sap(S,INF); 153 printf("%d ",maxflow); 154 for(int i = 0; i <= T; i++) { 155 h[i] = gap[i]=0; 156 head[i] = -1; 157 } 158 } 159 return 0; 160 }