結果

問題 No.1789 Tree Growing
ユーザー KudeKude
提出日時 2021-12-18 21:17:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,738 bytes
コンパイル時間 3,443 ms
コンパイル使用メモリ 255,516 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2023-10-13 18:10:45
合計ジャッジ時間 20,660 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 4 ms
4,352 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 5 ms
4,352 KB
testcase_21 AC 9 ms
4,352 KB
testcase_22 AC 7 ms
4,348 KB
testcase_23 AC 7 ms
4,352 KB
testcase_24 AC 6 ms
4,348 KB
testcase_25 AC 17 ms
4,348 KB
testcase_26 AC 16 ms
4,352 KB
testcase_27 AC 15 ms
4,348 KB
testcase_28 AC 14 ms
4,348 KB
testcase_29 AC 15 ms
4,348 KB
testcase_30 AC 21 ms
4,352 KB
testcase_31 AC 22 ms
4,352 KB
testcase_32 AC 21 ms
4,348 KB
testcase_33 AC 21 ms
4,348 KB
testcase_34 AC 26 ms
4,348 KB
testcase_35 AC 19 ms
4,348 KB
testcase_36 AC 25 ms
4,352 KB
testcase_37 AC 26 ms
4,348 KB
testcase_38 AC 23 ms
4,348 KB
testcase_39 AC 18 ms
4,348 KB
testcase_40 AC 23 ms
4,348 KB
testcase_41 AC 27 ms
4,348 KB
testcase_42 AC 28 ms
4,352 KB
testcase_43 AC 32 ms
4,480 KB
testcase_44 AC 29 ms
4,488 KB
testcase_45 AC 27 ms
4,348 KB
testcase_46 AC 34 ms
4,444 KB
testcase_47 AC 34 ms
4,420 KB
testcase_48 AC 35 ms
4,468 KB
testcase_49 AC 32 ms
4,476 KB
testcase_50 AC 34 ms
4,428 KB
testcase_51 AC 35 ms
4,704 KB
testcase_52 AC 33 ms
4,584 KB
testcase_53 AC 35 ms
4,448 KB
testcase_54 AC 26 ms
4,348 KB
testcase_55 AC 23 ms
4,352 KB
testcase_56 AC 30 ms
4,348 KB
testcase_57 AC 29 ms
4,352 KB
testcase_58 AC 29 ms
4,352 KB
testcase_59 AC 23 ms
4,352 KB
testcase_60 TLE -
testcase_61 AC 2,610 ms
4,352 KB
testcase_62 TLE -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) { a = max(a, b); }
template<class T> void chmin(T& a, const T& b) { a = min(a, b); }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

int n1, n2;
VVI g1, g2;

void dfs1(int u, int p);
void dfs2(int u, int p);

vector<vector<vector<vector<int>>>> dp;
void Rerooting() {
  dp.resize(n2);
  dfs1(0, -1);
  dfs2(0, -1);
}

VVI calc_val(int u, int i) {
  const int v = g2[u][i];
  VVI d(n1);
  auto dfs = [&](auto&& self, int x, int p) -> void {
    d[x].resize(g1[x].size());
    int j = 0;
    for(int y : g1[x]) {
      // edge (x, j)
      if (y != p) self(self, y, x);

      // match u->v and x->y
      int mx = -1;
      // skip case
      for(int k = 0, sz = g2[v].size(); k < sz; k++) {
        int w = g2[v][k];
        if (w == u) continue;
        // v -> w
        int temp = dp[v][k][x][j];
        if (temp != -1) chmax(mx, temp + 1);
      }
      // put case
      // u->'v' x->'y'
      int sz1 = g1[y].size();
      int sz2 = g2[v].size();
      if (sz1 <= sz2) {
        int s = sz1 + sz2, t = s + 1;
        mcf_graph<int, int> g(t + 1);
        constexpr int B = 110;
        rep(i, sz1) rep(j, sz2) {
          int z = g1[y][i], w = g2[v][j];
          if (z == x || w == u) continue;
          int temp = dp[v][j][y][i];
          if (temp == -1) continue;
          g.add_edge(i, sz1 + j, 1, B - temp);
        }
        rep(i, sz1) g.add_edge(s, i, 1, 0);
        rep(j, sz2) g.add_edge(sz1 + j, t, 1, 0);
        auto [f, cost] = g.flow(s, t);
        if (f == sz1 - 1) {
          cost -= f * B;
          cost = -cost;
          chmax(mx, cost + 1);
        }
      }

      d[x][j++] = mx;
    }
  };
  dfs(dfs, 0, -1);
  return d;
}

void dfs1(int u, int p=-1) {
  const int sz2 = g2[u].size();
  dp[u].resize(sz2);

  for(int i = 0; i < sz2; i++) {
    int v = g2[u][i];
    if (v == p) continue;
    dfs1(v, u);
    // cout << u << "->" << v << "(" << i << ")" << endl;
    dp[u][i] = calc_val(u, i);
  }
  return;
}

void dfs2(int u, int p=-1) {
  const int sz = g2[u].size();

  for(int i = 0; i < sz; i++) {
    int v = g2[u][i];
    if (v == p) continue;
    const int sz_v = g2[v].size();
    for(int j = 0; j < sz_v; j++) {
      const int w = g2[v][j];
      if (w != u) continue;

      // dp[v][j] = sl[u][i].merge(sr[u][i + 1]).apply(e);
      dp[v][j] = calc_val(v, j);
      break;
    }
    dfs2(v, u);
  }
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  rep(_, 2) {
    cin >> n1;
    g1.resize(n1);
    rep(_, n1 - 1) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      g1[a].emplace_back(b);
      g1[b].emplace_back(a);
    }
    swap(n1, n2);
    swap(g1, g2);
  }
  Rerooting();
  int ans = -1;
  rep(x, n1) rep(u, n2) {
    int sz1 = g1[x].size(), sz2 = g2[u].size();
    int s = sz1 + sz2, t = s + 1;
    mcf_graph<int, int> g(t + 1);
    constexpr int B = 110;
    rep(i, sz1) rep(j, sz2) {
      int temp = dp[u][j][x][i];
      if (temp != -1) g.add_edge(i, sz1 + j, 1, B - temp);
    }
    rep(i, sz1) g.add_edge(s, i, 1, 0);
    rep(j, sz2) g.add_edge(sz1 + j, t, 1, 0);
    auto [f, cost] = g.flow(s, t);
    if (f == sz1) {
      cost -= f * B;
      cost = -cost;
      chmax(ans, cost);
    }
  }
  cout << ans << endl;
}
0