fork download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 1234567890
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-8
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. #define dbl long double
  28. using namespace std;
  29. // mylittledoge
  30.  
  31. int main() {
  32. cin.sync_with_stdio(0);
  33. cin.tie(0);
  34. int N;
  35. cin >> N;
  36. vector< vector<int> > son(N);
  37. for(int i =1; i < N; i++) {
  38. int a;
  39. cin >> a;
  40. son[--a].push_back(i);}
  41.  
  42. vector<int> S(N,1);
  43. for(int i =N-1; i >= 0; i--)
  44. ALL_THE(son[i],it) S[i] +=S[*it];
  45.  
  46. vector< pair<int,int> > D;
  47. for(int i =0; i < N; i++) if(son[i].size() > 1)
  48. D.push_back(make_pair(son[i].size(),i));
  49. sort(D.begin(),D.end());
  50.  
  51. cout << "1";
  52. for(int k =N-1; k >= 1; k--) if(N%k == 0) {
  53. bool ok =true;
  54. for(int i =D.size()-1; i >= 0; i--) if(S[D[i].ss] > k) {
  55. int s =0;
  56. ALL_THE(son[D[i].ss],it) s +=S[*it]%k;
  57. if(s+1 > k) {ok =false; break;}
  58. }
  59. if(ok) cout << " " << N/k;}
  60.  
  61. cout << "\n";
  62. return 0;}
  63.  
  64. // look at my code
  65. // my code is amazing
Runtime error #stdin #stdout #stderr 0s 4504KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc