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>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);
//order_of_key()
#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define fi first
#define se second
#define la "LA"
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define pii pair<int,int>
vector<pii> a[300001];
priority_queue<pii> q;
bool vis[300001]={ };
void dfs(int x, int cover)
{
//cout<<"HA"<<x<<' '<<cover<<endl;
vis[x]=1;
for(auto y:a[x])
{
if (vis[y.fi]) continue;
if (cover>=y.se)
{
dfs(y.fi,cover-y.se);
}
else
{
q.push({0,y.fi});
// cout<<"NOOOOO"<<y.fi<<endl;
}
}
return;
}
signed main()
{
// #ifndef LOCAL
// freopen("lifeguards.in","r",stdin);
// freopen("lifeguards.out","w", stdout);
// #endif
fast
int n,k;
cin>>n >>k;
forr(i,0,n-1)
{
int x,y,z;
cin>>x >>y >>z;
a[x].pb({y,z});
a[y].pb({x,z});
}
forr(i,1,n+1)
{
if (a[i].sz==1) q.push({0,i});
}
vector<int> ans;
while(!q.empty())
{
int last=q.top().fi;
int x=q.top().se;
q.pop();
if (vis[x]) continue;
//cout<<x<<' '<<last<<endl;
vis[x]=1;
int mx=0;
int f=-1;
for(auto w:a[x])
{
int y=w.fi;
int z=w.se;
if (vis[y]) continue;
if (z>k) {
f=0;
//cout<<la<<x<<' '<<y<<endl;
q.push({0,y});}
else if ((z+last)>k)
{
mx=max(mx,z);
f=1;
}
else
{
f=1;
}
}
if (f==-1)
{
ans.pb(x);
}
else if (f==0)
{
ans.pb(x);
}
else if (mx==0)
{
for(auto w:a[x])
{
int y=w.fi;
int z=w.se;
if(vis[y]) continue;
if (z<=k) q.push({z+last,y});
}
}
else
{
ans.pb(x);
for(auto w:a[x])
{
int y=w.fi;
int z=w.se;
if (vis[y]) continue;
if (z<=k)
{
// cout<<y<<endl;
dfs(y,k-z);
// int cur=y;
// while()
}
}
}
}
// if (ans.sz==0) {cout<<1<<nn<<1; return 0;}
cout<<ans.sz<<nn;
for(auto p:ans) cout<<p<<' ';
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... |