题干:

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 ci candies 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 wants EXCEPT 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.

题目大意:

在学期开始的幼儿园,n个小孩(1到n)在课堂上需要选择他们的新领导。
第一个孩子会投票给他最好的朋友fi(其中1≤fi≤n,这太可惜了,自己投票,所以fi≠i)。

获得最多票数的孩子将是领袖。 如果有一个以上的孩子获得最多的票数,在新学期将有多个领导人。

小谢尔顿(下标为1)想成为唯一的领导者。 (这意味着他得到的票数应该严格大于任何其他。)

如果他给ci糖果给第i个孩子,第i个孩子会认为谢尔顿作为新的最好的朋友,当然投票谢尔顿。

请帮助谢尔顿成为唯一的领导者,最低的糖果成本。 顺便说一下,谢尔顿应该投票支持自己想要的任何一个人。

解题报告:

如果1号选手已经指定投票给哪一位选手,那很显然和codeforce上的那个C题Elections一模一样,n^2就可以解决。但是这题既然数据范围是100,说明肯定有坑,我本来的想法是先不让1号选手投票,最后再让他直接投给最小的那个人,但是发现这样好像不太行,所以干脆就直接枚举他投票给哪一个人就可以了。这样复杂度O(n^3),,反正也说得过去。就这么xjb写了,,刚开始wa了无数发还以为思路有问题,,结果发现写了cmp函数但是忘了sort了、、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
int bk[105];
struct Node {int f,c;
} p[MAX];
bool cmp(Node a,Node b) {return a.c < b.c;
}
int cnt[105];
int main() {int t;cin>>t;while(t--) {scanf("%d",&n);memset(cnt,0,sizeof cnt);for(int i = 2; i<=n; i++) scanf("%d",&p[i].f);for(int i = 2; i<=n; i++) scanf("%d",&p[i].c);int ans = 0x3f3f3f3f;for(int i = 2; i<=n; i++) cnt[p[i].f]++;sort(p+2,p+n+1,cmp);for(int qq = 2; qq<=n; qq++) {p[1].f = qq;for(int piao = 1; piao <= n; piao++) {memset(cnt,0,sizeof cnt);memset(bk,0,sizeof bk);for(int i = 1; i<=n; i++) cnt[p[i].f]++;int chu = cnt[1];int cost = 0;for(int i = 2; i<=n; i++) {if(p[i].f != 1 && cnt[p[i].f ] >= piao) {cost += p[i].c;chu++;cnt[p[i].f]--;bk[i] = 1;}}for(int i = 2; i<=n; i++) {if(p[i].f !=1 && chu < piao && bk[i] == 0) {chu++;cost += p[i].c;}}ans = min(ans,cost);}}printf("%d\n",ans);}return 0 ;
}

【ZOJ - 3715】Kindergarten Election(枚举得票数,贪心)相关推荐

  1. 2021算法竞赛入门班第一节课【枚举、贪心】习题

    目录 [USACO 2007 Jan S]保护花朵[经典贪心] [NOIP2005]校门外的树[区间合并] [NOIP2006]明明的随机数[签到] [HNOI2003]激光炸弹[二维前缀和] 铺地毯 ...

  2. 【ZOJ - 2968 】Difference Game (贪心,思维模拟)

    题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...

  3. 【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

    题干: Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dim ...

  4. zoj3715 Kindergarten Election

    题意: 有n(3-100)个人 ,给定n-1个人的投票以及动摇他们投票给自己的代价,问,如何使用最少的代价赢得选举 解题思路:本来开始是想如何一步步贪心得到最优解,后来才知道这属于多变量问题,解法就是 ...

  5. 牛客竞赛语法入门班数组模拟、枚举、贪心习题【未完结】

    题目地址: https://ac.nowcoder.com/acm/contest/19851?from=acdiscuss 目录 四舍五入 安卓图案解锁 Captcha Cracker 回文数 [N ...

  6. 【ZOJ - 3778】Talented Chef(贪心)

    题干: As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same ti ...

  7. 2018.09.19 atcoder AtCoDeer and Election Report(贪心)

    传送门 很有意思的一道贪心. 就是每次翻最小的倍数来满足条件. 代码: #include<bits/stdc++.h> #define ll long long using namespa ...

  8. 牛客-模拟、枚举与贪心-2022.10.18

    昨天和今天写的题都比较简单,本来都不想挂,但是还是觉得浅浅记录一下 Captcha Cracker 简单模拟输出即可 #include <bits/stdc++.h> using name ...

  9. 【CodeForces - 1020C】Elections (枚举投票数,贪心)

    题干: As you know, majority of students and teachers of Summer Informatics School live in Berland for ...

最新文章

  1. 64位Linux下使用QQ
  2. Linux系统查看位数和内存
  3. 多线程安全问题产生解决方案
  4. 表单元素值获取方式js及java方式
  5. maven项目在eclipse启动报错:java.lang.ClassNotFoundException
  6. pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程
  7. qt电容触摸屏实现长按_电阻屏和电容屏有什么区别?究竟谁更胜一筹?
  8. 解密阿里线上问题诊断工具Arthas和jvm-sandbox
  9. 错误: 找不到或无法加载主类 Main
  10. 策略模式和责任链模式
  11. 取消u盘写保护 u盘量产教程(无数尝试后的终极解决办法)
  12. 微信文件夹储存在什么位置?如何修改保存路径
  13. 蓝桥杯 种花小游戏 java 状压
  14. Not_flushed_delayed_rows
  15. matlab符号加粗_matlab坐标轴字体加粗
  16. [爬虫笔记01] Ajax爬取今日头条文章
  17. opencv python图片合成视频
  18. SELECT * FROM DUAL中的DUAL是什么?
  19. msys2 结合 vscode 使用 lldb 进行调试及 lldb-mi.exe 问题
  20. 陈绮贞 旅行的意义 Ukulele 尤克里里谱

热门文章

  1. 顺丰快递,果然不一般!
  2. 689 Maximum Sum of 3 Non-Overlapping Subarrays
  3. 【发现问题】IDEA设置全局新创建文件默认换行符
  4. r 语言roc_R语言画ROC曲线总结
  5. 二叉树垂直遍历 java_【004】二叉树垂直遍历
  6. ALLyeSNO 优化版浩方 第二版 Ver 2007 06 15 清除广告 自动挤房间
  7. pythonvim编辑教程_Pycharm学习教程(6) Pycharm作为Vim编辑器使用
  8. python可以在linux运行_服务器(Linux)上运行python总结
  9. 怎么用计算机打分数,电脑excel中分数怎么打出来(图解excel分数输入法)
  10. sqlite sqlite3_bind_int sqlite3_bind_text