團體程式設計天梯賽PTA L2-020功夫傳人
阿新 • • 發佈:2020-10-25
分析:題意不難理解,看上去就是不太難的題,然後WA了一天qaq
用bfs和並查集應該都能做,然後我沒壓縮好,一會超時一會超記憶體。最後用模擬stack儲存輩分數降低了遞迴的時間複雜度。
1 #include <iostream> 2 #include<cstdio> 3 #include<vector> 4 #include<map> 5 #include<stack> 6 using namespace std; 7 const int N = 1e5+10; 8 9 vector<int>ans;10 map<int,int>mp; 11 int cnt=0; 12 int fa[N]; 13 int gen[N]; 14 stack<int>q; 15 void fstack(int n) 16 { 17 while(!q.empty()) 18 q.pop(); 19 for(int i = 1; i < n; i ++) 20 { 21 if(fa[i]==0) 22 gen[i]=1; 23 else 24 { 25 q.push(i);26 while(gen[fa[q.top()]]!=0) 27 { 28 gen[q.top()] = gen[fa[q.top()]]+1; 29 q.pop(); 30 if(q.empty()) 31 break; 32 } 33 } 34 } 35 } 36 int main() 37 { 38 int n,m,a,flag; 39 doublez,r,bei[N]= {0},sum=0; 40 scanf("%d%lf%lf",&n,&z,&r); 41 fa[0] = 0; 42 for(int i = 0; i < n; i ++) 43 { 44 scanf("%d",&m); 45 if(m==0) 46 { 47 scanf("%d",&a); 48 ans.push_back(i); 49 mp[i]=a; 50 } 51 else 52 { 53 for(int j = 0; j < m; j ++) 54 { 55 scanf("%d",&a); 56 fa[a]=i; 57 } 58 } 59 } 60 fstack(n); 61 bei[0] = z; 62 double k = (100-r)/(double)100; 63 for(int i = 1; i < n; i ++) 64 { 65 bei[i] = bei[i-1]*k; 66 } 67 vector<int>::iterator it; 68 for(it = ans.begin(); it!=ans.end(); it++) 69 { 70 flag = *it; 71 sum += bei[gen[flag]]*mp[flag]; 72 } 73 int as = sum; 74 printf("%d\n",as); 75 return 0; 76 }