为什么暴力DFS就AC了?请求加强数据
查看原帖
为什么暴力DFS就AC了?请求加强数据
817723
sirwangg楼主2024/3/19 17:11
#include <bits/stdc++.h>

using namespace std;

int n, ans=-1e9;
int num[25];
vector<int > v[25];

struct node{
	int cnt;
	string str;
	node(int a, string s): cnt(a), str(s){};
};

node _max(node a, node b){
	if (a.cnt > b.cnt) return a;
	else return b;
}

node dfs(int n, int cnt, string s){
	node t = node(0, s);
	for (int i: v[n]){
		t = _max(t, dfs(i, num[i], s+" " + to_string(i)));
	}
	t.cnt += cnt;
	return t;
}

int main(){
	cin >> n;
	for (int i=1; i<=n; i++) cin >> num[i];
	
	int t;
	for (int i=1; i<=n-1; i++){
		for (int j=i+1; j<=n; j++){
			cin >> t;
			if (t == 1){
				v[i].push_back(j);
			}
		}
	}
	node ans = node(0, "");
	for (int i=1; i<=n; i++){
		ans = _max(ans, dfs(i, num[i], to_string(i)));
	}
	cout << ans.str << endl;
	cout << ans.cnt << endl;
}
2024/3/19 17:11
加载中...