結果
問題 | No.1789 Tree Growing |
ユーザー | hotman78 |
提出日時 | 2021-12-18 03:00:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 14,561 bytes |
コンパイル時間 | 4,999 ms |
コンパイル使用メモリ | 273,324 KB |
実行使用メモリ | 814,592 KB |
最終ジャッジ日時 | 2024-09-15 04:23:20 |
合計ジャッジ時間 | 20,729 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,376 KB |
testcase_03 | AC | 4 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 4 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 5 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | WA | - |
testcase_14 | AC | 4 ms
5,376 KB |
testcase_15 | AC | 16 ms
9,600 KB |
testcase_16 | AC | 5 ms
5,376 KB |
testcase_17 | WA | - |
testcase_18 | AC | 17 ms
9,216 KB |
testcase_19 | WA | - |
testcase_20 | AC | 25 ms
14,976 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 39 ms
25,600 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 186 ms
161,024 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | AC | 480 ms
447,104 KB |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | MLE | - |
testcase_43 | MLE | - |
testcase_44 | MLE | - |
testcase_45 | AC | 501 ms
495,104 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 6 "cpplib/graph_tree/push_relabel.hpp" /** * @brief 最大流(push_relabel法O(V^2√E)) */ template<typename T> class push_relabel{ int n; T f=0; using i64=long long; struct edge{ int from,to,rev; T flow,cap; }; std::vector<i64>h,d; std::vector<std::vector<edge*>>g; std::vector<size_t>seen; std::priority_queue<std::pair<i64,int>,std::vector<std::pair<i64,int>>,std::greater<std::pair<i64,int>>>que; public: push_relabel(int n):n(n){ h.resize(n,0); d.resize(n,0); g.resize(n); seen.resize(n,0); } void add_edge(int u,int v,T cap){ g[u].emplace_back(new edge{u,v,(int)g[v].size(),0,cap}); g[v].emplace_back(new edge{v,u,(int)g[u].size()-1,0,0}); } void push(edge* e){ int u=e->from,v=e->to; i64 df=std::min<i64>(d[u],e->cap-e->flow); e->flow+=df; g[v][e->rev]->flow-=df; d[u]-=df; d[v]+=df; if(d[v]>0)que.emplace(h[v],v); if(d[u]>0)que.emplace(h[u],u); } void relabel(int u){ i64 mn=n*2; for(edge* e:g[u]){ if(e->cap-e->flow>0){ mn=std::min(mn,h[e->to]); } } h[u]=1+mn; que.emplace(h[u],u); } void discharge(int u){ while(d[u]>0){ if(seen[u]<g[u].size()){ edge* e=g[u][seen[u]]; if(e->cap-e->flow>0 && h[u]==h[e->to]+1){ push(e); }else{ seen[u]+=1; } }else{ relabel(u); seen[u]=0; } } } T run(int s,int t){ h[s]=n; for(auto e:g[s]){ d[s]+=e->cap; push(e); } while(!que.empty()){ int u=que.top().second; que.pop(); if(u==s||u==t)continue; discharge(u); } for(auto e:g[s])f+=e->flow; return f; } }; #line 6 "cpplib/graph_tree/min_cost_flow.hpp" /** * @brief 最小費用流(CostScaling) */ //Resは答えがlong longの最大値を超える時用 template<typename T,typename Res=T> struct min_cost_flow{ int v; Res ans=0; struct edge{ int to; T cap,cost,st; int rev; bool is_rev,edge_rev; int id; }; push_relabel<T> mf; std::vector<T>p; std::vector<vector<edge*>>e;//辺のキャパシティ std::vector<T>d;//頂点のキャパシティ std::queue<int>active; std::vector<tuple<int,int,T,T,T>>edges; T eps=1; int idx=0; std::vector<T>res; min_cost_flow(int v):v(v),mf(v+2),p(v,0),e(v),d(v,0){} void add_edge(int from,int to,T mn,T mx,T cost){ edges.emplace_back(from,to,mn,mx,cost); res.push_back(0); if(from==to){ if(cost<0)res[idx++]=mx,ans+=mx*cost; else res[idx++]=mn,ans+=mn*cost; return; } if(cost>=0){ e[from].push_back(new edge{to,mx-mn,cost*v,mn,(int)e[to].size(),0,0,idx}); e[to].push_back(new edge{from,0,-cost*v,mn,(int)e[from].size()-1,1,0,idx++}); ans+=mn*cost; d[from]-=mn;d[to]+=mn; eps=max(eps,cost*v*v); mf.add_edge(from,to,mx-mn); } else{ e[to].push_back(new edge{from,mx-mn,-cost*v,mx,(int)e[from].size(),0,1,idx}); e[from].push_back(new edge{to,0,cost*v,mx,(int)e[to].size()-1,1,1,idx++}); ans+=mx*cost; d[from]-=mx;d[to]+=mx; eps=max(eps,-cost*v*v); mf.add_edge(to,from,mx-mn); } } void add_edge(int from,int to,T cap,T cost){ add_edge(from,to,T(),cap,cost); } Res flow(){ for(;eps;eps>>=1){ for(int i=0;i<v;i++){ for(auto ed:e[i]){ if(ed->is_rev)continue; if(ed->cost-p[i]+p[ed->to]<0){ T f=ed->cap; ed->cap-=f; d[i]-=f; d[ed->to]+=f; e[ed->to][ed->rev]->cap+=f; } if(ed->cost-p[i]+p[ed->to]>0){ T f=-e[ed->to][ed->rev]->cap; ed->cap-=f; d[i]-=f; d[ed->to]+=f; e[ed->to][ed->rev]->cap+=f; } } } for(int i=0;i<v;i++)if(d[i]>0){active.emplace(i);} while(!active.empty()){ int node=active.front(); active.pop(); if(d[node]<=0)continue; bool b=0; for(auto ed:e[node]){ if(!d[node])break; if(-eps<=ed->cost-p[node]+p[ed->to]&&ed->cost-p[node]+p[ed->to]<0){ auto f=std::min(d[node],ed->cap); if(!f)continue; ed->cap-=f; d[node]-=f; d[ed->to]+=f; e[ed->to][ed->rev]->cap+=f; if(d[ed->to]>0)active.emplace(ed->to); b=1; } } if(d[node]>0)active.emplace(node); if(!b)p[node]+=eps; } } for(int i=0;i<v;i++)for(auto ed:e[i]){ if(ed->is_rev)continue; ans+=e[ed->to][ed->rev]->cap*(ed->cost/v); } return ans; } Res run(int s,int t,T f=-1,bool check=0){ d[s]+=f; d[t]-=f; if(check){ // debug(d); int cnt=0; for(int i=0;i<v;++i){ if(d[i]>0){ mf.add_edge(v,i,d[i]); cnt+=d[i]; }else if(d[i]<0){ mf.add_edge(i,v+1,-d[i]); } } lint t=mf.run(v,v+1); if(t<cnt){ return IINF; } } return flow(); } Res run(std::vector<T>b){ for(int i=0;i<v;++i)d[i]+=b[i]; return flow(); } std::vector<T> flow_result(){ for(int i=0;i<v;i++)for(auto ed:e[i]){ if(ed->is_rev)continue; res[ed->id]=ed->st+e[ed->to][ed->rev]->cap*(ed->edge_rev?-1:1); } return res; } //flow_resultを渡す std::vector<T>potential(const std::vector<T>& f){ std::vector<T>p(v,0); std::vector<tuple<int,int,T>>g; int idx=0; for(auto [from,to,mn,mx,cost]:edges){ if(mn<f[idx])g.emplace_back(to,from,-cost); if(f[idx]<mx)g.emplace_back(from,to,cost); idx++; } for(int i=0;i<v;++i)for(auto [s,t,c]:g){ p[t]=std::min(p[t],p[s]+c); } return p; } }; #line 3 "main.cpp" 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; { // 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++; } } min_cost_flow<int>mf(asz+bsz+2); 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++; // } // } 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)mf.add_edge(acnt,asz+bcnt,1,-res); bcnt++; } // assert(bsz==bcnt); acnt++; } // assert(asz==acnt); auto cost=mf.run(asz+bsz,asz+bsz+1,asz,1); if(cost!=IINF)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); } if(ans==-1)cout<<-1<<endl; else cout<<k-1+ans<<endl; }