At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader.

The ith kid will vote for his best friend fi (where 1 ≤ fi ≤ n, and it's too shame to vote for yourself, so fi ≠
i
). And the kid who gets the most votes will be the leader. If more than one kids who get the largest number of votes, there will be multiple leaders in the new semester.

Little Sheldon (the kid with index 1) is extremely vain, and he would like to be the ONLY leader. (That means the number of votes he gets should strictly larger than
any other.) Soon Sheldon found that if he give cicandies to the ith kid, the ith kid would regard Sheldon as the new best friend, and of course vote for Sheldon.

Every kid including Sheldon loves candies. As an evil programmer, please help the evil Sheldon become the ONLY leader with minimum cost of candies. By the way, Sheldon
should vote for any one he wantsEXCEPT himself.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 100) indicating the number of test cases. Then T test cases follow.

The first line of each case contains one integer: n (3 ≤ n ≤ 100) -- the number of kids in class.

The second line contains n-1 integers: fi (1 ≤ fi ≤ nfi ≠ i, and 2 ≤ i ≤ n) -- represents that the best
friend of ith kid is indexed with fi.

The third line contains n-1 integers: ci (1 ≤ ci ≤ 1000, and 2 ≤ i ≤ n) -- represents that if Sheldon gave ci candies
to the ith kid, the ith kid would vote Sheldon, instead of their old best friend fi, as the new semester leader.

Output

For each test case, print the minimal cost of candies to help Sheldon become the ONLY leader.

Sample Input

2
4
1 1 2
1 10 100
3
3 2
1 10

Sample Output

0
11

Hint

In the first case,

  • If Sheldon vote for 2nd kid, the 2nd kid and Sheldon will both have 2 votes. In this case, Sheldon have to pay 100 candies to the 4th kid, and get 3 votes to win;
  • If Sheldon vote for 3rd or 4th kid, Sheldon will win with 2 votes without sacrifice any candy.
枚举孩子赢的选举得到的票数p,所有大于等于p的孩子都要减到p-1如果发现,减去之后的票数给那个主角发现他比p大了,那么这个情况就是不实际的。否则,就在剩下的票里面从小到大选择直到票数等于p。这里的有一个条件,主角自己有票,其实是不用考虑的,这里不证明了

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h> using namespace std;
#define MAX 10000000
struct Node
{
int pos;
int value;
}c[105],v[105][105];
int n;
int cmp(Node a,Node b)
{
return a.value<b.value;
}
int f[105];
int num[105];
int tag[105];
int ans;
int s[105];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i=2;i<=n;i++)
{
scanf("%d",&f[i]);
v[f[i]][++num[f[i]]].pos=i;
}
for(int i=2;i<=n;i++)
{scanf("%d",&c[i].value);c[i].pos=i;}
for(int i=2;i<=n;i++)
for(int j=1;j<=num[i];j++)
v[i][j].value=c[v[i][j].pos].value;
for(int i=2;i<=n;i++)
sort(v[i]+1,v[i]+1+num[i],cmp);
sort(c+1,c+1+n,cmp);
ans=MAX;
for(int p=num[1];p<=n;p++)
{
int l=p-num[1];
int sum1=0;
int sum2=0;
memset(s,0,sizeof(s));
for(int i=2;i<=n;i++)
{
if(num[i]>=p)
{
int m=num[i]-p+1;
for(int j=1;j<=m;j++) {sum2+=v[i][j].value;s[v[i][j].pos]=1;}
sum1+=m;
}
}
if(sum1>l)
continue;
int m2=l-sum1;int cot=0;
for(int i=2;i<=n;i++)
{
if(cot>=m2)
break;
if(!s[c[i].pos]&&f[c[i].pos]!=1){ sum2+=c[i].value;cot++;}
}
ans=min(ans,sum2);
}
printf("%d\n",ans);
}
return 0;
}

ZOJ 3715 Kindergarten Election的更多相关文章

  1. Kindergarten Election

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3715 题意:有N个孩子投票选举leader,不能自己选自己.Sheldon ...

  2. ZOJ - 3715贪心

    ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会 ...

  3. 2012-2014 三年浙江 acm 省赛 题目 分类

    The 9th Zhejiang Provincial Collegiate Programming Contest A    Taxi Fare    25.57% (166/649)     (水 ...

  4. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  5. [译]ZOOKEEPER RECIPES-Leader Election

    选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...

  6. ZOJ People Counting

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

  7. ZOJ 3686 A Simple Tree Problem

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

  8. ZOJ Problem Set - 1394 Polar Explorer

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

  9. ZOJ Problem Set - 1392 The Hardest Problem Ever

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

随机推荐

  1. Atititjs javascript异常处理机制与java异常的转换.js exception process

    Atititjs javascript异常处理机制与java异常的转换.js exception process 1. javascript异常处理机制 Throw str Not throw err ...

  2. [svc]linux buffer和cache的区别

    通俗理解buffer,cache Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而 ...

  3. [svc]linux查看主板型号及内存硬件信息

    公司服务器内存不够用了. 想看看买啥型号的. 购买内存条注意点: ddr3 or4 频率 块钱. 内存槽及内存条: dmidecode |grep -A16 "Memory Device$& ...

  4. 移动touch事件之一

    触摸事件分类: touchstart:当手指触摸屏幕时触发:即使已经有一个手指放在了屏幕上也会触发. touchmove:当手指在屏幕上滑动时连续的触发.在这个事件发生期间,调用preventDefa ...

  5. MacBook Air 2014 安装win7

    1.准备一个4G以上容量USB3.0 U盘.制作一个带USB3.0驱动的win7 2.将制作好的win7iso镜像文件复制到macbook上,插上U盘,运行Boot Camp助理: 3.选择默认勾选项 ...

  6. 应用DataAdapter对象填充DataSet数据集

    private void Form1_Load(object sender, EventArgs e) { string strCon = "Server=localhost;User Id ...

  7. 关于Linux网卡调优之:RPS (Receive Packet Steering)

    昨天在查LVS调度均衡性问题时,最终确定是 persistence_timeout 参数会使用IP哈希.目的是为了保证长连接,即一定时间内访问到的是同一台机器.而我们内部系统,由于出口IP相对单一,所 ...

  8. 基于Gitolite的Git服务架设

    如果不是要与他人协同开发,git根本不需要架设服务器,git可以直接使用本地版本库的路径完成git版本间的操作.但是如果需要和他人分享版本库,协作开发,就需要能够通过网络协议操作git库.git支持的 ...

  9. 情商 EQ & 儿童情商

    EQ 包括哪些内容 1. 认知自身情绪的能力(正确客观的评价自己)2. 管理自己情绪的能力(控制冲动) 3. 自我激励能力(学会抗挫折) 4. 认识他人情绪的能力(学会移情) 5. 人际关系处理能力 ...

  10. 服务器的svnserver修改密码

    VisualSVN Server是一个集成的svn服务端工具,是一款svn服务端不可多得的好工具.可以先安装好VisualSVN Server后,运行VisualSVN Server Manger,然 ...