45572023-03-29 17:40:59horvathabelLogisztikai központcpp17Wrong answer 48/50151ms42100 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

vector<pair<int,int>> g[100001];
bool seen[100001];
bool seen2[100001];
pair<ll,ll>  mxtav[100001];
ll mxtavvissza[100001];
pair<ll,ll>  mxtav2[100001];
int cnt[100001];
priority_queue<int> q[100001];
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
base48/50
1Accepted0/07ms12812 KiB
2Accepted0/0129ms27852 KiB
3Accepted4/47ms13308 KiB
4Accepted4/47ms13468 KiB
5Accepted4/47ms13640 KiB
6Accepted4/47ms13724 KiB
7Accepted4/47ms13960 KiB
8Accepted5/57ms14372 KiB
9Accepted2/2143ms30664 KiB
10Accepted2/2136ms30768 KiB
11Accepted2/27ms14664 KiB
12Accepted2/28ms14820 KiB
13Accepted2/212ms15488 KiB
14Accepted2/217ms16436 KiB
15Accepted2/2136ms30072 KiB
16Accepted2/2120ms29436 KiB
17Accepted2/2141ms30656 KiB
18Wrong answer0/2104ms26784 KiB
19Accepted2/2151ms28684 KiB
20Accepted3/3143ms42100 KiB