結果

問題 No.1789 Tree Growing
ユーザー chocoruskchocorusk
提出日時 2021-12-18 19:51:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 6,407 bytes
コンパイル時間 6,290 ms
コンパイル使用メモリ 289,300 KB
実行使用メモリ 5,436 KB
最終ジャッジ日時 2023-10-13 18:03:06
合計ジャッジ時間 9,954 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 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 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 3 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 3 ms
4,352 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 9 ms
4,532 KB
testcase_22 AC 6 ms
4,368 KB
testcase_23 AC 7 ms
4,348 KB
testcase_24 AC 6 ms
4,352 KB
testcase_25 AC 11 ms
4,536 KB
testcase_26 AC 9 ms
4,476 KB
testcase_27 AC 10 ms
4,376 KB
testcase_28 AC 10 ms
4,528 KB
testcase_29 AC 10 ms
4,480 KB
testcase_30 AC 12 ms
4,512 KB
testcase_31 AC 12 ms
4,472 KB
testcase_32 AC 11 ms
4,552 KB
testcase_33 AC 11 ms
4,500 KB
testcase_34 AC 12 ms
4,568 KB
testcase_35 AC 11 ms
4,452 KB
testcase_36 AC 11 ms
4,404 KB
testcase_37 AC 12 ms
4,396 KB
testcase_38 AC 10 ms
4,504 KB
testcase_39 AC 12 ms
4,476 KB
testcase_40 AC 12 ms
4,392 KB
testcase_41 AC 11 ms
4,540 KB
testcase_42 AC 12 ms
4,524 KB
testcase_43 AC 13 ms
4,416 KB
testcase_44 AC 13 ms
4,724 KB
testcase_45 AC 12 ms
4,572 KB
testcase_46 AC 13 ms
4,396 KB
testcase_47 AC 13 ms
4,532 KB
testcase_48 AC 12 ms
4,496 KB
testcase_49 AC 13 ms
4,532 KB
testcase_50 AC 14 ms
4,528 KB
testcase_51 AC 14 ms
4,456 KB
testcase_52 AC 12 ms
4,512 KB
testcase_53 AC 13 ms
4,476 KB
testcase_54 AC 17 ms
5,240 KB
testcase_55 AC 16 ms
5,132 KB
testcase_56 AC 17 ms
5,120 KB
testcase_57 AC 13 ms
4,636 KB
testcase_58 AC 19 ms
5,436 KB
testcase_59 AC 13 ms
4,668 KB
testcase_60 AC 9 ms
4,372 KB
testcase_61 AC 8 ms
4,352 KB
testcase_62 AC 10 ms
4,348 KB
testcase_63 AC 11 ms
4,352 KB
testcase_64 AC 8 ms
4,352 KB
testcase_65 AC 8 ms
4,352 KB
testcase_66 AC 9 ms
4,368 KB
testcase_67 AC 8 ms
4,348 KB
testcase_68 AC 9 ms
4,348 KB
testcase_69 AC 9 ms
4,472 KB
testcase_70 AC 8 ms
4,356 KB
testcase_71 AC 7 ms
4,368 KB
testcase_72 AC 9 ms
4,356 KB
testcase_73 AC 9 ms
4,352 KB
testcase_74 AC 11 ms
4,496 KB
testcase_75 AC 1 ms
4,352 KB
testcase_76 AC 1 ms
4,348 KB
testcase_77 AC 7 ms
4,448 KB
testcase_78 AC 10 ms
5,232 KB
testcase_79 AC 5 ms
4,352 KB
testcase_80 AC 15 ms
4,448 KB
testcase_81 AC 16 ms
4,536 KB
testcase_82 AC 15 ms
4,404 KB
testcase_83 AC 11 ms
4,368 KB
testcase_84 AC 11 ms
4,352 KB
testcase_85 AC 11 ms
4,376 KB
testcase_86 AC 11 ms
4,352 KB
testcase_87 AC 12 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘void hungarian<T>::augment() [with T = int]’ 内:
main.cpp:65:25: 警告: ‘y’ may be used uninitialized [-Wmaybe-uninitialized]
   65 |                         if(y<m) break;
      |                         ^~
main.cpp:51:24: 備考: ‘y’ はここで定義されています
   51 |                 int x, y;
      |                        ^
main.cpp:83:51: 警告: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   83 |                         for(int cx=x, cy=y, ty; cx!=-2; cx=prev[cx], cy=ty){
      |                                                 ~~^~~~
main.cpp:51:21: 備考: ‘x’ はここで定義されています
   51 |                 int x, y;
      |                     ^

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
template<typename T>
struct hungarian{//n<=m
	const T inf=numeric_limits<T>::max();
	int n, m, max_match, root;
	T max_cost;
	vector<vector<T>> cost;
	vector<T> lx, ly, slack;
	vector<int> xy, yx, prev, slackx;
	vector<bool> s, t;
	void update_labels(){
		T delta=inf;
		for(int y=0; y<m; y++) if(!t[y]) delta=min(delta, slack[y]);
		for(int x=0; x<n; x++) if(s[x]) lx[x]-=delta;
		for(int y=0; y<m; y++) if(t[y]) ly[y]+=delta;
		for(int y=0; y<m; y++) if(!t[y]) slack[y]-=delta;
	}
	void add_to_tree(int x, int prevx){
		s[x]=true;
		prev[x]=prevx;
		for(int y=0; y<m; y++){
			if(lx[x]+ly[y]-cost[x][y]<slack[y]){
				slack[y]=lx[x]+ly[y]-cost[x][y];
				slackx[y]=x;
			}
		}
	}
	void augment(){
		if(max_match==n) return;
		fill(s.begin(), s.end(), false);
		fill(t.begin(), t.end(), false);
		fill(prev.begin(), prev.end(), -1);
		queue<int> que;
		for(int x=0; x<n; x++){
			if(xy[x]==-1){
				root=x;
				que.push(x);
				prev[x]=-2;
				s[x]=true;
				break;
			}
		}
		for(int y=0; y<m; y++){
			slack[y]=lx[root]+ly[y]-cost[root][y];
			slackx[y]=root;
		}
		int x, y;
		while(1){
			while(!que.empty()){
				x=que.front(); que.pop();
				for(y=0; y<m; y++){
					if(cost[x][y]==lx[x]+ly[y] && !t[y]){
						if(yx[y]==-1) break;
						t[y]=true;
						que.push(yx[y]);
						add_to_tree(yx[y], x);
					}
				}
				if(y<m) break;
			}
			if(y<m) break;
			update_labels();
			for(y=0; y<m; y++){
				if(!t[y] && slack[y]==0){
					if(yx[y]==-1){
						x=slackx[y];
						break;
					}else{
						t[y]=true;
						que.push(yx[y]);
						add_to_tree(yx[y], slackx[y]);
					}
				}
			}
			if(y<m) break;
		}
		if(y<m){
			max_match++;
			for(int cx=x, cy=y, ty; cx!=-2; cx=prev[cx], cy=ty){
				ty=xy[cx];
				yx[cy]=cx, xy[cx]=cy;
			}
			augment();
		}
	}
	hungarian(const vector<vector<T>> &cost):max_match(0), max_cost(0), cost(cost), n(cost.size()), m(cost[0].size()), lx(n, -inf), ly(m), xy(n, -1), yx(m, -1), s(n), t(m), prev(n), slack(m), slackx(m){
		for(int x=0; x<n; x++) for(int y=0; y<m; y++) lx[x]=max(lx[x], cost[x][y]);
		augment();
		for(int x=0; x<n; x++) max_cost+=cost[x][xy[x]];
	}
};
const int MAXN=100;
const int MAXK=100;
const int INF=1e6;
int n, k, vn[MAXN][MAXN], vk[MAXK], ordn[MAXN][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();
    }

    vector<string> vss2;
    string tsn[MAXN][MAXN];
    int tn[MAXN][MAXN];
    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[r][x]=t;
            vn[r][t++]=x;
            for(auto y:gn[x]){
                if(used[y]) continue;
                que.push(y);
                used[y]=1;
            }
        }
        for(int i=n-1; i>=0; i--){
            int x=vn[r][i];
            vector<string> vs;
            for(auto y:gn[x]){
                if(ordn[r][y]<ordn[r][x]) continue;
                vs.push_back("("+tsn[r][ordn[r][y]]+")");
            }
            sort(vs.begin(), vs.end());
            tsn[r][i]="";
            for(auto s:vs) tsn[r][i]+=s;
            vss2.push_back(tsn[r][i]);
        }
    }
    sort(vss2.begin(), vss2.end());
    vss2.erase(unique(vss2.begin(), vss2.end()), vss2.end());
    for(int r=0; r<n; r++){
        for(int i=0; i<n; i++){
            tn[r][i]=lower_bound(vss2.begin(), vss2.end(), tsn[r][i])-vss2.begin();
        }
    }

    vector<vector<int>> dp(vss.size(), vector<int>(vss2.size(), -INF));
    vector<vector<bool>> ok(vss.size(), vector<bool>(vss2.size()));
    int ans=-INF;
    for(int r=0; r<n; r++){
        for(int i=n-1; i>=0; i--){
            for(int j=k-1; j>=0; j--){
                if(!(i==0 && j==0) && ok[tk[j]][tn[r][i]]) continue;
                int x=vn[r][i], y=vk[j], j1=tk[j], i1=tn[r][i];
                ok[j1][i1]=1;
                for(auto z:gn[x]){
                    if(ordn[r][z]<ordn[r][x]) continue;
                    dp[j1][i1]=max(dp[j1][i1], dp[j1][tn[r][ordn[r][z]]]+1);
                }
                int n1=gn[vn[r][i]].size(), k1=gk[vk[j]].size();
                if(i>0) n1--;
                if(j>0) k1--;
                if(k1>n1) continue;
                vector<vector<int>> cost(k1, vector<int>(n1, -INF));
                int i2=0;
                for(auto z:gn[x]){
                    if(ordn[r][z]<ordn[r][x]) continue;
                    int j2=0;
                    for(auto w:gk[y]){
                        if(ordk[w]<ordk[y]) continue;
						cost[j2++][i2]=dp[tk[ordk[w]]][tn[r][ordn[r][z]]];
                    }
                    i2++;
                }
                int mx=0;
                if(k1){
                    hungarian<int> h(cost);
                    mx=h.max_cost;
                }
                if(i==0 && j==0) ans=max(ans, mx);
                dp[j1][i1]=max(dp[j1][i1], mx);
            }
        }
    }
	if(ans<0){
		cout<<-1<<endl;
		return 0;
	}
	ans+=k-1;
    cout<<ans<<endl;
    return 0;
}
0