結果

問題 No.1789 Tree Growing
ユーザー chocoruskchocorusk
提出日時 2021-12-18 00:33:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 596 ms / 5,000 ms
コード長 2,767 bytes
コンパイル時間 6,310 ms
コンパイル使用メモリ 280,588 KB
実行使用メモリ 4,464 KB
最終ジャッジ日時 2023-10-13 04:03:26
合計ジャッジ時間 26,807 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,372 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,356 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 11 ms
4,352 KB
testcase_16 AC 3 ms
4,372 KB
testcase_17 AC 16 ms
4,352 KB
testcase_18 AC 13 ms
4,352 KB
testcase_19 AC 7 ms
4,348 KB
testcase_20 AC 17 ms
4,352 KB
testcase_21 AC 85 ms
4,352 KB
testcase_22 AC 41 ms
4,352 KB
testcase_23 AC 52 ms
4,352 KB
testcase_24 AC 45 ms
4,352 KB
testcase_25 AC 170 ms
4,348 KB
testcase_26 AC 157 ms
4,348 KB
testcase_27 AC 148 ms
4,348 KB
testcase_28 AC 153 ms
4,348 KB
testcase_29 AC 150 ms
4,356 KB
testcase_30 AC 228 ms
4,352 KB
testcase_31 AC 230 ms
4,348 KB
testcase_32 AC 229 ms
4,352 KB
testcase_33 AC 211 ms
4,352 KB
testcase_34 AC 271 ms
4,352 KB
testcase_35 AC 184 ms
4,352 KB
testcase_36 AC 258 ms
4,352 KB
testcase_37 AC 275 ms
4,352 KB
testcase_38 AC 263 ms
4,348 KB
testcase_39 AC 210 ms
4,352 KB
testcase_40 AC 255 ms
4,352 KB
testcase_41 AC 270 ms
4,348 KB
testcase_42 AC 308 ms
4,348 KB
testcase_43 AC 334 ms
4,348 KB
testcase_44 AC 326 ms
4,348 KB
testcase_45 AC 292 ms
4,352 KB
testcase_46 AC 344 ms
4,352 KB
testcase_47 AC 347 ms
4,352 KB
testcase_48 AC 357 ms
4,348 KB
testcase_49 AC 348 ms
4,348 KB
testcase_50 AC 368 ms
4,348 KB
testcase_51 AC 406 ms
4,356 KB
testcase_52 AC 342 ms
4,352 KB
testcase_53 AC 360 ms
4,352 KB
testcase_54 AC 346 ms
4,348 KB
testcase_55 AC 272 ms
4,352 KB
testcase_56 AC 354 ms
4,352 KB
testcase_57 AC 226 ms
4,352 KB
testcase_58 AC 349 ms
4,352 KB
testcase_59 AC 192 ms
4,352 KB
testcase_60 AC 347 ms
4,348 KB
testcase_61 AC 292 ms
4,376 KB
testcase_62 AC 411 ms
4,352 KB
testcase_63 AC 430 ms
4,352 KB
testcase_64 AC 223 ms
4,368 KB
testcase_65 AC 313 ms
4,356 KB
testcase_66 AC 220 ms
4,348 KB
testcase_67 AC 257 ms
4,352 KB
testcase_68 AC 286 ms
4,348 KB
testcase_69 AC 241 ms
4,352 KB
testcase_70 AC 229 ms
4,352 KB
testcase_71 AC 186 ms
4,348 KB
testcase_72 AC 596 ms
4,464 KB
testcase_73 AC 500 ms
4,348 KB
testcase_74 AC 187 ms
4,348 KB
testcase_75 AC 1 ms
4,352 KB
testcase_76 AC 2 ms
4,352 KB
testcase_77 AC 11 ms
4,352 KB
testcase_78 AC 11 ms
4,348 KB
testcase_79 AC 11 ms
4,352 KB
testcase_80 AC 407 ms
4,348 KB
testcase_81 AC 392 ms
4,352 KB
testcase_82 AC 373 ms
4,348 KB
testcase_83 AC 314 ms
4,352 KB
testcase_84 AC 305 ms
4,352 KB
testcase_85 AC 329 ms
4,352 KB
testcase_86 AC 336 ms
4,348 KB
testcase_87 AC 345 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
const int MAXN=100;
const int MAXK=100;
const int INF=1e6;
int n, k, vn[MAXN], vk[MAXK], ordn[MAXN], ordk[MAXK];;
vector<int> gn[MAXN], gk[MAXK];
int main()
{
    cin>>k;
	for(int i=0; i<k-1; i++){
        int a, b;
        cin>>a>>b;
        a--; b--;
        gk[a].push_back(b);
        gk[b].push_back(a);
    }
	cin>>n;
    for(int i=0; i<n-1; i++){
        int a, b;
        cin>>a>>b;
        a--; b--;
        gn[a].push_back(b);
        gn[b].push_back(a);
    }
    {
        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;
            }
        }
    }
    int ans=-INF;
    for(int r=0; r<n; r++){
        queue<int> que;
        que.push(r);
        bool used[MAXN]={};
        used[r]=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;
            }
        }
        int dp[MAXN][MAXK];
        for(int i=0; i<n; i++) for(int j=0; j<k; j++) dp[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[vn[i]].size(), k1=gk[vk[j]].size();
                if(i>0) n1--;
                if(j>0) k1--;
                if(k1>n1) continue;
				mcf_graph<int, int> 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;
						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){
					if(i==0 && j==0) ans=max(ans, INF*k1-res.second);
					dp[i][j]=max(dp[i][j], INF*k1-res.second);
                }
            }
        }
    }
	if(ans<0){
		cout<<-1<<endl;
		return 0;
	}
	ans+=k-1;
    cout<<ans<<endl;
    return 0;
}
0