43452023-03-25 16:23:01szilLogisztikai központcpp14Wrong answer 48/5093ms34680 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) {

	int o = dp[u], 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) {
			int 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
base48/50
1Accepted0/04ms6668 KiB
2Accepted0/063ms17540 KiB
3Accepted4/44ms7204 KiB
4Accepted4/44ms7404 KiB
5Accepted4/44ms7740 KiB
6Accepted4/44ms7812 KiB
7Accepted4/44ms7740 KiB
8Accepted5/54ms8104 KiB
9Accepted2/274ms20048 KiB
10Accepted2/267ms19844 KiB
11Accepted2/24ms8216 KiB
12Accepted2/26ms8548 KiB
13Accepted2/27ms9180 KiB
14Accepted2/29ms9756 KiB
15Accepted2/264ms19292 KiB
16Accepted2/259ms18960 KiB
17Accepted2/264ms19964 KiB
18Wrong answer0/250ms17296 KiB
19Accepted2/256ms19832 KiB
20Accepted3/393ms34680 KiB