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/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
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 vll vector<ll>
#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
map<pair<int,int>,int>mp;
vector<vi>adj;
int mx=0,lol=0;
vi xd;
void dfs(int node,int parent,int dis,vi path)
{
//cout<<node<<" "<<parent<<endl;
path.push_back(node);
if(adj[node].sz==1)
{
if(dis>mx)
lol=node,mx=dis,xd=path;
}
for(auto i:adj[node])
{
if(i!=parent)
dfs(i,node,dis+mp[{node,i}],path);
}
}
vector<vector<pair<int,int>>>adj1;
int no=0;
int dist[200002];
void dfs1(int node,int parent,int dis)
{
if(dis>=mx)no=node;
for(auto i:adj1[node])
if(i.first!=parent)dfs1(i.first,node,dis+i.second);
}
void dfsdis(int node,int parent,int dis)
{
dist[node]=max(dist[node],dis);
for(auto i:adj1[node])
if(i.first!=parent)dfsdis(i.first,node,dis+i.second);
}
void preprocess() {}
void solve()
{
int n,k;
cin>>n>>k;
adj.resize(n+1);
adj1.resize(n+1);
if(k==1)
{
for(int i=0; i<n-1; i++)
{
int a,b,c;
cin>>a>>b>>c;
adj1[a].push_back({b,c});
adj1[b].push_back({a,c});
}
dfs1(1,0,0);
int xdd=no;
mx=0;
no=0;
dfs1(xdd,0,0);
dfsdis(no,0,0);
dfsdis(xdd,0,0);
for(int i=1;i<=n;i++)
cout<<dist[i]<<endl;
return;
}
for(int i=0; i<n-1; i++)
{
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back(b);
adj[b].push_back(a);
mp[ {a,b}]=c;
mp[ {b,a}]=c;
}
map<pair<int,int>,int>mp1=mp;
for(int root=1; root<=n; root++)
{
mp=mp1;
int x=k;
int ans=0;
while(x--)
{
mx=0,lol=0;
xd.clear();
dfs(root,0,0,{});
if(lol==0)
break;
ans+=mx;
for(int i=1; i<xd.sz; i++)
{
mp[ {xd[i],xd[i-1]}]=0;
mp[ {xd[i-1],xd[i]}]=0;
}
}
cout<<ans<<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 (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:106:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=1; i<xd.sz; i++)
| ^
# | 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... |