답안 #848493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
848493 2023-09-12T19:15:01 Z TahirAliyev Paths (RMI21_paths) C++17
36 / 100
600 ms 1088 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long int
#define oo 1e9
#define pii pair<ll, int>

const int MAX = 2002;
int n, k;

pii segTree[4 * MAX];
ll lazy[4 * MAX];

vector<pii> g[MAX];
int timeIn[MAX], timeOut[MAX];
ll pathVal[MAX];
bool marked[MAX];
pii par[MAX];
vector<int> leafs;

void relax(int node, int l, int r){
    if(lazy[node] == 0) return;
    segTree[node].first += lazy[node];
    if(l != r){
        lazy[node << 1] += lazy[node];
        lazy[node << 1 | 1] += lazy[node];
    }
    lazy[node] = 0;
}

void build(int node, int l, int r){
    if(l == r){
        segTree[node] = {pathVal[leafs[l]], leafs[l]};
        return;
    }
    int mid = (l + r) >> 1;
    build(node << 1, l, mid);
    build(node << 1 | 1, mid + 1, r);
    segTree[node] = max(segTree[node << 1], segTree[node << 1| 1]);
}

void update(int node, int l, int r, int ul, int ur, ll val){
    relax(node, l, r);
    if(ul > r || l > ur) return;
    if(ul <= l && r <= ur){
        lazy[node] += val;
        relax(node, l, r);
        return;
    }
    int mid = (l + r) >> 1;
    update(node << 1, l, mid, ul, ur, val);
    update(node << 1 | 1, mid + 1, r, ul, ur, val);
    segTree[node] = max(segTree[node << 1], segTree[node << 1 | 1]);
}


int t = 1;
void dfs(int node, int p, ll h){
    timeIn[node] = t;
    pathVal[node] = h;
    for(pii to : g[node]){
        if(p == to.first) continue;
        par[to.first] = make_pair(node, to.second);
        dfs(to.first, node, h + to.second);
    }
    timeOut[node] = t - 1;
    if(g[node].size() == 1 && node != p){
        leafs.push_back(node);
        timeOut[node] = t++;
    }
}

int main(){
    scanf("%d%d", &n, &k);
    for(int i = 1; i < n; i++){
        int u, v, a; scanf("%d%d%d", &u, &v, &a);
        g[u].push_back({v, a});
        g[v].push_back({u, a});
    }
    for(int i = 1; i <= n; i++){
        t = 0;
        memset(lazy, 0, sizeof(lazy));
        memset(marked, 0, sizeof(marked));
        leafs.clear();

        dfs(i, i, 0);
        build(1, 0, t - 1);
        ll ans = 0;
        for(int j = 0; j < k; j++){
            pii m = segTree[1];
            if(m.first == 0) break;
            ans += m.first;
            int a = m.second;
            while(a != i){
                if(marked[a]) break;
                update(1, 0, t - 1, timeIn[a], timeOut[a], -par[a].second);
                marked[a] = true;
                a = par[a].first;
            }
        }
        cout << ans << '\n';
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:78:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         int u, v, a; scanf("%d%d%d", &u, &v, &a);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 86 ms 856 KB Output is correct
9 Correct 104 ms 692 KB Output is correct
10 Correct 77 ms 668 KB Output is correct
11 Correct 42 ms 664 KB Output is correct
12 Correct 60 ms 652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 86 ms 856 KB Output is correct
9 Correct 104 ms 692 KB Output is correct
10 Correct 77 ms 668 KB Output is correct
11 Correct 42 ms 664 KB Output is correct
12 Correct 60 ms 652 KB Output is correct
13 Correct 580 ms 784 KB Output is correct
14 Correct 453 ms 1088 KB Output is correct
15 Correct 242 ms 600 KB Output is correct
16 Correct 390 ms 784 KB Output is correct
17 Correct 488 ms 600 KB Output is correct
18 Correct 233 ms 604 KB Output is correct
19 Execution timed out 637 ms 996 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 3 ms 344 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 86 ms 856 KB Output is correct
9 Correct 104 ms 692 KB Output is correct
10 Correct 77 ms 668 KB Output is correct
11 Correct 42 ms 664 KB Output is correct
12 Correct 60 ms 652 KB Output is correct
13 Correct 580 ms 784 KB Output is correct
14 Correct 453 ms 1088 KB Output is correct
15 Correct 242 ms 600 KB Output is correct
16 Correct 390 ms 784 KB Output is correct
17 Correct 488 ms 600 KB Output is correct
18 Correct 233 ms 604 KB Output is correct
19 Execution timed out 637 ms 996 KB Time limit exceeded
20 Halted 0 ms 0 KB -