# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
717959 |
2023-04-02T23:00:26 Z |
vjudge1 |
Paths (RMI21_paths) |
C++17 |
|
600 ms |
64240 KB |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<pii> node[N];
vector<int> suff[N];
vector<int> operator+(vector<int> a,vector<int> b){
if (a.size() > b.size()){
a.insert(a.end(),b.begin(),b.end());
return a;
}
else {
b.insert(b.end(),a.begin(),a.end());
return b;
}
}
bool asd;
int n,k;
void dfs1(int i,int p){
// to calc the suff
if (sz(node[i])==1&&p!=-1){
suff[i] = {0};
return;
}
for (auto [it,w] : node[i]){
if (it==p) continue;
dfs1(it,i);
suff[it].front() += w;
suff[i] = suff[i]+suff[it];
suff[it].front() -= w;
}
sort(all(suff[i]),greater<int>());
if (suff[i].size() > k) suff[i].resize(k);
}
int ans[N];
void dfs2(int i,int p,vector<int> cur){
// calc the answer
// fix the cur for the next child
// idk
vector<int> tmp = cur + suff[i];
sort(all(tmp),greater<int>());
if (tmp.size() > k) tmp.resize(k);
for (int j=0;j<k;j++){
ans[i] += tmp[j];
}
for (auto [it1,w1] : node[i]){
if (it1==p) continue;
tmp = cur;
for (auto [it2,w2] : node[i]){
if (it2!=p&&it2!=it1){
suff[it2].front() += w2;
tmp = tmp + suff[it2];
suff[it2].front() -= w2;
}
}
sort(all(tmp),greater<int>());
if (tmp.size() > k) tmp.resize(k);
tmp.front() += w1;
dfs2(it1,i,tmp);
}
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> k;
if (k==1) asd = 1;
for (int i=0;i<n-1;i++){
int u,v,w;
cin >> u >> v >> w;
node[u].pb({v,w});
node[v].pb({u,w});
}
dfs1(1,-1);
dfs2(1,-1,{});
for (int i=1;i<=n;i++){
cout << ans[i] << endl;
}
}
Compilation message
Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:43:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
43 | if (suff[i].size() > k) suff[i].resize(k);
| ~~~~~~~~~~~~~~~^~~
Main.cpp: In function 'void dfs2(long long int, long long int, std::vector<long long int>)':
Main.cpp:53:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
53 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~
Main.cpp:68:20: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
68 | if (tmp.size() > k) tmp.resize(k);
| ~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
22 ms |
47188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
22 ms |
47188 KB |
Output is correct |
3 |
Correct |
23 ms |
47316 KB |
Output is correct |
4 |
Correct |
23 ms |
47276 KB |
Output is correct |
5 |
Correct |
24 ms |
47340 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
22 ms |
47188 KB |
Output is correct |
3 |
Correct |
23 ms |
47316 KB |
Output is correct |
4 |
Correct |
23 ms |
47276 KB |
Output is correct |
5 |
Correct |
24 ms |
47340 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
28 ms |
47452 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
22 ms |
47188 KB |
Output is correct |
3 |
Correct |
23 ms |
47316 KB |
Output is correct |
4 |
Correct |
23 ms |
47276 KB |
Output is correct |
5 |
Correct |
24 ms |
47340 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
28 ms |
47452 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
57928 KB |
Output is correct |
2 |
Correct |
198 ms |
64240 KB |
Output is correct |
3 |
Correct |
146 ms |
57360 KB |
Output is correct |
4 |
Execution timed out |
1078 ms |
57172 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
22 ms |
47188 KB |
Output is correct |
3 |
Correct |
23 ms |
47316 KB |
Output is correct |
4 |
Correct |
23 ms |
47276 KB |
Output is correct |
5 |
Correct |
24 ms |
47340 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
28 ms |
47452 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |