답안 #717956

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
717956 2023-04-02T22:57:09 Z vjudge1 Paths (RMI21_paths) C++17
19 / 100
600 ms 64320 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){
  a.insert(a.end(),b.begin(),b.end());
  return a;
}
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:37: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]
   37 |   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:47: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]
   47 |   if (tmp.size() > k) tmp.resize(k);
      |       ~~~~~~~~~~~^~~
Main.cpp:62: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]
   62 |     if (tmp.size() > k) tmp.resize(k);
      |         ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47188 KB Output is correct
2 Correct 27 ms 47232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47188 KB Output is correct
2 Correct 27 ms 47232 KB Output is correct
3 Correct 23 ms 47316 KB Output is correct
4 Correct 22 ms 47272 KB Output is correct
5 Correct 23 ms 47288 KB Output is correct
6 Correct 21 ms 47316 KB Output is correct
7 Correct 23 ms 47300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47188 KB Output is correct
2 Correct 27 ms 47232 KB Output is correct
3 Correct 23 ms 47316 KB Output is correct
4 Correct 22 ms 47272 KB Output is correct
5 Correct 23 ms 47288 KB Output is correct
6 Correct 21 ms 47316 KB Output is correct
7 Correct 23 ms 47300 KB Output is correct
8 Incorrect 30 ms 47420 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47188 KB Output is correct
2 Correct 27 ms 47232 KB Output is correct
3 Correct 23 ms 47316 KB Output is correct
4 Correct 22 ms 47272 KB Output is correct
5 Correct 23 ms 47288 KB Output is correct
6 Correct 21 ms 47316 KB Output is correct
7 Correct 23 ms 47300 KB Output is correct
8 Incorrect 30 ms 47420 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 57924 KB Output is correct
2 Correct 171 ms 64320 KB Output is correct
3 Correct 117 ms 57416 KB Output is correct
4 Execution timed out 1063 ms 57016 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47188 KB Output is correct
2 Correct 27 ms 47232 KB Output is correct
3 Correct 23 ms 47316 KB Output is correct
4 Correct 22 ms 47272 KB Output is correct
5 Correct 23 ms 47288 KB Output is correct
6 Correct 21 ms 47316 KB Output is correct
7 Correct 23 ms 47300 KB Output is correct
8 Incorrect 30 ms 47420 KB Output isn't correct
9 Halted 0 ms 0 KB -