rakcode1998

Untitled

Feb 17th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3.  
  4. #define pb push_back
  5. #define mp make_pair
  6. #define mod 1000000007
  7.  
  8. #define rep(i,n) for(int i=0;i<n;i++)
  9. #define reps(i,a,b) for(int i=a;i<=b;i++)
  10. #define rev(i,a,b) for(int i=a;i>=b;i--)
  11.  
  12. using namespace std;
  13. typedef long long ll;
  14. typedef long double ld;
  15.  
  16. class Task
  17. {
  18. public:
  19.     const int mod2=1000000;
  20.     set<int>a[2222];
  21.     void solve(istream& in,ostream& out)
  22.     {
  23.         int n;
  24.         in>>n;
  25.         int c;
  26.  
  27.         reps(i,1,n)
  28.         {
  29.             in>>c;
  30.             if(c!=-1)
  31.             {
  32.                 a[c].insert(i);
  33.             }
  34.         }
  35.  
  36.         done(n);
  37.  
  38.         int res=1;
  39.  
  40.         for(int i=1;i<=n;i++)
  41.         {
  42.             for(auto h=a[i].begin();h!=a[i].end();h++)
  43.             {
  44.                 out<<(*h)<<" ";
  45.             }
  46.  
  47.             if(a[i].size()!=0)
  48.             {
  49.                 out<<endl;
  50.             }
  51.         }
  52.  
  53.         set<int>store;
  54.  
  55.         for(int i=1;i<=n;i++)
  56.         {
  57.             store.insert(i);
  58.         }
  59.  
  60.         set<int>repeat;
  61.         for(int i=1;i<=n;i++)
  62.         {
  63.             if(a[i].size()!=0)
  64.             {
  65.                 repeat.insert(i);
  66.                 store.erase(i);
  67.                 for(auto h=store.begin();h!=store.end();h++)
  68.                 {
  69.                      if(a[i].find(*h)==a[i].end() && (*h)!=i)
  70.                      {
  71.                          repeat.insert(*h);
  72.                          store.erase(*h);
  73.                      }
  74.                 }
  75.  
  76.                 if(repeat.size()!=0)
  77.                 {
  78.                      res++;
  79.                      repeat.clear();
  80.                 }
  81.             }
  82.         }
  83.  
  84.         out<<res<<endl;
  85.     }
  86.  
  87.     void done(int n)
  88.     {
  89.         for(int i=1;i<=n;i++)
  90.         {
  91.             if(a[i].size()!=0)
  92.             {
  93.                 for(auto j=a[i].begin();j!=a[i].end();j++)
  94.                 {
  95.                     if(a[*j].size()!=0)
  96.                     {
  97.                         for(auto h=a[*j].begin();h!=a[*j].end();h++)
  98.                         {
  99.                             a[i].insert(*h);
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106. };
  107.  
  108.  
  109. int main()
  110. {
  111.     ios_base::sync_with_stdio(false);
  112.     cin.tie(NULL);
  113.     cout.tie(NULL);
  114.     Task solver;
  115.     istream& in(std::cin);
  116.     ostream& out(std::cout);
  117.     solver.solve(in,out);
  118.     out.flush();
  119.  
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment