rakcode1998

Untitled

Mar 5th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 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. struct comps
  30. {
  31.     bool operator()(set<int>a,set<int>b)
  32.     {
  33.        if(a.size()<b.size())
  34.        return false;
  35.        else
  36.        return true;
  37.     }
  38. };
  39.  
  40. class Task
  41. {
  42. public:
  43.     int value=0;
  44.     void solve(istream& in,ostream& out)
  45.     {
  46.         int n;
  47.         in>>n;
  48.  
  49.         int c,d;
  50.  
  51.         rep(i,n-1)
  52.         {
  53.             in>>c>>d;
  54.             a[c].insert(d);
  55.             a[d].insert(c);
  56.         }
  57.  
  58.         map<set<int>,int,comps>check;
  59.  
  60.         for(int i=1;i<=n;i++)
  61.         {
  62.             check.insert(mp(a[i],i));
  63.         }
  64.  
  65.         for(auto j=check.begin();j!=check.end();j++)
  66.         {
  67.             for(auto h=(*j).fi.begin();h!=(*j).fi.end();h++)
  68.             {
  69.                 out<<*h<<" ";
  70.             }
  71.  
  72.             out<<endl;
  73.         }
  74.  
  75.         sort(a,a+n+3,comp);
  76.  
  77.         int result=0;
  78.         int color[n+10];
  79.         color[1]=1;
  80.         vis[1]=true;
  81.         int yup=2,st=2;
  82.  
  83.         for(int i=0;a[i].size()!=0;i++)
  84.         {
  85.       //      out<<a[i]<<endl;
  86.             yup=st;
  87.             int g=(*check.find(a[i])).se;
  88.             out<<g<<endl;
  89.             if(vis[g]==false)
  90.             {
  91.                  result++;
  92.                  vis[g]=true;
  93.                  color[g]=yup;
  94.                  yup++;
  95.           //       out<<g<<endl;
  96.  
  97.                  for(auto j=a[i].begin();j!=a[i].end();j++)
  98.                  {
  99.                      result++;
  100.                      if(!vis[*j])
  101.                      {
  102.                          vis[*j]=true;
  103.                          color[*j]=yup;
  104.                          yup++;
  105.                      }
  106.                  }
  107.             }
  108.  
  109.             if(a[i].size()==0)
  110.             break;
  111.         }
  112.  
  113.         out<<result<<endl;
  114.  
  115.         for(int i=1;i<=result;i++)
  116.         {
  117.             out<<color[i]<<" ";
  118.         }
  119.     }
  120.  
  121.     int dfs(int c, int d)
  122.     {
  123.         if(a[c].find(d)!=a[c].end())
  124.         return 1;
  125.         else
  126.         {
  127.             for(auto i=a[c].begin();i!=a[c].end();i++)
  128.             {
  129.                 if(a[*i].find(d)!=a[*i].end())
  130.                 {
  131.                     return 2;
  132.                 }
  133.             }
  134.  
  135.             return 0;
  136.         }
  137.     }
  138. };
  139.  
  140.  
  141. int main()
  142. {
  143.     ios_base::sync_with_stdio(false);
  144.     cin.tie(NULL);
  145.     cout.tie(NULL);
  146.     Task solver;
  147.     std::istream& in(std::cin);
  148.     std::ostream& out(std::cout);
  149.     solver.solve(in,out);
  150.     out.flush();
  151.  
  152.     return 0;
  153. }
Add Comment
Please, Sign In to add comment