Guest User

Untitled

a guest
May 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. using namespace std;
  6.  
  7. long long n,m;
  8. vector <vector <int> > G;
  9. vector <pair<int,int> > M;
  10.  
  11. int dsu_parent[5000];
  12. int dsu_rank[5000];
  13. int size_of_component[5000];
  14. int edges_of_component[5000];
  15. set <int> all_components;
  16. long long C[5001][5001];
  17. long long answer_one=0,answer_two=0,answer_three=0,answer_four=0;
  18. long long real_answer=0;
  19. int MAXN=5000;
  20.  
  21. ostream & operator<<(ostream &stream, vector <int> &A){
  22.     for (int i=0;i<A.size();i++)
  23.         cout<<A[i]<<" ";
  24.     return stream;
  25. }
  26.  
  27. int dsu_make(int i){
  28.     dsu_parent[i]=i;
  29.     dsu_rank[i]=0;
  30. }
  31.  
  32. void pascal_init(){
  33.     for (int i=0;i<=n;i++)
  34.         C[i][i]=1,C[i][1]=i;
  35.     for (int i=2;i<=n;i++)
  36.     for (int j=2;j<=n-1;j++){
  37.         C[i][j]=C[i-1][j-1]+C[i-1][j];
  38.         //cout<<C[i][j]<<" "<<endl;
  39.     }
  40.     real_answer=C[n][1]+C[n][2]+C[n][3]+C[n][4];
  41. }
  42.  
  43. int dsu_find(int i){
  44.     if (i==dsu_parent[i])
  45.         return i;
  46.     return dsu_parent[i]=dsu_find(dsu_parent[i]);
  47. }
  48.  
  49. void dsu_unite(int a, int b){
  50.     a=dsu_find(a);
  51.     b=dsu_find(b);
  52.     if (a!=b){
  53.         if (dsu_rank[a]<dsu_rank[b])
  54.             swap(a,b);
  55.         dsu_parent[b]=a;
  56.         size_of_component[a]+=size_of_component[b];
  57.         edges_of_component[a]+=edges_of_component[b];
  58.         all_components.erase(b);
  59.         if (dsu_rank[a]==dsu_rank[b])
  60.             dsu_rank[a]++;
  61.     }
  62. }
  63.  
  64. void iterat_for_three(int numb){
  65.     //cout<<"ENTEROFTHREE"<<endl;
  66.     int u=M[numb].first; int v=M[numb].second;
  67.     long long k1=G[u].size();
  68.     long long k2=G[v].size();
  69.     sort (G[u].begin(),G[u].end());
  70.     sort (G[v].begin(),G[v].end());
  71.     vector<int> intersect_of_lists(G[u].size()+G[v].size());
  72.     vector<int>::iterator ite=set_intersection(G[u].begin(),G[u].end(),G[v].begin(),G[v].end(),intersect_of_lists.begin());
  73.     long long e=ite-intersect_of_lists.begin();
  74.     long long answer=(n-2)-k1-k2+e;
  75.     answer_three+=answer;
  76.     //cout<<"EXITOFTHREE"<<endl;
  77. }
  78.  
  79. void iterat_for_four(int numb){
  80.     long long answer=0;
  81.    
  82.    
  83.     answer_four+=answer;
  84.     dsu_unite(u,v);
  85.     u_comp=dsu_find(u);
  86.     G[u].push_back(v);
  87.     G[v].push_back(u);
  88.     edges_of_component[u_comp]++;
  89.     //cout<<"EXIT"<<endl;
  90. }
  91.  
  92. int main(){
  93.     cin>>n>>m;
  94.     G.resize(n);
  95.     M.resize(m);
  96.     answer_one=0; answer_two=m;
  97.     for (int i=0;i<m;i++){
  98.         int a,b; cin>>a>>b;
  99.         a--; b--;
  100.         if (a>b) swap(a,b);
  101.         M[i]=(make_pair(a,b));
  102.     }
  103.     for (int i=0;i<n;i++){
  104.         dsu_make(i);
  105.         size_of_component[i]=1;
  106.         all_components.insert(i);
  107.     }
  108.     for (int i=0;i<m;i++){
  109.         iterat_for_three(i);
  110.         iterat_for_four(i);
  111.     }
  112.     pascal_init();
  113.     cout<<C[n][4]-answer_four<<" "<<C[n][3]-answer_three<<" "<<real_answer<<endl;
  114.     cout<<real_answer-answer_one-answer_two-answer_three-answer_four<<endl;
  115.     return 0;
  116. }
Add Comment
Please, Sign In to add comment