rakcode1998

Untitled

Mar 5th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ipow(a,b)+0.5 (int)pow(a,b)
  4. #define pb push_back
  5. #define mp make_pair
  6. #define mod 1000000007
  7. #define fi first
  8. #define se second
  9. #define pii pair<int,int>
  10.  
  11. #define rep(i,n) for(int i=0;i<n;i++)
  12. #define reps(i,a,b) for(int i=a;i<=b;i++)
  13.  
  14. using namespace std;
  15. typedef long long ll;
  16. typedef long double ld;
  17.  
  18. set<int>a[200005];
  19. bool vis[200005]={false};
  20.  
  21. bool comp(set<int>a,set<int>b)
  22. {
  23.     if(a.size()<b.size())
  24.     return true;
  25.     else
  26.     return false;
  27. }
  28.  
  29. inline bool operator<(const set<int>& lhs, const set<int>& rhs)
  30. {
  31.   return lhs.size() > rhs.size();
  32. }
  33.  
  34. class Task
  35. {
  36. public:
  37.     int value=0;
  38.     void solve(istream& in,ostream& out)
  39.     {
  40.         int n;
  41.         in>>n;
  42.  
  43.         int c,d;
  44.  
  45.         rep(i,n-1)
  46.         {
  47.             in>>c>>d;
  48.             a[c].insert(d);
  49.             a[d].insert(c);
  50.         }
  51.  
  52.         map<set<int>,int>check;
  53.  
  54.         for(int i=1;i<=n;i++)
  55.         {
  56.             check.insert(mp(a[i],i));
  57.         }
  58.  
  59.         for(auto j=check.begin();j!=check.end();j++)
  60.         {
  61.             for(auto h=(*j).fi.begin();h!=(*j).fi.end();h++)
  62.             {
  63.                 out<<*h<<" ";
  64.             }
  65.  
  66.             out<<endl;
  67.         }
  68.  
  69.         sort(a,a+n+3,comp);
  70.  
  71.         int result=0;
  72.         int color[n+10];
  73.         color[1]=1;
  74.         vis[1]=true;
  75.         int yup=2,st=2;
  76.  
  77.         for(int i=0;a[i].size()!=0;i++)
  78.         {
  79.       //      out<<a[i]<<endl;
  80.             yup=st;
  81.             int g=(*check.find(a[i])).se;
  82.             out<<g<<"dshhagshadsh"<<endl;
  83.             if(vis[g]==false)
  84.             {
  85.                  result++;
  86.                  vis[g]=true;
  87.                  color[g]=yup;
  88.                  yup++;
  89.           //       out<<g<<endl;
  90.  
  91.                  for(auto j=a[i].begin();j!=a[i].end();j++)
  92.                  {
  93.                      result++;
  94.                      if(!vis[*j])
  95.                      {
  96.                          vis[*j]=true;
  97.                          color[*j]=yup;
  98.                          yup++;
  99.                      }
  100.                  }
  101.             }
  102.  
  103.             if(a[i].size()==0)
  104.             break;
  105.         }
  106.  
  107.         out<<result<<endl;
  108.  
  109.         for(int i=1;i<=result;i++)
  110.         {
  111.             out<<color[i]<<" ";
  112.         }
  113.     }
  114.  
  115.     int dfs(int c, int d)
  116.     {
  117.         if(a[c].find(d)!=a[c].end())
  118.         return 1;
  119.         else
  120.         {
  121.             for(auto i=a[c].begin();i!=a[c].end();i++)
  122.             {
  123.                 if(a[*i].find(d)!=a[*i].end())
  124.                 {
  125.                     return 2;
  126.                 }
  127.             }
  128.  
  129.             return 0;
  130.         }
  131.     }
  132. };
  133.  
  134.  
  135. int main()
  136. {
  137.     ios_base::sync_with_stdio(false);
  138.     cin.tie(NULL);
  139.     cout.tie(NULL);
  140.     Task solver;
  141.     std::istream& in(std::cin);
  142.     std::ostream& out(std::cout);
  143.     solver.solve(in,out);
  144.     out.flush();
  145.  
  146.     return 0;
  147. }
Add Comment
Please, Sign In to add comment