43422023-03-25 16:12:24szilLogisztikai központcpp14Wrong answer 48/50101ms34300 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 << " ";
	cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base48/50
1Accepted0/04ms6672 KiB
2Accepted0/064ms17532 KiB
3Accepted4/44ms7184 KiB
4Accepted4/44ms7404 KiB
5Accepted4/44ms7616 KiB
6Accepted4/44ms7564 KiB
7Accepted4/44ms7584 KiB
8Accepted5/54ms7692 KiB
9Accepted2/271ms19508 KiB
10Accepted2/276ms19592 KiB
11Accepted2/24ms8056 KiB
12Accepted2/26ms8536 KiB
13Accepted2/27ms8748 KiB
14Accepted2/29ms9428 KiB
15Accepted2/264ms19216 KiB
16Accepted2/261ms18628 KiB
17Accepted2/289ms19588 KiB
18Wrong answer0/250ms16832 KiB
19Accepted2/263ms19444 KiB
20Accepted3/3101ms34300 KiB