結果

問題 No.1789 Tree Growing
ユーザー chocoruskchocorusk
提出日時 2021-12-18 00:35:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,569 bytes
コンパイル時間 3,873 ms
コンパイル使用メモリ 197,324 KB
実行使用メモリ 7,664 KB
最終ジャッジ日時 2023-10-13 04:06:18
合計ジャッジ時間 48,508 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 5 ms
4,352 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 3 ms
4,352 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 10 ms
4,352 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 95 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 106 ms
4,348 KB
testcase_18 AC 73 ms
4,352 KB
testcase_19 AC 35 ms
4,348 KB
testcase_20 AC 175 ms
4,352 KB
testcase_21 AC 697 ms
4,348 KB
testcase_22 AC 259 ms
4,348 KB
testcase_23 AC 337 ms
4,352 KB
testcase_24 AC 298 ms
4,348 KB
testcase_25 AC 2,871 ms
4,352 KB
testcase_26 AC 2,856 ms
4,352 KB
testcase_27 AC 2,461 ms
4,348 KB
testcase_28 AC 2,488 ms
4,348 KB
testcase_29 AC 2,046 ms
4,352 KB
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 AC 4,964 ms
4,348 KB
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘void hungarian<T>::augment() [with T = int]’ 内:
main.cpp:90:25: 警告: ‘y’ may be used uninitialized [-Wmaybe-uninitialized]
   90 |                         if(y<m) break;
      |                         ^~
main.cpp:76:24: 備考: ‘y’ はここで定義されています
   76 |                 int x, y;
      |                        ^
main.cpp:108:51: 警告: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
  108 |                         for(int cx=x, cy=y, ty; cx!=-2; cx=prev[cx], cy=ty){
      |                                                 ~~^~~~
main.cpp:76:21: 備考: ‘x’ はここで定義されています
   76 |                 int x, y;
      |                     ^

ソースコード

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>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
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;
int n, k;
vector<int> gn[MAXN], gk[MAXK];
int vn[MAXN], vk[MAXK], ordn[MAXN], ordk[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);
    }
    
    const int INF=1e6;
    int ans=-INF;
    for(int r1=0; r1<k; r1++){
        queue<int> que1;
        que1.push(r1);
        bool used1[MAXK]={};
        used1[r1]=1;
        int t=0;
        while(!que1.empty()){
            int x=que1.front(); que1.pop();
            ordk[x]=t;
            vk[t++]=x;
            for(auto y:gk[x]){
                if(used1[y]) continue;
                que1.push(y);
                used1[y]=1;
            }
        }
        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;
                    vector<vector<int>> cost(k1, vector<int>(n1, -INF));
                    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;
                            cost[j1++][i1]=dp[ordn[z]][ordk[w]];
                        }
                        i1++;
                    }
                    int mx=0;
                    if(k1){
                        hungarian<int> h(cost);
                        mx=h.max_cost;
                    }
                    if(i==0 && j==0) ans=max(ans, mx);
                    dp[i][j]=max(dp[i][j], mx);
                }
            }
        }
    }
	if(ans<0){
		cout<<-1<<endl;
		return 0;
	}
	ans+=k-1;
    cout<<ans<<endl;
    return 0;
}
0