PAT 1106node
BFS用在tree上,这一个题里主要关注的是用vector去保存每个节点所链接的子节点,当BFS 时,一旦发现该节点下面没有子节点,这一层必定是最短的路径,而后用当前的层数去为后面的节点作判断,若是接下来,仍然有节点没有子节点,那么用层数去验 证,若是层数一致则是,不然不是。ios
vectorc++
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<vector> 6 #include<queue> 7 #define LL long long; 8 using namespace std; 9 const int MAXN=100005; 10 double pi,r; 11 struct node//节点 12 { 13 int v;//节点编号 14 int cnt;//记录距离根节点的层数 15 }; 16 vector <int> p[MAXN];//构造这棵树 17 queue <node> q; 18 int minct=-1; 19 int bfs() 20 { 21 int c=0; 22 bool flag = false; 23 24 while(!q.empty()) 25 { 26 node nd = q.front(); 27 q.pop(); 28 int id = nd.v; 29 int ct = nd.cnt; 30 //初始状况下flag记为false,也就是还未有叶子节点 31 //当第一次出现有叶子节点是flag变为true 32 //若是flag记为true,那么后面的节点若是还有子节点也没有必要再放进去了 33 if(p[id].size()!=0) 34 { 35 if(flag==false) 36 { 37 int len = p[id].size(); 38 for(int i = 0;i<len;i++) 39 { 40 node tn={p[id][i],ct+1};//结构体对象赋值的简化表达 41 q.push(tn); 42 } 43 } 44 } 45 else 46 { 47 //若是第一次有叶子节点即(flag==false) 48 //或者后面发现叶子节点层数相同(minct==ct) 49 if(flag==false||minct==ct) 50 { 51 flag=true; 52 minct=ct; 53 c++; 54 } 55 } 56 } 57 return c; 58 } 59 int main() 60 { 61 freopen("1106.txt","r",stdin); 62 int m,k,t,s; 63 cin>>m>>pi>>r; 64 for(int i = 0;i<m;i++) 65 { 66 scanf("%d",&k); 67 for(int j = 1;j<=k;j++) 68 { 69 scanf("%d",&t); 70 p[i].push_back(t); 71 } 72 } 73 node nd; 74 nd.v=0; 75 nd.cnt=0; 76 q.push(nd); 77 s = bfs(); 78 for(int i = 1;i<=minct;i++) 79 { 80 pi=pi*(1+r/100.0); 81 } 82 printf("%.4lf %d\n",pi,s); 83 return 0; 84 }