fork download
  1. #include <bits/stdc++.h>
  2.  
  3. const int N = 200005;
  4. using namespace std;
  5.  
  6. vector<pair<int, int>> edges;
  7. vector<int> adj[N];
  8. int low[N], num[N], cnt[N], parent[N], sz[N];
  9. bool isBridge[N];
  10. int n, m, timer, cntBridge, cc;
  11.  
  12. void dfs(int u) {
  13. num[u] = ++timer;
  14. low[u] = num[u];
  15. cnt[cc]++;
  16. sz[u] = 1;
  17.  
  18. for (int v : adj[u]) {
  19. if (v == parent[u])
  20. continue;
  21.  
  22. if (num[v]) {
  23. low[u] = min(low[u], num[v]);
  24. } else {
  25. parent[v] = u;
  26. dfs(v);
  27. low[u] = min(low[u], low[v]);
  28. sz[u] += sz[v];
  29.  
  30. if (low[v] > num[u]) {
  31. cntBridge++;
  32. isBridge[v] = true;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. long long solve() {
  39. if (cc > 2)
  40. return 0;
  41. if (cc == 2)
  42. return (long long)cnt[1] * cnt[2] * (m - cntBridge);
  43.  
  44. long long ans = 0;
  45. long long maxE = (long long)n * (n - 1) / 2;
  46.  
  47. for (const auto &edge : edges) {
  48. int u = edge.first;
  49. int v = edge.second;
  50. if (num[u] > num[v])
  51. swap(u, v);
  52. if (parent[v] == u && isBridge[v]) {
  53. ans += (long long)sz[v] * (sz[1] - sz[v]) - 1;
  54. } else {
  55. ans += maxE - m;
  56. }
  57. }
  58. return ans;
  59. }
  60.  
  61. int main() {
  62. ios::sync_with_stdio(0);
  63. cin.tie(0);
  64.  
  65. cin >> n >> m;
  66. for (int i = 0; i < m; i++) {
  67. int u, v;
  68. cin >> u >> v;
  69. adj[u].push_back(v);
  70. adj[v].push_back(u);
  71. edges.push_back({u, v});
  72. }
  73.  
  74. for (int i = 1; i <= n; i++) {
  75. if (num[i] == 0) {
  76. cc++;
  77. dfs(i);
  78. }
  79. }
  80.  
  81. cout << solve();
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0.01s 9624KB
stdin
Standard input is empty
stdout
Standard output is empty