43462023-03-25 16:28:05szilLogisztikai központcpp14Accepted 50/5083ms34900 KiB
#include <bits/stdc++.h>
 
using ll = long long;
using namespace std;

const int MAXN = 100001;

ll dp[MAXN];
vector<pair<int,ll>> g[MAXN];
ll best = LONG_LONG_MAX/2;
vector<int> ans;

void dfs(int u, int p) {
	for (auto [v,w] : g[u]) {
		if (v == p) continue;
		dfs(v, u);
		dp[u] = max(dp[u], dp[v]+w);
	}
}

void dfs2(int u, int p) {

	ll o = dp[u]; int mx;
	dp[u] = 0;
	for (auto [v,w] : g[u]) {
		if (dp[u] <= dp[v]+w) {
			dp[u] = dp[v]+w;
			mx = v;
		}
	}

	for (auto [v,w] : g[u]) {
		if (v == p) continue;
		if (v == mx) {
			ll o2 = dp[u];
			dp[u] = 0;

			for (auto [k,y] : g[u]) {
				if (k != mx) dp[u] = max(dp[u], dp[k]+y);
			}

			dfs2(v, u);

			dp[u] = o2;
		} else {
			dfs2(v, u);
		}
	}

	if (dp[u] < best) {
		best = dp[u];
		ans.clear();
		ans.push_back(u);
	} else if (dp[u] == best) {
		ans.push_back(u);
	}

	dp[u] = o;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;

	for (int i = 0; i < n - 1; i++) {
		int a, b;
		ll w;
		cin >> a >> b >> w;
		g[a].push_back({b,w});
		g[b].push_back({a,w});
	}

	dfs(1, 0);
	dfs2(1, 0);

	sort(ans.begin(), ans.end());

	cout << best << "\n" << ans.size() << "\n";
	for (int i : ans) cout << i << " ";
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms6672 KiB
2Accepted0/064ms17524 KiB
3Accepted4/44ms7072 KiB
4Accepted4/44ms7284 KiB
5Accepted4/44ms7248 KiB
6Accepted4/44ms7508 KiB
7Accepted4/44ms7464 KiB
8Accepted5/54ms7780 KiB
9Accepted2/270ms19728 KiB
10Accepted2/265ms19984 KiB
11Accepted2/24ms8384 KiB
12Accepted2/26ms8596 KiB
13Accepted2/27ms8868 KiB
14Accepted2/29ms9540 KiB
15Accepted2/261ms19428 KiB
16Accepted2/257ms18904 KiB
17Accepted2/264ms19832 KiB
18Accepted2/250ms17132 KiB
19Accepted2/254ms19756 KiB
20Accepted3/383ms34900 KiB