Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ipow(a,b)+0.5 (int)pow(a,b)
- #define pb push_back
- #define mp make_pair
- #define mod 1000000007
- #define fi first
- #define se second
- #define pii pair<int,int>
- #define rep(i,n) for(int i=0;i<n;i++)
- #define reps(i,a,b) for(int i=a;i<=b;i++)
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- set<int>a[200005];
- bool vis[200005]={false};
- bool comp(set<int>a,set<int>b)
- {
- if(a.size()<b.size())
- return true;
- else
- return false;
- }
- struct comps
- {
- bool operator()(set<int>a,set<int>b)
- {
- if(a.size()<b.size())
- return false;
- else
- return true;
- }
- };
- class Task
- {
- public:
- int value=0;
- void solve(istream& in,ostream& out)
- {
- int n;
- in>>n;
- int c,d;
- rep(i,n-1)
- {
- in>>c>>d;
- a[c].insert(d);
- a[d].insert(c);
- }
- map<set<int>,int,comps>check;
- for(int i=1;i<=n;i++)
- {
- check.insert(mp(a[i],i));
- }
- for(auto j=check.begin();j!=check.end();j++)
- {
- for(auto h=(*j).fi.begin();h!=(*j).fi.end();h++)
- {
- out<<*h<<" ";
- }
- out<<endl;
- }
- sort(a,a+n+3,comp);
- int result=0;
- int color[n+10];
- color[1]=1;
- vis[1]=true;
- int yup=2,st=2;
- for(int i=0;a[i].size()!=0;i++)
- {
- // out<<a[i]<<endl;
- yup=st;
- int g=(*check.find(a[i])).se;
- out<<g<<endl;
- if(vis[g]==false)
- {
- result++;
- vis[g]=true;
- color[g]=yup;
- yup++;
- // out<<g<<endl;
- for(auto j=a[i].begin();j!=a[i].end();j++)
- {
- result++;
- if(!vis[*j])
- {
- vis[*j]=true;
- color[*j]=yup;
- yup++;
- }
- }
- }
- if(a[i].size()==0)
- break;
- }
- out<<result<<endl;
- for(int i=1;i<=result;i++)
- {
- out<<color[i]<<" ";
- }
- }
- int dfs(int c, int d)
- {
- if(a[c].find(d)!=a[c].end())
- return 1;
- else
- {
- for(auto i=a[c].begin();i!=a[c].end();i++)
- {
- if(a[*i].find(d)!=a[*i].end())
- {
- return 2;
- }
- }
- return 0;
- }
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- Task solver;
- std::istream& in(std::cin);
- std::ostream& out(std::cout);
- solver.solve(in,out);
- out.flush();
- return 0;
- }
Add Comment
Please, Sign In to add comment