結果
問題 | No.1789 Tree Growing |
ユーザー | hotman78 |
提出日時 | 2021-12-18 11:49:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 8,814 bytes |
コンパイル時間 | 4,123 ms |
コンパイル使用メモリ | 241,496 KB |
実行使用メモリ | 817,920 KB |
最終ジャッジ日時 | 2024-09-15 12:05:59 |
合計ジャッジ時間 | 18,054 ms |
ジャッジサーバーID (参考情報) |
judge3 / 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 | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 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 | 4 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 | 9 ms
8,064 KB |
testcase_16 | AC | 3 ms
5,376 KB |
testcase_17 | AC | 10 ms
9,856 KB |
testcase_18 | AC | 7 ms
7,424 KB |
testcase_19 | AC | 5 ms
5,376 KB |
testcase_20 | AC | 13 ms
12,800 KB |
testcase_21 | AC | 44 ms
44,672 KB |
testcase_22 | AC | 18 ms
17,280 KB |
testcase_23 | AC | 22 ms
21,376 KB |
testcase_24 | AC | 23 ms
22,784 KB |
testcase_25 | AC | 150 ms
160,000 KB |
testcase_26 | AC | 146 ms
152,448 KB |
testcase_27 | AC | 131 ms
139,008 KB |
testcase_28 | AC | 125 ms
131,328 KB |
testcase_29 | AC | 143 ms
153,984 KB |
testcase_30 | AC | 270 ms
286,848 KB |
testcase_31 | AC | 273 ms
296,704 KB |
testcase_32 | AC | 279 ms
304,640 KB |
testcase_33 | AC | 257 ms
282,496 KB |
testcase_34 | AC | 385 ms
422,784 KB |
testcase_35 | AC | 200 ms
214,656 KB |
testcase_36 | AC | 381 ms
388,352 KB |
testcase_37 | AC | 398 ms
434,560 KB |
testcase_38 | AC | 355 ms
384,256 KB |
testcase_39 | AC | 256 ms
265,216 KB |
testcase_40 | AC | 340 ms
366,208 KB |
testcase_41 | AC | 433 ms
477,056 KB |
testcase_42 | MLE | - |
testcase_43 | MLE | - |
testcase_44 | MLE | - |
testcase_45 | AC | 489 ms
482,048 KB |
testcase_46 | MLE | - |
testcase_47 | MLE | - |
testcase_48 | MLE | - |
testcase_49 | MLE | - |
testcase_50 | MLE | - |
testcase_51 | MLE | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
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 | -- | - |
ソースコード
#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; mat 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; mat 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<lint>(-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; }