結果
問題 | No.1789 Tree Growing |
ユーザー | hotman78 |
提出日時 | 2021-12-18 11:51:29 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,703 ms / 5,000 ms |
コード長 | 8,845 bytes |
コンパイル時間 | 4,272 ms |
コンパイル使用メモリ | 241,220 KB |
実行使用メモリ | 450,560 KB |
最終ジャッジ日時 | 2023-10-13 16:08:39 |
合計ジャッジ時間 | 32,455 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 1 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 1 ms
4,348 KB |
testcase_04 | AC | 1 ms
4,352 KB |
testcase_05 | AC | 2 ms
4,348 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 2 ms
4,348 KB |
testcase_08 | AC | 2 ms
4,352 KB |
testcase_09 | AC | 1 ms
4,348 KB |
testcase_10 | AC | 2 ms
4,352 KB |
testcase_11 | AC | 2 ms
4,348 KB |
testcase_12 | AC | 2 ms
4,348 KB |
testcase_13 | AC | 2 ms
4,352 KB |
testcase_14 | AC | 2 ms
4,348 KB |
testcase_15 | AC | 5 ms
6,040 KB |
testcase_16 | AC | 2 ms
4,348 KB |
testcase_17 | AC | 6 ms
6,788 KB |
testcase_18 | AC | 4 ms
5,740 KB |
testcase_19 | AC | 2 ms
4,348 KB |
testcase_20 | AC | 8 ms
8,480 KB |
testcase_21 | AC | 25 ms
24,988 KB |
testcase_22 | AC | 10 ms
10,868 KB |
testcase_23 | AC | 12 ms
12,612 KB |
testcase_24 | AC | 14 ms
13,628 KB |
testcase_25 | AC | 86 ms
85,224 KB |
testcase_26 | AC | 83 ms
79,892 KB |
testcase_27 | AC | 76 ms
73,472 KB |
testcase_28 | AC | 72 ms
70,204 KB |
testcase_29 | AC | 87 ms
83,668 KB |
testcase_30 | AC | 149 ms
151,916 KB |
testcase_31 | AC | 151 ms
156,976 KB |
testcase_32 | AC | 161 ms
164,316 KB |
testcase_33 | AC | 141 ms
146,892 KB |
testcase_34 | AC | 208 ms
223,064 KB |
testcase_35 | AC | 120 ms
116,100 KB |
testcase_36 | AC | 191 ms
205,012 KB |
testcase_37 | AC | 214 ms
228,992 KB |
testcase_38 | AC | 189 ms
199,304 KB |
testcase_39 | AC | 134 ms
140,348 KB |
testcase_40 | AC | 185 ms
197,188 KB |
testcase_41 | AC | 230 ms
247,044 KB |
testcase_42 | AC | 273 ms
277,564 KB |
testcase_43 | AC | 356 ms
355,192 KB |
testcase_44 | AC | 295 ms
300,124 KB |
testcase_45 | AC | 249 ms
258,976 KB |
testcase_46 | AC | 362 ms
373,624 KB |
testcase_47 | AC | 377 ms
389,388 KB |
testcase_48 | AC | 407 ms
424,676 KB |
testcase_49 | AC | 359 ms
383,240 KB |
testcase_50 | AC | 382 ms
412,040 KB |
testcase_51 | AC | 429 ms
450,560 KB |
testcase_52 | AC | 363 ms
373,452 KB |
testcase_53 | AC | 396 ms
424,872 KB |
testcase_54 | AC | 217 ms
214,836 KB |
testcase_55 | AC | 151 ms
150,516 KB |
testcase_56 | AC | 248 ms
246,972 KB |
testcase_57 | AC | 170 ms
160,348 KB |
testcase_58 | AC | 251 ms
252,336 KB |
testcase_59 | AC | 130 ms
128,632 KB |
testcase_60 | AC | 960 ms
265,568 KB |
testcase_61 | AC | 626 ms
208,860 KB |
testcase_62 | AC | 1,455 ms
332,308 KB |
testcase_63 | AC | 1,701 ms
343,044 KB |
testcase_64 | AC | 369 ms
151,864 KB |
testcase_65 | AC | 731 ms
232,960 KB |
testcase_66 | AC | 242 ms
169,684 KB |
testcase_67 | AC | 335 ms
205,036 KB |
testcase_68 | AC | 390 ms
210,868 KB |
testcase_69 | AC | 278 ms
174,632 KB |
testcase_70 | AC | 447 ms
161,972 KB |
testcase_71 | AC | 336 ms
116,092 KB |
testcase_72 | AC | 3,703 ms
450,512 KB |
testcase_73 | AC | 1,133 ms
450,484 KB |
testcase_74 | AC | 123 ms
118,204 KB |
testcase_75 | AC | 2 ms
4,348 KB |
testcase_76 | AC | 1 ms
4,348 KB |
testcase_77 | AC | 3 ms
4,352 KB |
testcase_78 | AC | 3 ms
4,352 KB |
testcase_79 | AC | 19 ms
4,608 KB |
testcase_80 | AC | 437 ms
450,552 KB |
testcase_81 | AC | 432 ms
441,840 KB |
testcase_82 | AC | 413 ms
441,780 KB |
testcase_83 | AC | 316 ms
313,356 KB |
testcase_84 | AC | 307 ms
313,200 KB |
testcase_85 | AC | 349 ms
359,220 KB |
testcase_86 | AC | 352 ms
358,612 KB |
testcase_87 | AC | 396 ms
407,684 KB |
ソースコード
#line 2 "cpplib/util/template.hpp" #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #include<bits/stdc++.h> using namespace std; struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__; typedef long long lint; #define INF (1LL<<60) #define IINF (1<<30) #define EPS (1e-10) #define endl ('\n') typedef vector<lint> vec; typedef vector<vector<lint>> mat; typedef vector<vector<vector<lint>>> mat3; typedef vector<string> svec; typedef vector<vector<string>> smat; template<typename T>using V=vector<T>; template<typename T>using VV=V<V<T>>; template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;} template<typename T>inline void output2(T t){for(auto i:t)output(i);} template<typename T>inline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<<i;f=1;}cerr<<endl;} template<typename T>inline void debug2(T t){for(auto i:t)debug(i);} #define loop(n) for(long long _=0;_<(long long)(n);++_) #define _overload4(_1,_2,_3,_4,name,...) name #define __rep(i,a) repi(i,0,a,1) #define _rep(i,a,b) repi(i,a,b,1) #define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c) #define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__) #define _overload3_rev(_1,_2,_3,name,...) name #define _rep_rev(i,a) repi_rev(i,0,a) #define repi_rev(i,a,b) for(long long i=(long long)(b)-1;i>=(long long)(a);--i) #define rrep(...) _overload3_rev(__VA_ARGS__,repi_rev,_rep_rev)(__VA_ARGS__) // #define rep(i,...) for(auto i:range(__VA_ARGS__)) // #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__))) // #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i) // #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i) // #define irep(i) for(lint i=0;;++i) // inline vector<long long> range(long long n){if(n<=0)return vector<long long>();vector<long long>v(n);iota(v.begin(),v.end(),0LL);return v;} // inline vector<long long> range(long long a,long long b){if(b<=a)return vector<long long>();vector<long long>v(b-a);iota(v.begin(),v.end(),a);return v;} // inline vector<long long> range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector<long long>();vector<long long>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;} // template<typename T>inline T reversed(T v){reverse(v.begin(),v.end());return v;} #define all(n) begin(n),end(n) template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;} template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;} const string ds="DRUL"; const vector<lint> dx={1,0,-1,0,1,1,-1,-1}; const vector<lint> dy={0,1,0,-1,1,-1,1,-1}; #define SUM(v) accumulate(all(v),0LL) #if __cplusplus>=201703L template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));} #endif #define extrep(v,...) for(auto v:__MAKE_MAT__({__VA_ARGS__})) #define bit(n,a) ((n>>a)&1) vector<vector<long long>> __MAKE_MAT__(vector<long long> v){if(v.empty())return vector<vector<long long>>(1,vector<long long>());long long n=v.back();v.pop_back();vector<vector<long long>> ret;vector<vector<long long>> tmp=__MAKE_MAT__(v);for(auto e:tmp)for(long long i=0;i<n;++i){ret.push_back(e);ret.back().push_back(i);}return ret;} using graph=vector<vector<int>>; template<typename T>using graph_w=vector<vector<pair<int,T>>>; template<typename T,typename E>ostream& operator<<(ostream& out,pair<T,E>v){out<<"("<<v.first<<","<<v.second<<")";return out;} #if __cplusplus>=201703L constexpr inline long long powll(long long a,long long b){long long res=1;while(b--)res*=a;return res;} #endif template<typename T,typename E>pair<T,E>& operator+=(pair<T,E>&s,const pair<T,E>&t){s.first+=t.first;s.second+=t.second;return s;} template<typename T,typename E>pair<T,E>& operator-=(pair<T,E>&s,const pair<T,E>&t){s.first-=t.first;s.second-=t.second;return s;} template<typename T,typename E>pair<T,E> operator+(const pair<T,E>&s,const pair<T,E>&t){auto res=s;return res+=t;} template<typename T,typename E>pair<T,E> operator-(const pair<T,E>&s,const pair<T,E>&t){auto res=s;return res-=t;} // 128*1024*1024 #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024)); #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_); #line 2 "main.cpp" //copied by https://judge.yosupo.jp/submission/4713 using ll=long long; vector<int> hungarian(const vector<vector<ll>>& a){ int n=a.size(),p,q; vector<ll> fx(n,INF),fy(n,0); vector<int> x(n,-1),y(n,-1); rep(i,0,n)rep(j,0,n)chmax(fx[i],a[i][j]); for(int i=0;i<n;){ vector<ll> t(n,-1),s(n+1,i); for(p=q=0;p<=q&&x[i]<0;p++){ for(int k=s[p],j=0;j<n&&x[i]<0;j++){ if(fx[k]+fy[j]==a[k][j]&&t[j]<0){ s[++q]=y[j],t[j]=k; if(s[q]<0)for(p=j;p>=0;j=p){y[j]=k=t[j],p=x[k],x[k]=j;} } } } if(x[i]<0){ ll d=INF; rep(k,0,q+1)rep(j,0,n)if(t[j]<0)chmin(d,fx[s[k]]+fy[j]-a[s[k]][j]); rep(j,0,n)fy[j]+=(t[j]<0?0:d); rep(k,0,q+1)fx[s[k]]-=d; } else i++; } return x; } int main(){ lint k; cin>>k; vector<vector<int>> a(k); rep(i,k-1){ lint s,t; cin>>s>>t; s--;t--; a[s].emplace_back(t); a[t].emplace_back(s); } lint n; cin>>n; vector<vector<int>> b(n); rep(i,n-1){ lint s,t; cin>>s>>t; s--;t--; b[s].emplace_back(t); b[t].emplace_back(s); } auto dp=make_vector<int>(-2,k+1,k+1,n+1,n+1); vector<lint>p(n),sz(n,1); auto dfs2=[&](auto dfs2,lint now,lint pp)->void{ p[now]=pp; for(auto e:b[now]){ if(e==pp)continue; dfs2(dfs2,e,now); sz[now]+=sz[e]; } }; dfs2(dfs2,0,-1); // auto size=[&](int bnow,int bp){ // if(bp==-1)return n; // return p[bnow]==bp?sz[bnow]:n-sz[bp]; // }; auto dfs=[&](auto dfs,lint anow,lint ap,lint bnow,lint bp)->lint{ lint res2=-1; if(dp[anow][ap!=-1?ap:k][bnow][bp!=-1?bp:n]!=-2){ return dp[anow][ap!=-1?ap:k][bnow][bp!=-1?bp:n]; } const int asz=(int)a[anow].size()-(ap!=-1); const int bsz=(int)b[bnow].size()-(bp!=-1); // if(asz>bsz)return dp[anow][ap!=-1?ap:k][bnow][bp!=-1?bp:n]=-1; if(ap!=-1&&bp!=-1){ // int sz=size(bnow,bp)-1; int bcnt=0; for(auto bto:b[bnow]){ if(bp==bto)continue; auto res=dfs(dfs,anow,ap,bto,bnow); if(res==-1){ bcnt++; continue; } chmax(res2,res+1); bcnt++; } } vector<vector<ll>>v(bsz,vector<ll>(bsz)); int acnt=0; // rep(i,asz)mf.add_edge(asz+bsz,i,1,0); // rep(i,bsz)mf.add_edge(i+asz,asz+bsz+1,1,0); // { // int bcnt=0; // for(auto bto:b[bnow]){ // if(bp==bto)continue; // // cerr<<bto<<" "<<bnow<<endl; // mf.add_edge(asz+bsz,bcnt+asz,1,-size(bto,bnow)); // bcnt++; // } // } if(asz<=bsz){ for(auto ato:a[anow]){ if(ap==ato)continue; int bcnt=0; for(auto bto:b[bnow]){ if(bp==bto)continue; auto res=dfs(dfs,ato,anow,bto,bnow); if(res!=-1)v[acnt][bcnt]=res; else v[acnt][bcnt]=-IINF; bcnt++; } // assert(bsz==bcnt); acnt++; } // assert(asz==acnt); auto res=hungarian(v); bool ok=1; lint cost=0; rep(i,asz){ if(v[i][res[i]]!=-IINF)cost+=v[i][res[i]]; else{ ok=0; break; } } if(ok)chmax(res2,cost); } return dp[anow][ap!=-1?ap:k][bnow][bp!=-1?bp:n]=res2; }; lint ans=-1; rep(i,k)rep(j,n){ auto res=dfs(dfs,i,-1,j,-1); if(res==-1)continue; chmax(ans,res); } cerr<<ans<<endl; if(ans==-1)cout<<-1<<endl; else cout<<k-1+ans<<endl; }