ZOJ 3715 Kindergarten Election (enumeration + greedy)

Source: Internet
Author: User

Kindergarten election Time limit: 2 Seconds Memory Limit: 65536 KB

At the beginning of the semester in kindergarten, the n little Kids (indexed from 1 to N, for Convenienc e) In the class need to elect their new leader.

The ith Kid would vote for he 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 would be the leader. If more than one kids who get the largest number of votes, there'll be multiple leaders in the new semester.

Little Sheldon (The Kid with index 1) was extremely vain, and he would like to being 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 the-the-Sheldon should vote for any one he wants EXCEPT himself.

Input

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

The first line of all case contains one integer: n (3 ≤n≤ ) – 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's best friend of ith Kid was indexed with fi.

The third line contains n-1 integers: ci (1 ≤ci≤ , and 2 ≤i≤n)--represents that if Sheldon gave ci candies to the ith Kid, the ith Kid would vote Sheldon, instead of thei R 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
241 1 21 10 10033 21 10
Sample Output
011
Hint

In the first case,

    • If Sheldon vote for 2nd Kid, the 2nd Kid and Sheldon would both have 2 votes. In this case, Sheldon has to pay candies to the 4th kid, and get 3 votes to win;
    • If Sheldon Vote for 3rd or 4th kid, Sheldon would win with 2 votes without sacrifice any candy.


Test instructions: in the kindergarten, each child vote for the election leader (cannot vote for himself), the most votes of the children to become leaders (if there are multiple leaders). Now there's a little friend (number 1) want to be the only leader, so ready to bribe some children, let them vote for themselves, buy the first I personally need to spend c[i] a candy. Ask the child how much sugar he has at least to make him the sole leader.

Problem-Solving ideas: First enumerate a child vote of the person, and then enumerate its want to become the leader of the number of votes X, need to bribe d=x-cnt[i] individuals, if 2 to n children have a certain I vote number is not less than X, then need to bribe people to vote I (at the cost from small to large order bribery), so that its votes less than Every bribe a person d decreases by 1, if d<0, then the enumeration fails, need to increase x, if D is positive then only need to choose the lowest price in the person who does not bribe the bribe can be.

The code is as follows:

#include <iostream> #include <cstring> #include <algorithm> #include <vector>using namespace Std;typedef Long Long ll;const int MAXN = 1e2 + 10;int c[maxn],f[maxn],cnt[maxn],n;bool used[maxn];bool cmp (const int &amp ; A,const int & B) {return c[a]<c[b];} int main (int argc, char const *argv[]) {int T;cin>>t;while (t--) {cin>>n;memset (cnt,0,sizeof cnt);vector< Int>sa,v[maxn];//sa Save 1 person, V[i] Save cast I for (int i=2;i<=n;i++) {cin>>f[i];cnt[f[i]]++;if (f[i]!=1) { Sa.push_back (i); V[f[i]].push_back (i);}} for (int i=2;i<=n;i++) cin>>c[i];for (int i=2;i<=n;i++) sort (V[i].begin (), V[i].end (), CMP);// Sort (Sa.begin (), Sa.end (), CMP) for people who elect I at the cost of bribery;//For those who did not choose 1, sort int ans=1e9;for (int vote=2;vote<=n;vote++) at the cost of bribery {// Enum 1th cast person cnt[vote]++;for (int x=cnt[1];x<n;x++) {//enum 1th last required number of votes memset (used,0,sizeof used); int d=x-cnt[1],w=0;for (int i=2;i<=n;i++) {//Check if successful if (cnt[i]<x) continue;int t=cnt[i];for (int j=0;j<v[i].size (); j + +) {T--;w+=c[v[i][j] ];used[v[i][j]]=1;d--;if (t<x| | d<=0) break;} if (t>=x| | d<0) {w=1e9;d=0;break;}} if (d>0) {for (int i=0;d&&i<sa.size (); i++) if (!used[sa[i]]) {W+=c[sa[i]];d--;}} Ans=min (ans,w);//update cost}cnt[vote]--;} Cout<<ans<<endl;} return 0;}


ZOJ 3715 Kindergarten Election (enumeration + greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.