45562023-03-29 17:39:27horvathabelLogisztikai központcpp17Runtime error 35/50201ms127748 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

vector<pair<int,int>> g[1000001];
bool seen[1000001];
bool seen2[1000001];
pair<ll,ll>  mxtav[1000001];
ll mxtavvissza[1000001];
pair<ll,ll>  mxtav2[1000001];
int cnt[1000001];
priority_queue<int> q[1000001];
int dfs(int x, int tv){
		seen[x]=true;
		if (cnt[x]==1 && x!=1) return tv; 
		for (pair<int, int> edge:g[x]){
			if (!seen[edge.first]){
								seen[edge.first]=true;

				int v=dfs(edge.first, edge.second); 
				if (v>mxtav[x].first){
					mxtav2[x]=mxtav[x];
					mxtav[x].first=v;
					mxtav[x].second=edge.first; 
				}
				else if (mxtav2[x].first<v){
					mxtav2[x].first=v;
					mxtav2[x].second=edge.first; 
				}
		}
		}
		
	return (mxtav[x].first+tv);
}
void dfs2(int x){
	seen2[x]=true;
	for (pair<int, int> edge:g[x]){
		if (!seen2[edge.first]){
			seen2[edge.first]=true; 
			mxtavvissza[edge.first]=max(mxtavvissza[x]+edge.second,mxtavvissza[edge.first]);
			if (edge.first!=mxtav[x].second) mxtavvissza[edge.first]=max(mxtavvissza[edge.first], mxtav[x].first+edge.second);
			else  mxtavvissza[edge.first]=max(mxtavvissza[edge.first], mxtav2[x].first+edge.second);
			dfs2(edge.first);
		}

	}
}
int main() {
	int n;
	cin>>n;
	for (int i=0; i<n-1;i++){
		int x,y,c;
		cin>>x>>y>>c;
		g[x].push_back({y,c});
		g[y].push_back({x,c});
		cnt[x]++;
		cnt[y]++; 
		}
		

	dfs(1,0);
	dfs2(1);
	for (int i=1; i<=n ;i++) mxtav[i].first=max(mxtav[i].first, mxtavvissza[i]);
	vector<int> ans; 
	int z=INT_MAX; 
	for (int i=1; i<=n;i++){
		if (z>mxtav[i].first){
			z=mxtav[i].first;
			ans.clear();
		}
		if (z==mxtav[i].first) ans.push_back(i);
		
	}
	cout<<z<<endl<<ans.size()<<endl;
	for (int i:ans) cout<<i<<" ";
}
SubtaskSumTestVerdictTimeMemory
base35/50
1Accepted0/041ms111436 KiB
2Accepted0/0201ms126304 KiB
3Accepted4/443ms111836 KiB
4Accepted4/441ms112312 KiB
5Accepted4/441ms112028 KiB
6Accepted4/452ms112352 KiB
7Accepted4/452ms112572 KiB
8Accepted5/543ms112664 KiB
9Runtime error0/2160ms127748 KiB
10Runtime error0/2187ms127724 KiB
11Accepted2/241ms112944 KiB
12Accepted2/241ms113236 KiB
13Accepted2/259ms113880 KiB
14Accepted2/254ms114676 KiB
15Runtime error0/2185ms127068 KiB
16Runtime error0/2148ms127056 KiB
17Runtime error0/2164ms127048 KiB
18Wrong answer0/2152ms124888 KiB
19Accepted2/2194ms126876 KiB
20Runtime error0/3148ms126920 KiB