ferinの競プロ帳

競プロについてのメモ

Code Festival Team Relay D - Shock

問題ページ
D - Shock

考えたこと

  • 頂点1が含まれる連結成分V1と頂点2が含まれる連結成分V2とそれ以外の頂点集合V3に分割して考える
  • V1とV2の頂点が繋がれることはありえない
  • V1とV3をつなげばV2とV3をつなぐのは不可能で逆も同様
  • V1とV2で要素数が多い方をV3とつなぐべき
  • 連結成分ごとに頂点数と辺の数を求めるのはDFSすればできる
  • 辺の数は握手補題から次数の和/2で計算すると楽そう
  • V頂点E辺のグラフで書き足せる辺は V(V-1)/2 - E 本になる
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

VI g[100010];
int d[100010], edge, v;
bool used[100010];

void dfs(int x) {
  used[x] = true;
  edge += d[x];
  v++;
  for(auto e: g[x]) {
    if(!used[e]) dfs(e);
  }
}

signed main(void)
{
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].PB(b);
    g[b].PB(a);
    d[a]++, d[b]++;
  }

  v = 0, edge = 0;
  dfs(0);
  int v1 = v, e1 = edge/2;

  v = 0, edge = 0;
  dfs(1);
  int v2 = v, e2 = edge/2;

  int v3 = 0, e3 = 0;
  REP(i, n) {
    if(!used[i]) {
      v = 0, edge = 0;
      dfs(i);
      v3 += v;
      e3 += edge/2;
    }
  }

  int ret = 0;
  if(v1 > v2) {
    int v = v1+v3;
    ret += v*(v-1)/2 - e1 - e3;
    ret += v2*(v2-1)/2 - e2;
  } else {
    int v = v2+v3;
    ret += v*(v-1)/2 - e2 - e3;
    ret += v1*(v1-1)/2 - e1;
  }

  cout << ret << endl;

  return 0;
}