Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #define pb push_back
- #define mp make_pair
- #define mod 1000000007
- #define rep(i,n) for(int i=0;i<n;i++)
- #define reps(i,a,b) for(int i=a;i<=b;i++)
- #define rev(i,a,b) for(int i=a;i>=b;i--)
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- class Task
- {
- public:
- const int mod2=1000000;
- set<int>a[2222];
- void solve(istream& in,ostream& out)
- {
- int n;
- in>>n;
- int c;
- reps(i,1,n)
- {
- in>>c;
- if(c!=-1)
- {
- a[c].insert(i);
- }
- }
- done(n);
- int res=1;
- for(int i=1;i<=n;i++)
- {
- for(auto h=a[i].begin();h!=a[i].end();h++)
- {
- out<<(*h)<<" ";
- }
- if(a[i].size()!=0)
- {
- out<<endl;
- }
- }
- set<int>store;
- for(int i=1;i<=n;i++)
- {
- store.insert(i);
- }
- set<int>repeat;
- for(int i=1;i<=n;i++)
- {
- if(a[i].size()!=0)
- {
- repeat.insert(i);
- store.erase(i);
- for(auto h=store.begin();h!=store.end();h++)
- {
- if(a[i].find(*h)==a[i].end() && (*h)!=i)
- {
- repeat.insert(*h);
- store.erase(*h);
- }
- }
- if(repeat.size()!=0)
- {
- res++;
- repeat.clear();
- }
- }
- }
- out<<res<<endl;
- }
- void done(int n)
- {
- for(int i=1;i<=n;i++)
- {
- if(a[i].size()!=0)
- {
- for(auto j=a[i].begin();j!=a[i].end();j++)
- {
- if(a[*j].size()!=0)
- {
- for(auto h=a[*j].begin();h!=a[*j].end();h++)
- {
- a[i].insert(*h);
- }
- }
- }
- }
- }
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- Task solver;
- istream& in(std::cin);
- ostream& out(std::cout);
- solver.solve(in,out);
- out.flush();
- return 0;
- }
Add Comment
Please, Sign In to add comment