This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
int const mxN = 3e5+5;
int dis[mxN], lift[mxN][20], elem[mxN], n, k, reach[mxN];
vector<pair<int,int>> adj[mxN];
bool vis[mxN];
vector<pair<int,pair<int,int>>> v;
int up ( int steps , int e) {
for(int i=0; i<=19; i++)
if(steps&(1<<i)) e = lift[e][i];
return e;
}
int bring ( int i ) {
int l = 0, r = n+1, ans = i;
while(l<=r) {
int mid = (l+r)/2, e = up(mid, i);
if(dis[i]-dis[e]<=k) { ans = e, l = mid+1; }
else r = mid-1;
}
return ans;
}
void dfs ( int i , int p, int m) {
dis[i] = m;
lift[i][0] = p;
for(int j=1; j<20; j++) lift[i][j] = lift[lift[i][j-1]][j-1];
elem[i] = bring(i);
v.push_back({-dis[elem[i]], {elem[i], i}});
for(auto it:adj[i]) {
if(it.first==p) continue;
dfs(it.first,i, m+it.second);
}
}
bool f = 1;
void do_work ( int i , int p, int d) {
vis[i] = 1;
for(auto it:adj[i]) {
if(it.first==p) continue;
if(d+it.second>k) continue;
if(reach[it.first]<d+it.second) continue;
reach[it.first] = d+it.second;
vis[it.first] = 1;
if(f) do_work(it.first, i, d+it.second);
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
cin>>n>>k;
int mini = 1e18;
for(int i=1; i<=n+1; i++) reach[i] = 1e18;
for(int i = 0; i<n-1; i++) {
int x,y,c; cin>>x>>y>>c; mini = min(mini, c);
adj[x].push_back({y,c}), adj[y].push_back({x,c});
}
if(mini*2>k) f = 0;
dfs(1,1,0);
vector<int> fin;
sort(v.begin(),v.end());
for(auto it:v) {
if(!vis[it.second.second]) {
vis[it.second.second] = 1;
do_work(it.second.first, 0, 0);
fin.push_back(it.second.first);
}
}
cout<<fin.size()<<'\n';
for(auto it:fin) cout<<it<<' ';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |