ZOJ 3715 Kindergarten Election
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 ≤ n, fi ≠ 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.
#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的更多相关文章
- Kindergarten Election
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3715 题意:有N个孩子投票选举leader,不能自己选自己.Sheldon ...
- ZOJ - 3715贪心
ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会 ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- [译]ZOOKEEPER RECIPES-Leader Election
选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- Missing separate debuginfos, use: debuginfo-install
环境:CentOS6.2 64位 操作:使用gdb调试C++查询MySQL数据库的程序 原因: 解决办法: 1. 修改文件/etc/yum.repos.d/CentOS-Debuginfo.repo ...
- FreeRtos——任务删除,改变任务优先级
以下转载自安富莱电子: http://forum.armfly.com/forum.php vTaskDelete() API 函数任务可以使用 API 函数 vTaskDelete()删除自己或其它 ...
- Makefile 9——为依赖关系文件建立依赖关系
现在我们再对complicated项目做一些更改,增加程序文件间依赖关系的复杂度. /× main.c ×/ #include"foo.h" int main(void) { fo ...
- 逃生dp
蒜头君在玩一款逃生的游戏.在一个 n \times mn×m的矩形地图上,蒜头位于其中一个点.地图上每个格子有加血的药剂,和掉血的火焰,药剂的药效不同,火焰的大小也不同,每个格子上有一个数字,如果格子 ...
- dp之多重背包hdu1059
题意:价值为1,2,3,4,5,6. 分别有n[1],n[2],n[3],n[4],n[5],n[6]个.求能否找到满足价值刚好是所有的一半的方案. 思路:简单的多重背包,我建议多重背包都用二进制拆分 ...
- 算法图绘制工具Graphviz
graphviz是贝尔实验室设计的一个开源的画图工具,它的强大主要体现在“所思即所得"(WYTIWYG,what you think is what you get),这是和office的“ ...
- js监听文本框变化事件
用js有两种写法: 法一: <!DOCTYPE HTMl> <html> <head> <title> new document </title& ...
- C# linq查询 动态OrderBy
groupList是原始数据集合,List<T> sortOrder是排序类型,desc 或者asc sortName是排序属性名称 1.使用反射. private static obje ...
- 回文树(回文自动机) - BZOJ 3676 回文串
BZOJ 3676 回文串 Problem's Link: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 Mean: 略 analyse: ...
- Asp.net回调技术Callback学习
.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.a ...