# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
878261 |
2023-11-24T07:49:50 Z |
Mr_Ph |
Paths (RMI21_paths) |
C++17 |
|
0 ms |
0 KB |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update>;
ll mod=(ll)1e9+7;
ll mod1=998244353;
///the defines :)
#define endl '\n'
#define vi vector<int>
#define ent(arr) for(int i=0;i<arr.size();i++)cin>>arr[i];
#define all(arr) arr.begin(),arr.end()
#define allr(arr) arr.rbegin(),arr.rend()
#define sz size()
#define int long long
vector<vector<pair<int,int>>>adj;
int n,k;
int length[200002];
pair<int,int> ans[2][20002];
pair<int,int>dfs(int node,int parent,int w)
{
// cout<<"HI"<<endl;
pair<int,int>xd= {0,0};
for(auto i:adj[node])
{
if(i.first==parent)
continue;
pair<int,int>mx=dfs(i.first,node,i.second);
// mx.first+=w;
xd=max(xd,mx);
if(ans[0][i.first]>ans[0][node])
ans[1][node]=max(ans[0][node],ans[1][i.first]),ans[0][node]=ans[0][i.first];
else
ans[1][node]=max(ans[0][i.first],ans[1][node]);
}
if(adj[node].sz==1)
{
length[node]=w;
ans[0][node]= {w,node};
return {w,node};
}
length[xd.second]+=w;
xd.first+=w;
ans[0][node].first+=w;
return xd;
}
ordered_set<pair<int,int>>st;
int lol;
int fans[200002];
void re(int x)
{
int pog=st.order_of_key({length[x],x});
if(pog>=k)lol-=length[x];
st.erase({length[x],x});
}
void add(int x,bool del)
{
st.insert({length[x],x});
int pog=st.order_of_key({length[x],x});
if(del){
if(pog>=k)
lol+=length[x];
else lol+=(*st.find_by_order(k)).first;
}
else{
if(pog>=k)
{
lol+=length[x];
if(k>0)
lol-=(*st.find_by_order(k-1)).first;
}
}
}
void dfs1(int node,int parent,int w,pair<int,int>prv)
{
if(parent!=0){
prv.first+=w;
int pog=st.order_of_key({length[ans[0][node].second],ans[0][node].second});
re(ans[0][node].second);
length[ans[0][node].second]-=w;
add(ans[0][node].second,pog>=k);
fans[node]=lol;
}
for(auto i:adj[node])
{
if(i.first==parent)continue;
pair<int,int>nxt=prv;
if(ans[0][i.first].second==ans[0][node].second)
nxt=max(ans[1][node],nxt);
else nxt=max(ans[1][node],nxt);
int pog=st.order_of_key({length[nxt.second],nxt.second});
re(nxt.second);
length[nxt.second]+=i.second;
add(nxt.second,pog>=k);
dfs1(i.first,node,i.second,nxt);
pog=st.order_of_key({length[nxt.second],nxt.second});
re(nxt.second);
length[nxt.second]-=i.second;
add(nxt.second,pog>=k);
}
if(parent!=0){
int pog=st.order_of_key({length[ans[0][node].second],ans[0][node].second});
re(ans[0][node].second);
length[ans[0][node].second]+=w;
add(ans[0][node].second,pog>=k);
}
}
void preprocess() {}
void solve()
{
cin>>n>>k;
adj.resize(n+1);
for(int i=0; i<n-1; i++)
{
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
dfs(1,0,0);
for(int i=1; i<=n; i++)
st.insert({length[i],i});
int cnt=0;
for(auto i:st)
{
if(st.sz-cnt<=k)
lol+=i.first;
cnt++;
}
k=max(0ll,st.sz-k);
fans[1]=lol;
dfs1(1,0,0,{0,0});
for(int i=1;i<=n;i++)cout<<fans[i]<<endl;
}
signed main()
{
// freopen("meta_game_input.txt","r",stdin);
// freopen("otput.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
preprocess();
//bla();
int t=1;
//cin>>t;
while(t--)
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:129:21: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
129 | if(st.sz-cnt<=k)
| ~~~~~~~~~^~~
Main.cpp:133:22: error: no matching function for call to 'max(long long int, long long unsigned int)'
133 | k=max(0ll,st.sz-k);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from Main.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
254 | max(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: template argument deduction/substitution failed:
Main.cpp:133:22: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
133 | k=max(0ll,st.sz-k);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from Main.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: template argument deduction/substitution failed:
Main.cpp:133:22: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
133 | k=max(0ll,st.sz-k);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from Main.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
3480 | max(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: template argument deduction/substitution failed:
Main.cpp:133:22: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
133 | k=max(0ll,st.sz-k);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from Main.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
3486 | max(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: template argument deduction/substitution failed:
Main.cpp:133:22: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
133 | k=max(0ll,st.sz-k);
| ^