242872026-02-08 09:46:30ubormaciLogisztikai központcpp17Accepted 50/50120ms12668 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

const ll inf = 1e16;

void bfs(ll start, vector<ll> &d, vector<vector<pair<ll,ll>>> &v, ll n) {

	d.resize(n+1, inf);
	d[start] = 0;

	queue<ll> q;
	q.push(start);
	while(!q.empty()) {
		ll curr = q.front();
		q.pop();

		for(const auto & [neigh, dis] : v[curr]) {

			if(d[curr] + dis < d[neigh]) {
				d[neigh] = d[curr] + dis;
				q.push(neigh);
			} 
		}
	}

}

void solve() {

	// najo, itt a fa szelei kellenek
	// marmint az atmero
	// de biztos sulyozott fara is mukodik?
	// most kiderul
	// (najo, megneztem, az)

	ll n;
	cin >> n;
	vector<vector<pair<ll,ll>>> v(n+1); // node, distance
	for(ll i = 0; i < n - 1; i++) {
		ll a, b, d;
		cin >> a >> b >> d;
		v[a].push_back({b, d});
		v[b].push_back({a, d});
	}

	// do 1 bfs from a random node, pick the one that is farthest
	vector<ll> drand;
	bfs(1, drand, v, n);
	ll e1 = 1;
	for(ll i = 1; i <= n; i++) {
		if(drand[i] >= drand[e1]){
			e1 = i;
		}
 	}

	vector<ll> e1d;
	bfs(e1, e1d, v, n);

	ll e2 = 1;
	for(ll i = 1; i <= n; i++) {
		if(e1d[i] >= e1d[e2]) {
			e2 = i;
		}
	}

	vector<ll> e2d;
	bfs(e2, e2d, v, n);

	// minden node-re megnezzuk, hogy ha itt hozunk letre logisztikai kozpontot
	// mennyi lesz

	ll mn = inf;

	for(ll i = 1; i <= n; i++) {
		// ha ide keszitunk logisztikai kozpontot, milyen messze lesz a legtavolabbi?
		ll h = max(e1d[i], e2d[i]);
		if(h < mn) {
			mn = h;
		}
	}

	cout << mn << "\n";
	vector<ll> ans;

	for(ll i = 1; i <= n; i++) {
		// ha ide keszitunk logisztikai kozpontot, milyen messze lesz a legtavolabbi?
		ll h = max(e1d[i], e2d[i]);
		if(h == mn) {
			ans.push_back(i);
		}
	}

	cout << ans.size() << "\n";
	for(const auto & x : ans) {
		cout << x << " ";
	}

}

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	solve();

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms508 KiB
2Accepted0/081ms10772 KiB
3Accepted4/41ms316 KiB
4Accepted4/41ms316 KiB
5Accepted4/41ms316 KiB
6Accepted4/41ms316 KiB
7Accepted4/41ms316 KiB
8Accepted5/52ms316 KiB
9Accepted2/2120ms11800 KiB
10Accepted2/2119ms11828 KiB
11Accepted2/21ms316 KiB
12Accepted2/22ms564 KiB
13Accepted2/24ms996 KiB
14Accepted2/28ms1588 KiB
15Accepted2/283ms10976 KiB
16Accepted2/278ms10468 KiB
17Accepted2/286ms11256 KiB
18Accepted2/271ms8788 KiB
19Accepted2/287ms12668 KiB
20Accepted3/3101ms11832 KiB