結果

問題 No.1789 Tree Growing
ユーザー chocoruskchocorusk
提出日時 2021-12-10 02:53:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 387 ms / 5,000 ms
コード長 8,233 bytes
コンパイル時間 9,680 ms
コンパイル使用メモリ 325,992 KB
実行使用メモリ 5,892 KB
最終ジャッジ日時 2023-10-13 02:48:35
合計ジャッジ時間 14,301 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 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,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 8 ms
4,352 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 7 ms
4,352 KB
testcase_28 AC 7 ms
4,352 KB
testcase_29 AC 7 ms
4,348 KB
testcase_30 AC 10 ms
4,348 KB
testcase_31 AC 9 ms
4,352 KB
testcase_32 AC 10 ms
4,352 KB
testcase_33 AC 10 ms
4,356 KB
testcase_34 AC 11 ms
4,352 KB
testcase_35 AC 9 ms
4,352 KB
testcase_36 AC 10 ms
4,348 KB
testcase_37 AC 11 ms
4,348 KB
testcase_38 AC 11 ms
4,348 KB
testcase_39 AC 9 ms
4,348 KB
testcase_40 AC 10 ms
4,348 KB
testcase_41 AC 12 ms
4,348 KB
testcase_42 AC 12 ms
4,348 KB
testcase_43 AC 13 ms
4,348 KB
testcase_44 AC 12 ms
4,352 KB
testcase_45 AC 11 ms
4,348 KB
testcase_46 AC 14 ms
4,348 KB
testcase_47 AC 15 ms
4,352 KB
testcase_48 AC 14 ms
4,348 KB
testcase_49 AC 14 ms
4,352 KB
testcase_50 AC 14 ms
4,352 KB
testcase_51 AC 15 ms
4,352 KB
testcase_52 AC 14 ms
4,352 KB
testcase_53 AC 14 ms
4,352 KB
testcase_54 AC 12 ms
4,348 KB
testcase_55 AC 10 ms
4,348 KB
testcase_56 AC 14 ms
4,348 KB
testcase_57 AC 10 ms
4,352 KB
testcase_58 AC 12 ms
4,352 KB
testcase_59 AC 10 ms
4,348 KB
testcase_60 AC 95 ms
4,816 KB
testcase_61 AC 58 ms
4,412 KB
testcase_62 AC 147 ms
4,872 KB
testcase_63 AC 181 ms
4,840 KB
testcase_64 AC 39 ms
4,348 KB
testcase_65 AC 76 ms
4,396 KB
testcase_66 AC 21 ms
4,348 KB
testcase_67 AC 36 ms
4,352 KB
testcase_68 AC 39 ms
4,348 KB
testcase_69 AC 23 ms
4,348 KB
testcase_70 AC 30 ms
4,352 KB
testcase_71 AC 20 ms
4,352 KB
testcase_72 AC 387 ms
5,892 KB
testcase_73 AC 125 ms
4,352 KB
testcase_74 AC 9 ms
4,348 KB
testcase_75 AC 2 ms
4,348 KB
testcase_76 AC 1 ms
4,352 KB
testcase_77 AC 2 ms
4,352 KB
testcase_78 AC 2 ms
4,352 KB
testcase_79 AC 2 ms
4,348 KB
testcase_80 AC 16 ms
4,352 KB
testcase_81 AC 15 ms
4,348 KB
testcase_82 AC 14 ms
4,348 KB
testcase_83 AC 12 ms
4,352 KB
testcase_84 AC 12 ms
4,348 KB
testcase_85 AC 13 ms
4,348 KB
testcase_86 AC 13 ms
4,348 KB
testcase_87 AC 14 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#include "testlib.h"
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct unionfind{
    int cnt;
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1), cnt(n){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        cnt--;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
const int MAXN=100;
const int MAXK=100;
const ll INF=1e9;
int n, k;
vector<int> gn[MAXN], gk[MAXK];
int vn[MAXN], vk[MAXK], ordn[MAXN], ordk[MAXK];
ll dp[MAXN][MAXK], dp2[MAXN][MAXK];
int a[MAXK], b[MAXK], c[MAXN], d[MAXN];

int main(int argc, char* argv[]){
    registerValidation(argc, argv);
    k=inf.readInt(2, MAXK);
    inf.readEoln();
    for(int i=0; i<k-1; i++){
        a[i]=inf.readInt(1, k);
        inf.readSpace();
        b[i]=inf.readInt(a[i]+1, k);
        inf.readEoln();
    }
    n=inf.readInt(k, MAXN);
    inf.readEoln();
    for(int i=0; i<n-1; i++){
        c[i]=inf.readInt(1, n);
        inf.readSpace();
        d[i]=inf.readInt(c[i]+1, n);
        inf.readEoln();
    }
    inf.readEof();
    unionfind ufk(k), ufn(n);
    for(int i=0; i<k-1; i++){
        ensure(!ufk.same(a[i]-1, b[i]-1));
        ufk.unite(a[i]-1, b[i]-1);
    }
    for(int i=0; i<n-1; i++){
        ensure(!ufn.same(c[i]-1, d[i]-1));
        ufn.unite(c[i]-1, d[i]-1);
    }
    
	for(int i=0; i<k-1; i++){
        a[i]--; b[i]--;
        gk[a[i]].push_back(b[i]);
        gk[b[i]].push_back(a[i]);
    }
    for(int i=0; i<n-1; i++){
        c[i]--; d[i]--;
        gn[c[i]].push_back(d[i]);
        gn[d[i]].push_back(c[i]);
    }
    {
        queue<int> que;
        que.push(0);
        bool used[MAXK]={};
        used[0]=1;
        int t=0;
        while(!que.empty()){
            int x=que.front(); que.pop();
            ordk[x]=t;
            vk[t++]=x;
            for(auto y:gk[x]){
                if(used[y]) continue;
                que.push(y);
                used[y]=1;
            }
        }
    }
    {
        queue<int> que;
        que.push(0);
        bool used[MAXN]={};
        used[0]=1;
        int t=0;
        while(!que.empty()){
            int x=que.front(); que.pop();
            ordn[x]=t;
            vn[t++]=x;
            for(auto y:gn[x]){
                if(used[y]) continue;
                que.push(y);
                used[y]=1;
            }
        }
    }
    for(int i=0; i<n; i++) for(int j=0; j<k; j++) dp[i][j]=-INF, dp2[i][j]=-INF;
    for(int i=n-1; i>=0; i--){
        for(int j=k-1; j>=0; j--){
            int x=vn[i], y=vk[j];
            for(auto z:gn[x]){
                if(ordn[z]<ordn[x]) continue;
                dp[i][j]=max(dp[i][j], dp[ordn[z]][j]+1);
            }
            int n1=gn[x].size(), k1=gk[y].size();
            if(i>0) n1--;
            if(j>0) k1--;
            if(k1>n1) continue;
            mcf_graph<int, ll> g(k1+n1+2);
            int s=k1+n1, t=s+1;
            for(int l=0; l<k1; l++){
                g.add_edge(s, l, 1, 0);
            }
            for(int l=0; l<n1; l++){
                g.add_edge(l+k1, t, 1, 0);
            }
            int i1=0;
            for(auto z:gn[x]){
                if(ordn[z]<ordn[x]) continue;
                int j1=0;
                for(auto w:gk[y]){
                    if(ordk[w]<ordk[y]) continue;
                    if(dp[ordn[z]][ordk[w]]>=0){
                        g.add_edge(j1, i1+k1, 1, INF-dp[ordn[z]][ordk[w]]);
                    }
                    j1++;
                }
                i1++;
            }
            auto res=g.flow(s, t);
            if(res.first==k1){
                dp[i][j]=max(dp[i][j], INF*k1-res.second);
            }
        }
    }
    ll ans=-INF;
    for(int i=0; i<n; i++){
        for(int j=0; j<k; j++){
            int x=vn[i], y=vk[j];
            set<pair<ll, int>> st;
            st.insert({-INF, -1});
            for(auto z:gn[x]){
                if(ordn[z]<ordn[x]){
                    st.insert({dp2[i][j]+1, z});
                }else{
                    st.insert({dp[ordn[z]][j]+1, z});
                }
            }
            for(auto z:gn[x]){
                if(ordn[z]<ordn[x]) continue;
                st.erase({dp[ordn[z]][j]+1, z});
                dp2[ordn[z]][j]=max(dp2[ordn[z]][j], st.rbegin()->first);
                st.insert({dp[ordn[z]][j]+1, z});
            }
            int n1=gn[x].size(), k1=gk[y].size();
            if(j>0) k1--;
            if(k1>n1) continue;
            if(k1==0){
                for(auto z:gn[x]){
                    if(ordn[z]<ordn[x]) continue;
                    dp2[ordn[z]][j]=max(dp2[ordn[z]][j], 0ll);
                }
                continue;
            }
            mcf_graph<int, ll> g0(k1+n1+2);
            int s=k1+n1, t=s+1;
            for(int l=0; l<k1; l++){
                g0.add_edge(s, l, 1, 0);
            }
            for(int l=0; l<n1; l++){
                g0.add_edge(l+k1, t, 1, 0);
            }
            vector<bool> myon(n1);
            int i1=0;
            for(auto z:gn[x]){
                int j1=0;
                for(auto w:gk[y]){
                    if(ordk[w]<ordk[y]) continue;
                    if(ordn[z]<ordn[x]){
                        if(dp2[i][ordk[w]]>=0) g0.add_edge(j1, i1+k1, 1, INF-dp2[i][ordk[w]]);
                    }else{
                        if(dp[ordn[z]][ordk[w]]>=0) g0.add_edge(j1, i1+k1, 1, INF-dp[ordn[z]][ordk[w]]);
                    }
                    j1++;
                }
                i1++;
            }
            auto res=g0.flow(s, t);
            ll mx=-INF;
            if(res.first==k1) mx=max(mx, INF*k1-res.second);
            if(j==0){
                if(res.first==k1) ans=max(ans, mx);
            }
            auto edges=g0.edges();
            for(auto e:edges){
                if(e.from<k1 && (e.to>=k1 && e.to<k1+n1) && e.flow==1){
                    myon[e.to-k1]=1;
                }
            }
            for(int l=0; l<n1; l++){
                int z0=gn[x][l];
                if(ordn[z0]<ordn[x]) continue;
                if(!myon[l]){
                    dp2[ordn[z0]][j]=max(dp2[ordn[z0]][j], mx);
                    continue;
                }

                mcf_graph<int, ll> g(k1+n1+2);
                int s=k1+n1, t=s+1;
                for(int m=0; m<k1; m++){
                    g.add_edge(s, m, 1, 0);
                }
                for(int m=0; m<n1; m++){
                    if(l!=m) g.add_edge(m+k1, t, 1, 0);
                }
                int i1=0;
                for(auto z:gn[x]){
                    int j1=0;
                    for(auto w:gk[y]){
                        if(ordk[w]<ordk[y]) continue;
                        if(ordn[z]<ordn[x]){
                            if(dp2[i][ordk[w]]>=0) g.add_edge(j1, i1+k1, 1, INF-dp2[i][ordk[w]]);
                        }else{
                            if(dp[ordn[z]][ordk[w]]>=0) g.add_edge(j1, i1+k1, 1, INF-dp[ordn[z]][ordk[w]]);
                        }
                        j1++;
                    }
                    i1++;
                }
                auto res=g.flow(s, t);
                if(res.first==k1){
                    dp2[ordn[z0]][j]=max(dp2[ordn[z0]][j], INF*k1-res.second);
                }
            }
        }
    }
    if(ans<0){
        cout<<-1<<endl;
        return 0;
    }
	ans+=k-1;
    cout<<ans<<endl;
    return 0;
}
0