Submission #5990986


Source Code Expand

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <functional>
#include <bitset>
#include <cstdint>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

i64 n, m;
vector<vector<i64>> g;

vector<bool> seen;
pair<i64, i64> dfs(i64 u) {
  if (seen[u]) return { 0, 0 };
  seen[u] = true;
  i64 cv = 1, ce = 0;
  for (i64 v : g[u]) {
    auto p = dfs(v);
    cv += p.first;
    ce += 1 + p.second;
  }
  return { cv, ce };
}

int main() {
  cin >> n >> m;
  init_n(g, n + 1);
  for (i64 i = 0; i < m; ++i) {
    i64 a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  init_n(seen, n + 1);

  auto p1 = dfs(1);
  auto p2 = dfs(2);

  if (p1.first < p2.first) swap(p1, p2);

  i64 v1 = n - p2.first, e1 = m - p2.second,
      v2 = p2.first, e2 = p2.second;

  i64 y = v1 * (v1 - 1) / 2 - e1
        + v2 * (v2 - 1) / 2 - e2;

  cout << y << '\n';
  return 0;
}

Submission Info

Submission Time
Task D - Shock
User cottoncotton
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1667 Byte
Status AC
Exec Time 79 ms
Memory 10368 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 2 ms 2560 KB
b06 AC 69 ms 10368 KB
b07 AC 69 ms 8064 KB
b08 AC 35 ms 5760 KB
b09 AC 69 ms 10368 KB
b10 AC 68 ms 5760 KB
b11 AC 1 ms 256 KB
b12 AC 73 ms 6144 KB
b13 AC 79 ms 6272 KB
b14 AC 75 ms 6272 KB
b15 AC 78 ms 6272 KB
b16 AC 78 ms 6656 KB
b17 AC 73 ms 6144 KB
b18 AC 60 ms 4736 KB
b19 AC 60 ms 4736 KB
b20 AC 4 ms 768 KB
b21 AC 12 ms 896 KB
b22 AC 73 ms 6144 KB
b23 AC 76 ms 6784 KB