fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define lli long long int
  4. #define li long int
  5. #define scan(x) cin >> x
  6.  
  7. #define pb push_back
  8. #define mk make_pair
  9. #define scantype int
  10. #define endl "\n"
  11. #define unique(x) x.erase(unique(x.begin(),x.end()), x.end())
  12. #define all(x) x.begin(),x.end()
  13.  
  14. lli MOD = 1000000007;
  15. lli inf = 1e15;
  16.  
  17. lli powermod(lli _a,lli _b,lli _m){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
  18. lli string_to_number(string s){lli x=0; stringstream convert(s); convert>>x; return x;}
  19. lli add(lli a,lli b){lli x = (a+b)%MOD; return x; }
  20. lli mul(lli a,lli b){lli x = (a*b)%MOD; return x; }
  21. lli sub(lli a,lli b){lli x = (a-b+MOD)%MOD; return x; }
  22. lli divi(lli a,lli b){lli x = a;lli y = powermod(b,MOD-2,MOD);lli res = (x*y)%MOD;return res;}
  23. #define N 100010
  24.  
  25. vector<int> adj[N+10],ans;
  26. vector<bool> vis(N+10);
  27. int dfs(int u){
  28. if(!vis[u]){
  29. vis[u]=true;
  30. int temp=0;
  31. for(int i=0;i<adj[u].size();i++)
  32. temp+=dfs(adj[u][i]);
  33. return temp+1;
  34. }
  35. return 0;
  36. }
  37.  
  38.  
  39. int main(){
  40. //Code here
  41. int n,m,t1,t2;
  42. scanf("%d%d",&n,&m);
  43. for(int i=0;i<m;i++){
  44. scanf("%d%d",&t1,&t2);
  45. adj[t1].pb(t2);
  46. adj[t2].pb(t1);
  47. }
  48. for(int i=0;i<n;i++){
  49. if(!vis[i])
  50. ans.pb(dfs(i));
  51. }
  52. lli cnt=0;
  53. for(auto it:ans)
  54. cnt+=it;
  55. lli pnt=0;
  56. for(auto it:ans){
  57. cnt-=it;
  58. pnt+=(lli)it*cnt;
  59. }
  60. cout << pnt;
  61. return 0;
  62. }
Success #stdin #stdout 0s 17616KB
stdin
Standard input is empty
stdout
61566156