PTA effort successor (BFS)

An ancient martial arts and heritage can flourish, it is to look at fate. In general, the martial arts master to teach apprentice always want to make a discount, so the more backward pass, the weaker the disciples of kung fu ...... until a certain generation of a sudden emergence of a talented disciple of particularly high (or eat a panacea, dug special secrets), all of a sudden the power of kung fu will enlarge N times - we call this disciple “得道者”.
Here we examine disciples ancestral family tree a bit under the door: Assuming that each person in the family tree only one master (except ancestral no master); each teacher can take a lot of disciples; and assume that generational strict and orderly, that is the ancestral home the door of the powers each i-th generation descendant only thanks to a master in the first of the i-1 generation descendant. We assume ancestral skill is known Z, each generation heritage down, will be weakened r%unless certain generation disciple of enlightenment. Now gives the door division pedigree relationship, asking you to calculate the total value of all skill enlightenment persons.

Input formats:

3 gives a positive integer input on the first line, are: N (≤10 ^ 5) - The total number of the entire division of the door (so each from 0 to N-1 number, the number ancestral 0); Z-- ancestral skill values ​​(not necessarily an integer, but at least is a positive number); r - every effort to generation by playing the discount percentage value (positive number not exceeding 100). Then there are N rows, row i (i = 0, ⋯, N-1) number i describe the person's apprentice transmission format: K i I D [ 1 ] I D [ 2 ] I D [ K i ] {K_​i ​​}\quad ID[1] \quad ID[2] ⋯ \quad ID[K_i​ ]
Which K i {K_​i ​​} is the number of apprentice, is followed by an apprentice you number, a space between the digital gap. K i {K_​i ​​} is zero which is represented by an attained, then later with a digital representation which is enlarged multiples martial arts.

Output formats:

Gross output of all skill Enlightened in a row, leaving only its integer part. Topic ensure that the input and output of no more than correct 1 0 10 10^{10}

Sample input:

10 18.0 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3

Sample output:

404


Ideas:

  • The more that storage structure is very important, beginning with fa storage array, but this can only be the same bottom-up, but logically, it should be top-down. Then switch to vector child [N] is stored.
  • However, this problem has test points for the n = 1, the disciples themselves did not, but he is Enlightened, have doubled ...
  • Also pay attention to the complexity of the algorithm, the algorithm of the card n ^ 2

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100005
vector<int> child[N];
queue<int> qu;
double value[N],r,sum=0;
int n,flag[N];
void bfs(){
	while(qu.size()){
		int e = qu.front();
		qu.pop();
		for(int i=0;i<child[e].size();i++){
			int j = child[e][i];
			if(flag[j]!=1){
				sum += value[e]*r*flag[j];
			}
			else {
				value[j] = value[e]*r;
				qu.push(j);
			}
		}
	}
}
int main(){
	int size,x,k;
	double z;
	scanf("%d%lf%lf",&n,&z,&r);
	value[0] = z; r = (100-r)/100.0;
	for(int i=0;i<n;i++){
		scanf("%d",&size); 
		for(int j=0;j<size;j++){
			scanf("%d",&x); 
			child[i].push_back(x);
			flag[x] = 1;
		} 
		if(!size) {
			scanf("%d",&k); 
			flag[i] = k;
		}	
	}
	if(n==1){
		printf("%d\n",(int)(z*flag[0]));
		return 0; 
	}
	qu.push(0);
	bfs();
	printf("%d\n",(int)sum);
	return 0;
}

Published 63 original articles · won praise 34 · views 6534

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/104092347