結果

問題 No.1789 Tree Growing
ユーザー 👑 NachiaNachia
提出日時 2021-12-18 00:48:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,290 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 102,768 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-15 01:54:36
合計ジャッジ時間 16,847 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 6 ms
5,376 KB
testcase_26 AC 7 ms
5,376 KB
testcase_27 AC 6 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 8 ms
5,376 KB
testcase_31 AC 9 ms
5,376 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 10 ms
5,376 KB
testcase_35 WA -
testcase_36 AC 9 ms
5,376 KB
testcase_37 WA -
testcase_38 AC 8 ms
5,376 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 10 ms
5,376 KB
testcase_43 AC 12 ms
5,376 KB
testcase_44 WA -
testcase_45 AC 10 ms
5,376 KB
testcase_46 AC 12 ms
5,376 KB
testcase_47 WA -
testcase_48 AC 13 ms
5,376 KB
testcase_49 AC 12 ms
5,376 KB
testcase_50 AC 12 ms
5,376 KB
testcase_51 WA -
testcase_52 AC 12 ms
5,376 KB
testcase_53 AC 12 ms
5,376 KB
testcase_54 AC 8 ms
5,376 KB
testcase_55 AC 8 ms
5,376 KB
testcase_56 AC 34 ms
5,376 KB
testcase_57 AC 122 ms
5,376 KB
testcase_58 AC 13 ms
5,376 KB
testcase_59 AC 47 ms
5,376 KB
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 AC 367 ms
5,376 KB
testcase_65 WA -
testcase_66 AC 132 ms
5,376 KB
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 AC 264 ms
5,376 KB
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 AC 12 ms
5,376 KB
testcase_82 AC 13 ms
5,376 KB
testcase_83 WA -
testcase_84 WA -
testcase_85 AC 42 ms
5,376 KB
testcase_86 AC 42 ms
5,376 KB
testcase_87 AC 48 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


const i64 INF = 1001001001001001001;

pair<vector<i64>,vector<int>> dense_dijkstra(const vector<vector<i64>>& E, const vector<i64>& P, int s){
  int n = E.size();
  vector<i64> D(n, INF);
  D[s] = 0;
  vector<int> pre(n, -1);
  vector<int> visited(n, 0);
  int p = s;
  while(p != -1){
    visited[p] = 1;
    for(int e=0; e<n; e++) if(D[e] > D[p] + P[p] - P[e] + E[p][e]){
      pre[e] = p;
      D[e] = D[p] + P[p] - P[e] + E[p][e];
    }
    int nx = -1;
    i64 nxd = INF;
    for(int e=0; e<n; e++) if(visited[e] == 0) if(D[e] < nxd){ nx = e; nxd = D[nx]; }
    p = nx;
  }
  return make_pair(move(D), move(pre));
}


vector<int> assignment_problem(const vector<vector<i64>>& A){
  int n = A.size();
  vector<vector<i64>> E(n*2+2, vector<i64>(n*2+2,INF));
  rep(i,n) E[n*2][i] = 0;
  rep(i,n) E[n+i][n*2+1] = 0;
  rep(i,n){
    i64 mina = INF;
    rep(j,n) mina = min(mina, A[i][j]);
    rep(j,n) E[i][n+j] = A[i][j] - mina;
  }
  vector<int> ans(n,-1);
  vector<i64> P(n*2+2,0);
  rep(t,n){
    vector<i64> D;
    vector<int> pre;
    tie(D,pre) = dense_dijkstra(E,P,n*2);
    rep(i,n*2+2) P[i] += D[i];
    int p = n*2+1;
    while(p != n*2){
      E[p][pre[p]] = -E[pre[p]][p];
      E[pre[p]][p] = INF;
      if(pre[p] < n) ans[pre[p]] = p-n;
      p = pre[p];
    }
  }
  return ans;
}


int small_N;
vector<vector<int>> small_E;
vector<vector<int>> small_E_idx;
vector<vector<int>> small_E_for_E;
vector<pair<int,int>> small_edges;
int N;
vector<vector<int>> E;
vector<int> bfsorder;
vector<int> parent;
vector<vector<int>> dp;
int ans;


int update_dp(int p){
    int res = -1;
    rep(sp, small_N){
        if(E[p].size() < small_E[sp].size()) continue;
        int assignment_N = E[p].size();
        if(assignment_N < small_E[sp].size()) continue;
        //cout << "assignment_N = " << assignment_N << endl;
        vector<vector<i64>> assignment_matrix(assignment_N, vector<i64>(assignment_N, 0));
        rep(ei, E[p].size()){
            rep(sei, small_E[sp].size()){
                int tmp = dp[E[p][ei]][small_E_idx[sp][sei]];
                assignment_matrix[ei][sei] = ((tmp == -1) ? 1000000000 : -tmp);
            }
        }
        auto assignment_res = assignment_problem(assignment_matrix);
        i64 costsum = 0;
        rep(i, assignment_N) costsum += assignment_matrix[i][assignment_res[i]];
        //cout << "costsum = " << costsum << endl;
        res = max<i64>(res, -costsum);
    }
    rep(se, small_edges.size()){
        for(int e : E[p]) if(dp[e][se] != -1) dp[p][se] = max(dp[p][se], dp[e][se]+1);
        int assignment_N = E[p].size();
        if(assignment_N < small_E_for_E[se].size()) continue;
        //cout << "assignment_N = " << assignment_N << endl;
        vector<vector<i64>> assignment_matrix(assignment_N, vector<i64>(assignment_N, 0));
        rep(ei, E[p].size()){
            rep(sei, small_E_for_E[se].size()){
                int tmp = dp[E[p][ei]][small_E_for_E[se][sei]];
                assignment_matrix[ei][sei] = ((tmp == -1) ? 1000000000 : -tmp);
            }
        }
        auto assignment_res = assignment_problem(assignment_matrix);
        i64 costsum = 0;
        rep(i, assignment_N) costsum += assignment_matrix[i][assignment_res[i]];
        //cout << "costsum = " << costsum << endl;
        dp[p][se] = max<i64>(dp[p][se], -costsum+1);
    }
    //cout << "dp[" << p << "] = [ "; for(auto a : dp[p]) cout << a << " "; cout << "]" << endl;
    rep(sp, small_N) if(small_E_idx[sp].size() == 1) res = max(res, dp[p][small_E_idx[sp][0]]);
    return res;
}


int main() {
    cin >> small_N;
    small_E.resize(small_N);
    small_E_idx.resize(small_N);
    rep(i,small_N-1){
        int u,v; cin >> u >> v; u--; v--;
        small_E[u].push_back(v);
        small_E[v].push_back(u);
        small_E_idx[u].push_back(i*2);
        small_E_idx[v].push_back(i*2+1);
        small_edges.push_back(make_pair(u,v));
        small_edges.push_back(make_pair(v,u));
    }

    small_E_for_E.resize(small_edges.size());
    rep(i, small_E_for_E.size()){
        int to = small_edges[i].second;
        for(int ei : small_E_idx[to]) if((ei ^ 1) != i) small_E_for_E[i].push_back(ei);
    }

    cin >> N;
    E.resize(N);
    rep(i,N-1){
        int u,v; cin >> u >> v; u--; v--;
        E[u].push_back(v);
        E[v].push_back(u);
    }

    bfsorder = {0};
    parent.assign(N, -1);
    rep(i,bfsorder.size()){
        int p = bfsorder[i];
        for(int e : E[p]) if(parent[p] != e){
            parent[e] = p;
            bfsorder.push_back(e);
        }
        rep(j,E[p].size()) if(E[p][j] == parent[p]){ swap(E[p][j], E[p].back()); E[p].pop_back(); j--; }
    }

    dp.assign(N, vector<int>((small_N-1)*2, -1));

    //cout << "bfsorder = [ "; for(auto a : bfsorder) cout << a << " "; cout << "]" << endl;

    ans = -1;
    for(int i=N-1; i>=0; i--) ans = max(ans, update_dp(bfsorder[i]));
    cout << ans << endl;
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0