답안 #477984

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477984 2021-10-05T00:39:42 Z zerver Magic Tree (CEOI19_magictree) C++14
3 / 100
140 ms 29144 KB
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(nullptr)
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define sz(v) int(v.size())
#define ss second
#define ff first
#define endl '\n'
using namespace std;
typedef long long Long;
typedef pair<int, int> ii;
const Long INF = 1e13;
const int N = 1e5 + 7;
vector<int> adj[N];
set<ii> dif[N];
int dia[N], peso[N], pt[N];

void dfs(int from){
  int big = 0;
  for(int to: adj[from]){
    dfs(to);
    if(big == 0 || sz(dif[pt[to]]) > sz(dif[pt[big]])) big = to;
  }
  if(big == 0){
    if(dia[from] == 0) return;
    dif[pt[from]].insert({dia[from], peso[from]});
    return;
  }
  pt[from] = pt[big];
  for(int to: adj[from]){
    if(to == big) continue;
    for(auto p: dif[pt[to]]) dif[pt[from]].insert(p);
  }
  if(dia[from] != 0){
    ii par = {dia[from] + 1, 0};
    auto it = dif[pt[from]].lower_bound(par);
    ii ins = {dia[from], peso[from]};
    for(; it != dif[pt[from]].end(); ){
      ii npar = *(it);
      if(npar.ss <= ins.ss){
        ins.ss -= npar.ss;
        it = dif[pt[from]].erase(it);
      }
      else{
        npar.ss -= ins.ss;
        dif[pt[from]].erase(it);
        dif[pt[from]].insert(npar);
        break;
      }
    }
    dif[pt[from]].insert(ins);
  }
}

void solve(){
  int n, m, k; cin >> n >> m >> k;
  for(int i = 2; i <= n; i++){
    int p; cin >> p;
    adj[p].push_back(i);
  }
  for(int i = 0; i < m; i++){
    int v; cin >> v;
    cin >> dia[v] >> peso[v];
  }
  for(int i = 1; i <= n; i++) pt[i] = i;
  dfs(1);
  Long ans = 0;
  for(auto p: dif[pt[1]]) ans += p.ss;
  cout << ans << endl;
}

int main(){
  FAST_IO;
  int tt = 1; 
  // cin >> tt;
  while(tt--) solve();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 15812 KB Output is correct
2 Correct 43 ms 17716 KB Output is correct
3 Correct 140 ms 29144 KB Output is correct
4 Correct 75 ms 17368 KB Output is correct
5 Correct 116 ms 18760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 7500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 140 ms 27560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 8012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 7244 KB Output isn't correct
2 Halted 0 ms 0 KB -