結果

問題 No.922 東北きりきざむたん
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-11-08 21:40:00
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 3,870 bytes
コンパイル時間 3,400 ms
コンパイル使用メモリ 104,144 KB
実行使用メモリ 27,708 KB
最終ジャッジ日時 2023-09-04 03:17:54
合計ジャッジ時間 9,693 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 67 ms
11,852 KB
testcase_10 AC 62 ms
6,640 KB
testcase_11 AC 67 ms
10,800 KB
testcase_12 AC 24 ms
13,632 KB
testcase_13 AC 22 ms
6,660 KB
testcase_14 AC 97 ms
17,164 KB
testcase_15 AC 13 ms
12,852 KB
testcase_16 AC 185 ms
18,264 KB
testcase_17 AC 192 ms
18,576 KB
testcase_18 AC 189 ms
18,000 KB
testcase_19 AC 187 ms
18,256 KB
testcase_20 AC 187 ms
18,340 KB
testcase_21 AC 194 ms
18,528 KB
testcase_22 AC 195 ms
18,392 KB
testcase_23 AC 222 ms
18,520 KB
testcase_24 AC 225 ms
17,980 KB
testcase_25 AC 209 ms
19,708 KB
testcase_26 AC 210 ms
19,864 KB
testcase_27 AC 214 ms
19,588 KB
testcase_28 AC 78 ms
17,716 KB
testcase_29 AC 201 ms
27,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum E = 18;

int N, M, Q;
int[] U, V;
int[] A, B;

int[][] G;
long[] freq, freqSum;
int[][] par;
int[] dep;

void dfs(int u, int p) {
  par[0][u] = p;
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  freqSum[u] = freq[u];
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u);
      freqSum[u] += freqSum[v];
    }
  }
}

int lca(int u, int v) {
  foreach_reverse (e; 0 .. E) {
    if (dep[u] - (1 << e) >= dep[v]) {
      u = par[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      v = par[e][v];
    }
  }
  foreach_reverse (e; 0 .. E) {
    if (par[e][u] != par[e][v]) {
      u = par[e][u];
      v = par[e][v];
    }
  }
  if (u != v) {
    u = par[0][u];
    v = par[0][v];
  }
  return u;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = readInt();
      Q = readInt();
      U = new int[M];
      V = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
      }
      A = new int[Q];
      B = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
      }
      
      G = new int[][N];
      foreach (i; 0 .. M) {
        G[U[i]] ~= V[i];
        G[V[i]] ~= U[i];
      }
      
      auto uf = new int[N];
      uf[] = -1;
      foreach (i; 0 .. M) {
        uf.connect(U[i], V[i]);
      }
      freq = new long[N];
      foreach (q; 0 .. Q) {
        if (uf.root(A[q]) != uf.root(B[q])) {
          ++freq[A[q]];
          ++freq[B[q]];
        }
      }
      
      freqSum = new long[N];
      dep = new int[N];
      par = new int[][](E, N);
      foreach (u; 0 .. N) {
        if (uf[u] < 0) {
          debug {
            writeln("root ", u);
          }
          dfs(u, u);
        }
      }
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          par[e + 1][u] = par[e][par[e][u]];
        }
      }
      debug {
        writeln("freqSum = ", freqSum);
      }
      
      long ans;
      foreach (q; 0 .. Q) {
        if (uf.root(A[q]) == uf.root(B[q])) {
          const l = lca(A[q], B[q]);
          const cost = dep[A[q]] + dep[B[q]] - 2 * dep[l];
          debug {
            writeln("same ", A[q], " ", B[q], ": ", cost);
          }
          ans += cost;
        }
      }
      foreach (u; 0 .. N) {
        if (par[0][u] != u) {
          ans += min(freqSum[u], freqSum[uf.root(u)] - freqSum[u]);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0