45542023-03-29 17:38:31horvathabelTársaság (50)cpp17Wrong answer 0/50122ms88328 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
base0/50
1Wrong answer0/0122ms88328 KiB
2Runtime error0/027ms64748 KiB
3Runtime error0/327ms64512 KiB
4Runtime error0/327ms64276 KiB
5Runtime error0/330ms64064 KiB
6Runtime error0/327ms63952 KiB
7Runtime error0/327ms63716 KiB
8Runtime error0/326ms63700 KiB
9Runtime error0/326ms63468 KiB
10Runtime error0/326ms63452 KiB
11Runtime error0/330ms63216 KiB
12Runtime error0/326ms63208 KiB
13Runtime error0/426ms63196 KiB
14Runtime error0/430ms62972 KiB
15Runtime error0/426ms62960 KiB
16Runtime error0/426ms62952 KiB
17Runtime error0/430ms62712 KiB