結果

問題 No.1789 Tree Growing
ユーザー chocorusk
提出日時 2021-12-18 19:28:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 356 ms / 5,000 ms
コード長 3,502 bytes
コンパイル時間 5,847 ms
コンパイル使用メモリ 284,368 KB
最終ジャッジ日時 2025-01-27 03:21:49
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

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;
            }
        }
    }
    string ts[MAXK];
    int tk[MAXK];
    vector<string> vss(k);
    for(int i=k-1; i>=0; i--){
        int x=vk[i];
        vector<string> vs;
        for(auto y:gk[x]){
            if(ordk[y]<ordk[x]) continue;
            vs.push_back("("+ts[ordk[y]]+")");
        }
        sort(vs.begin(), vs.end());
        ts[i]="";
        for(auto s:vs) ts[i]+=s;
        vss[i]=ts[i];
    }
    sort(vss.begin(), vss.end());
    vss.erase(unique(vss.begin(), vss.end()), vss.end());
    for(int i=0; i<k; i++){
        tk[i]=lower_bound(vss.begin(), vss.end(), ts[i])-vss.begin();
    }

    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[MAXK][MAXN];
        bool ok[MAXK][MAXN]={};
        for(int j=0; j<vss.size(); j++) for(int i=0; i<n; i++) dp[j][i]=-INF;

        for(int i=n-1; i>=0; i--){
            for(int j=k-1; j>=0; j--){
                if(!(i==0 && j==0) && ok[tk[j]][i]) continue;
                int x=vn[i], y=vk[j], j1=tk[j];
                ok[j1][i]=1;
                for(auto z:gn[x]){
                    if(ordn[z]<ordn[x]) continue;
                    dp[j1][i]=max(dp[j1][i], dp[j1][ordn[z]]+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[tk[ordk[w]]][ordn[z]]);
						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[j1][i]=max(dp[j1][i], INF*k1-res.second);
                }
            }
        }
    }
	if(ans<0){
		cout<<-1<<endl;
		return 0;
	}
	ans+=k-1;
    cout<<ans<<endl;
    return 0;
}
0