結果
問題 | No.1789 Tree Growing |
ユーザー | 👑 Nachia |
提出日時 | 2021-12-18 00:41:16 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,137 bytes |
コンパイル時間 | 1,513 ms |
コンパイル使用メモリ | 102,220 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-15 01:47:58 |
合計ジャッジ時間 | 16,793 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
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 | WA | - |
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 | AC | 3 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 3 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 3 ms
5,376 KB |
testcase_18 | AC | 3 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 | AC | 4 ms
5,376 KB |
testcase_24 | AC | 4 ms
5,376 KB |
testcase_25 | AC | 7 ms
5,376 KB |
testcase_26 | AC | 7 ms
5,376 KB |
testcase_27 | WA | - |
testcase_28 | AC | 6 ms
5,376 KB |
testcase_29 | AC | 6 ms
5,376 KB |
testcase_30 | AC | 8 ms
5,376 KB |
testcase_31 | WA | - |
testcase_32 | AC | 7 ms
5,376 KB |
testcase_33 | AC | 8 ms
5,376 KB |
testcase_34 | WA | - |
testcase_35 | AC | 7 ms
5,376 KB |
testcase_36 | AC | 9 ms
5,376 KB |
testcase_37 | AC | 9 ms
5,376 KB |
testcase_38 | AC | 8 ms
5,376 KB |
testcase_39 | AC | 7 ms
5,376 KB |
testcase_40 | AC | 8 ms
5,376 KB |
testcase_41 | AC | 10 ms
5,376 KB |
testcase_42 | AC | 9 ms
5,376 KB |
testcase_43 | AC | 13 ms
5,376 KB |
testcase_44 | AC | 11 ms
5,376 KB |
testcase_45 | AC | 10 ms
5,376 KB |
testcase_46 | AC | 12 ms
5,376 KB |
testcase_47 | AC | 12 ms
5,376 KB |
testcase_48 | AC | 13 ms
5,376 KB |
testcase_49 | WA | - |
testcase_50 | AC | 12 ms
5,376 KB |
testcase_51 | AC | 12 ms
5,376 KB |
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 | 7 ms
5,376 KB |
testcase_56 | AC | 33 ms
5,376 KB |
testcase_57 | AC | 124 ms
5,376 KB |
testcase_58 | AC | 13 ms
5,376 KB |
testcase_59 | AC | 47 ms
5,376 KB |
testcase_60 | AC | 965 ms
5,376 KB |
testcase_61 | AC | 569 ms
5,376 KB |
testcase_62 | AC | 1,507 ms
5,376 KB |
testcase_63 | AC | 1,831 ms
5,376 KB |
testcase_64 | AC | 364 ms
5,376 KB |
testcase_65 | AC | 748 ms
5,376 KB |
testcase_66 | WA | - |
testcase_67 | AC | 217 ms
5,376 KB |
testcase_68 | AC | 248 ms
5,376 KB |
testcase_69 | WA | - |
testcase_70 | AC | 624 ms
5,376 KB |
testcase_71 | AC | 463 ms
5,376 KB |
testcase_72 | AC | 3,268 ms
5,376 KB |
testcase_73 | AC | 902 ms
5,376 KB |
testcase_74 | AC | 262 ms
5,376 KB |
testcase_75 | AC | 2 ms
5,376 KB |
testcase_76 | AC | 2 ms
5,376 KB |
testcase_77 | WA | - |
testcase_78 | WA | - |
testcase_79 | AC | 112 ms
5,376 KB |
testcase_80 | AC | 12 ms
5,376 KB |
testcase_81 | AC | 12 ms
5,376 KB |
testcase_82 | AC | 13 ms
5,376 KB |
testcase_83 | AC | 34 ms
5,376 KB |
testcase_84 | AC | 34 ms
5,376 KB |
testcase_85 | AC | 41 ms
5,376 KB |
testcase_86 | AC | 40 ms
5,376 KB |
testcase_87 | AC | 49 ms
5,376 KB |
ソースコード
#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(); //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); if(E[p].size() < small_E_for_E[se].size()) continue; int assignment_N = E[p].size(); //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; 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;