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>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 2e5+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int parent[nax];
vii adj[nax];
vii cur[nax];
ll res=0;
int dis;
bool test=false;
ll mx=0;
void dfs(int x,int p)
{
for(auto u:cur[x])
{
if (u.fi==p) continue;
parent[u.fi]=x;
res+=u.se;
//cout <<x<<" "<<u.fi<<" "<<u.se<<endl;
if (mx<res)
{
mx=res;
dis=u.fi;
}
dfs(u.fi,x);
res-=u.se;
}
}
bool search(int x,int p)
{
for(int u=0;u<cur[x].size();u++)
{
if (cur[x][u].fi==p) continue;
if (cur[x][u].fi==dis){
cur[x][u].se=0;
return true;
}
if (search(cur[x][u].fi,x)){
cur[x][u].se=0;
//cout << x<<" "<<cur[x][u].fi<<" "<<cur[x][u].se<<endl;
return true;
}
}
return false;
}
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("hopscotch");
int n,k;
cin>>n>>k;
for (int i = 0; i < n-1; ++i)
{
int x,y,z;
cin>>x>>y>>z;
x--;y--;
adj[x].pb({y,z});
adj[y].pb({x,z});
}
ll sum=0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cur[j].clear();
}
sum=0;
parent[i]=i;
for (int j = 0; j < n; ++j)
{
for (int q = 0; q < adj[j].size(); ++q)
{
cur[j].pb({adj[j][q]});
}
}
for (int j = 0; j < k; ++j)
{
mx=0;
res=0;
dfs(i,-1);
//cout <<mx<<" "<<dis<<endl;
sum+=mx;
search(i,-1);
}
cout <<sum<<endl;
}
}
Compilation message (stderr)
Main.cpp: In function 'bool search(int, int)':
Main.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int u=0;u<cur[x].size();u++)
| ~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:105:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int q = 0; q < adj[j].size(); ++q)
| ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |