結果

問題 No.1789 Tree Growing
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-18 00:37:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,133 ms / 5,000 ms
コード長 6,306 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 109,304 KB
実行使用メモリ 15,108 KB
最終ジャッジ日時 2023-09-04 15:25:04
合計ジャッジ時間 15,098 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 8 ms
4,376 KB
testcase_21 AC 17 ms
4,376 KB
testcase_22 AC 13 ms
4,384 KB
testcase_23 AC 11 ms
4,380 KB
testcase_24 AC 14 ms
4,380 KB
testcase_25 AC 30 ms
4,380 KB
testcase_26 AC 35 ms
4,376 KB
testcase_27 AC 31 ms
4,376 KB
testcase_28 AC 24 ms
4,380 KB
testcase_29 AC 28 ms
4,376 KB
testcase_30 AC 45 ms
6,104 KB
testcase_31 AC 38 ms
4,380 KB
testcase_32 AC 41 ms
5,512 KB
testcase_33 AC 51 ms
4,940 KB
testcase_34 AC 46 ms
4,896 KB
testcase_35 AC 37 ms
4,384 KB
testcase_36 AC 52 ms
6,444 KB
testcase_37 AC 52 ms
4,896 KB
testcase_38 AC 49 ms
6,696 KB
testcase_39 AC 45 ms
4,932 KB
testcase_40 AC 50 ms
4,904 KB
testcase_41 AC 57 ms
5,928 KB
testcase_42 AC 61 ms
7,968 KB
testcase_43 AC 49 ms
4,872 KB
testcase_44 AC 56 ms
7,768 KB
testcase_45 AC 44 ms
6,152 KB
testcase_46 AC 67 ms
8,236 KB
testcase_47 AC 66 ms
7,968 KB
testcase_48 AC 73 ms
7,956 KB
testcase_49 AC 52 ms
6,724 KB
testcase_50 AC 48 ms
5,548 KB
testcase_51 AC 54 ms
8,104 KB
testcase_52 AC 62 ms
7,200 KB
testcase_53 AC 56 ms
4,908 KB
testcase_54 AC 37 ms
6,228 KB
testcase_55 AC 42 ms
5,916 KB
testcase_56 AC 46 ms
4,900 KB
testcase_57 AC 49 ms
5,604 KB
testcase_58 AC 38 ms
4,868 KB
testcase_59 AC 31 ms
4,376 KB
testcase_60 AC 882 ms
6,416 KB
testcase_61 AC 614 ms
7,488 KB
testcase_62 AC 1,153 ms
15,108 KB
testcase_63 AC 1,337 ms
12,568 KB
testcase_64 AC 378 ms
5,884 KB
testcase_65 AC 672 ms
6,956 KB
testcase_66 AC 181 ms
5,204 KB
testcase_67 AC 256 ms
6,924 KB
testcase_68 AC 213 ms
5,932 KB
testcase_69 AC 152 ms
5,644 KB
testcase_70 AC 248 ms
4,608 KB
testcase_71 AC 215 ms
4,952 KB
testcase_72 AC 2,133 ms
10,860 KB
testcase_73 AC 483 ms
10,284 KB
testcase_74 AC 35 ms
4,376 KB
testcase_75 AC 2 ms
4,380 KB
testcase_76 AC 1 ms
4,380 KB
testcase_77 AC 3 ms
4,380 KB
testcase_78 AC 3 ms
4,376 KB
testcase_79 AC 40 ms
4,380 KB
testcase_80 AC 56 ms
9,028 KB
testcase_81 AC 61 ms
7,492 KB
testcase_82 AC 75 ms
9,768 KB
testcase_83 AC 82 ms
5,916 KB
testcase_84 AC 72 ms
4,964 KB
testcase_85 AC 133 ms
13,784 KB
testcase_86 AC 62 ms
4,928 KB
testcase_87 AC 145 ms
12,388 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.mathspecial, 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)); }


// Dijkstra
class MinCostFlow(Capa, Cost, Capa CAPA_EPS = 0) {
  int n, m;
  int[][] g;
  int[] as, bs;
  Capa[] capa;
  Cost[] cost, pot;
  Capa tof;
  Cost toc;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; as = []; bs = []; capa = []; cost = [];
  }
  int addEdge(int u, int v, Capa w, Cost c) {
    debug {
      // writeln("addEdge ", u, " ", v, " ", w, " ", c);
    }
    const i = m;
    g[u] ~= m; as ~= u; bs ~= v; capa ~= w; cost ~= c; ++m;
    g[v] ~= m; as ~= v; bs ~= u; capa ~= 0; cost ~= -c; ++m;
    return i;
  }
  bool solve(int source, int sink, Capa flow) {
    pot = new Cost[n];
    pot[] = 0;
    for (; ; ) {
      bool cont;
      foreach (u; 0 .. n) foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
        const v = bs[i];
        if (pot[v] > pot[u] + cost[i]) {
          pot[v] = pot[u] + cost[i];
          cont = true;
        }
      }
      if (!cont) break;
    }
    auto dist = new Cost[n];
    auto prei = new int[n];
    auto vis = new bool[n];
    for (tof = 0, toc = 0; tof + CAPA_EPS < flow; ) {
      /*
      alias Entry = Tuple!(Cost, "c", int, "u");
      BinaryHeap!(Array!Entry, "a > b") que;
      */
      dist[] = -1;
      prei[] = -1;
      vis[] = false;
      dist[source] = 0;
      prei[source] = -2;
      /*
      que.insert(Entry(0, source));
      for (; !que.empty(); ) {
        const c = que.front.c;
        const u = que.front.u;
        que.removeFront;
        if (vis[u]) continue;
        vis[u] = true;
        foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
          const v = bs[i];
          if (vis[v]) continue;
          const cc = c + cost[i] + pot[u] - pot[v];
          if (prei[v] == -1 || dist[v] > cc) {
            dist[v] = cc;
            prei[v] = i;
            que.insert(Entry(cc, v));
          }
        }
      }
      */
      for (; ; ) {
        int um = -1;
        foreach (u; 0 .. n) if (!vis[u] && ~dist[u]) {
          if (!~um || dist[um] > dist[u]) {
            um = u;
          }
        }
        if (!~um) {
          break;
        }
        vis[um] = true;
        foreach (i; g[um]) if (capa[i] > CAPA_EPS) {
          const v = bs[i];
          if (!vis[v]) {
            const cc = dist[um] + cost[i] + pot[um] - pot[v];
            if (!~dist[v] || dist[v] > cc) {
              dist[v] = cc;
              prei[v] = i;
            }
          }
        }
      }
      if (!vis[sink]) return false;
      Capa f = flow - tof;
      for (int v = sink; v != source; ) {
        const i = prei[v];
        if (f > capa[i]) f = capa[i];
        v = as[i];
      }
      for (int v = sink; v != source; ) {
        const i = prei[v];
        capa[i] -= f; capa[i ^ 1] += f;
        v = as[i];
      }
      tof += f;
      toc += f * (dist[sink] - pot[source] + pot[sink]);
      pot[] += dist[];
    }
    return true;
  }
}


int[2] N;
int[][2] A, B;

int[][][2] G;

int[int] cache;
int calc(int u0, int p0, int u1, int p1) {
  const key = u0 | p0 << 8 | u1 << 16 | p1 << 24;
  auto ptr = key in cache;
  if (ptr) {
    return *ptr;
  }
  int[2] vsLens;
  int[][2] vss;
  foreach (h; 0 .. 2) {
    const u = (h == 0) ? u0 : u1;
    const p = (h == 0) ? p0 : p1;
    foreach (i; G[h][u]) {
      const v = A[h][i] ^ B[h][i] ^ u;
      if (v != p) {
        vss[h] ~= v;
      }
    }
    vsLens[h] = cast(int)(vss[h].length);
  }
  auto mcf = new MinCostFlow!(int, int)(2 + vsLens[0] + vsLens[1]);
  foreach (x; 0 .. vsLens[0]) {
    mcf.addEdge(0, 2 + x, 1, 0);
  }
  foreach (y; 0 .. vsLens[1]) {
    mcf.addEdge(2 + vsLens[0] + y, 1, 1, 0);
  }
  foreach (x; 0 .. vsLens[0]) foreach (y; 0 .. vsLens[1]) {
    const score = calcSub(vss[0][x], u0, vss[1][y], u1);
    if (score >= 0) {
      mcf.addEdge(2 + x, 2 + vsLens[0] + y, 1, -(1 + score));
    }
  }
  const status = mcf.solve(0, 1, vsLens[0]);
  const ret = status ? -mcf.toc : -1;
  return cache[key] = ret;
}

int[int] cacheSub;
int calcSub(int u0, int p0, int u1, int p1) {
  const key = u0 | p0 << 8 | u1 << 16 | p1 << 24;
  auto ptr = key in cacheSub;
  if (ptr) {
    return *ptr;
  }
  int ret = calc(u0, p0, u1, p1);
  foreach (i; G[1][u1]) {
    const v1 = A[1][i] ^ B[1][i] ^ u1;
    if (v1 != p1) {
      const res = calcSub(u0, p0, v1, u1);
      if (res >= 0) {
        chmax(ret, 1 + res);
      }
    }
  }
  return cacheSub[key] = ret;
}


void main() {
  try {
    for (; ; ) {
      foreach (h; 0 .. 2) {
        N[h] = readInt;
        A[h] = new int[N[h] - 1];
        B[h] = new int[N[h] - 1];
        foreach (i; 0 .. N[h] - 1) {
          A[h][i] = readInt - 1;
          B[h][i] = readInt - 1;
        }
      }
      
      foreach (h; 0 .. 2) {
        G[h] = new int[][N[h]];
        foreach (i; 0 .. N[h] - 1) {
          G[h][A[h][i]] ~= i;
          G[h][B[h][i]] ~= i;
        }
      }
      
      cache.clear;
      cacheSub.clear;
      int ans = -1;
      /*
      foreach (r0; 0 .. N[0]) foreach (r1; 0 .. N[1]) {
        chmax(ans, calc(r0, N[0], r1, N[1]));
      }
      */
      foreach (r1; 0 .. N[1]) {
        chmax(ans, calc(0, N[0], r1, N[1]));
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0