fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int array[1000000];
  5. int array1[1000000];
  6. int main()
  7. {
  8. int n,m;
  9. cin>>n>>m;
  10. int group=0;
  11. int total=0;
  12. int a,b;
  13. for(int i=0;i<m;i++)
  14. {
  15. cin>>a>>b;
  16.  
  17. if(array[a]!=0 && array[b]==0)
  18. {
  19. array[b]=array[a];
  20. }else if(array[a]==0 && array[b]!=0)
  21. {
  22. array[a]=array[b];
  23. }
  24. else if(array[a]==0 && array[b]==0)
  25. {
  26. group++;
  27. total++;
  28. array[a]=group;
  29. array[b]=group;
  30. }else if(array[a]!=0 && array[b]!=0 && array[a]!=array[b])
  31. {
  32. /* for(int i=1;i<=n;i++)
  33.   {
  34.   cout<<array[i]<<"huhuhuhuhuhu\n";
  35.   }*/
  36. int temp=array[b];
  37. for(int z=1;z<=n;z++)
  38. {
  39. if(array[z]==temp)
  40. {array[z]=array[a];
  41. //cout<<z<<"sdfsdfdsf"<<array[z]<<"dfsada\n";
  42. }
  43. }
  44. total--;
  45. }
  46. }
  47. //cout<<group<<"dasd"<<total<<"adasda\n";
  48.  
  49. /*for(int i=1;i<=n;i++)
  50.   {
  51.   cout<<array[i]<<"\n";
  52.   }*/
  53.  
  54. for(int i=1;i<=n;i++)
  55. {
  56. //cout<<array[i]<<"\n";
  57. array1[array[i]]++;
  58.  
  59. }
  60. /*for(int i=1;i<=group;i++)
  61.   {
  62.   cout<<array1[i]<<"\n";
  63.   }*/
  64. int ans=0;
  65. for(int i=0;i<=group;i++)
  66. {
  67. for(int j=i+1;j<=group;j++)
  68. {
  69. ans=ans+array1[i]*array1[j];
  70. }
  71. }
  72.  
  73. cout<<ans<<"\n";
  74. return 0;
  75. }
  76.  
  77.  
  78. /*
  79. 5 3
  80. 1 2
  81. 3 4
  82. 1 3
  83. */
  84.  
Success #stdin #stdout 0s 11160KB
stdin
5 3
1 2
3 4
1 3
stdout
4