#include<iostream>
#include<vector>
#define MAXN 100010
using namespace std;
vector<pair<int,int>> edges[MAXN];
vector<pair<int,int>> edges2[MAXN];
int dfs(int curr, int parent)
{
int maxx=0;
for(int i=0;i<edges[curr].size();i++)
{
int next=edges[curr][i].first;
if(next==parent)continue;
int val=edges[curr][i].second;
maxx=max(maxx,val+dfs(next,curr));
}
return maxx;
}
bool clean(int curr, int parent, int len, int lim)
{
if(len==lim)return 1;
for(int i=0;i<edges[curr].size();i++)
{
int next=edges[curr][i].first;
if(next==parent)continue;
int val=edges[curr][i].second;
if(clean(next,curr,len+val,lim))
{
edges[curr][i].second=0;
return 1;
}
}
return 0;
}
int main()
{
int n,k;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++)
{
int a,b,x;
cin>>a>>b>>x;
edges[a].push_back({b,x});
edges[b].push_back({a,x});
edges2[a].push_back({b,x});
edges2[b].push_back({a,x});
}
for(int i=1;i<=n;i++)//n
{
int ans=0;
for(int j=1;j<=k;j++)
{
int res=dfs(i,0);
ans+=res;
clean(i,0,0,res);
}
for(int j=1;j<=n;j++)
{
edges[j]=edges2[j];
}
cout<<ans<<"\n";
}
}
Compilation message
Main.cpp: In function 'int dfs(int, int)':
Main.cpp:10: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]
10 | for(int i=0;i<edges[curr].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'bool clean(int, int, int, int)':
Main.cpp:22: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]
22 | for(int i=0;i<edges[curr].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1049 ms |
14712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |