Submission #718238

# Submission time Handle Problem Language Result Execution time Memory
718238 2023-04-03T17:36:13 Z lacito Paths (RMI21_paths) C++14
100 / 100
235 ms 18904 KB
//NlogN
#include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#define DIM 100005
#define f first
#define s second
using namespace std;
int n, i, x, y, c, k, nrfrunze, root, nr;
int tcost[DIM], pos[DIM], pheap[DIM];
long long sol[DIM], heapmax[DIM], heapmin[DIM], sumk, valh[DIM];
pair<long long, int> lantJos[DIM], lantSus[DIM], lantInit[DIM];
vector< pair<int, int> > v[DIM];
void dfs(int nod, int t) {
    if (v[nod].size() == 1) {
        lantJos[nod] = make_pair(0, nod);
        return;
    }
    for (int i = 0; i < v[nod].size(); i++) {
        int fiu = v[nod][i].f;
        if (fiu == t) {
            continue;
        }
        dfs(fiu, nod);
        tcost[fiu] = v[nod][i].s;
        if (lantJos[nod].f <= lantJos[fiu].f + tcost[fiu]) {
            lantJos[nod].f = lantJos[fiu].f + tcost[fiu];
            lantJos[nod].s = lantJos[fiu].s;
        }
    }
}
void dfs2(int nod, int t) {
    if (nod == root || lantJos[nod].s != lantJos[t].s) {
        lantInit[++nr] = lantJos[nod];
        lantInit[nr].f += tcost[nod];
    }
    int pmax = 0, pmax2 = 0;
    for (int i = 0; i < v[nod].size(); i++) {
        int fiu = v[nod][i].f;
        if (fiu == t) {
            continue;
        }
        if (lantJos[fiu].f + tcost[fiu] >= lantJos[pmax].f + tcost[pmax]) {
            pmax2 = pmax;
            pmax = fiu;
        }
        else if (lantJos[fiu].f + tcost[fiu] >= lantJos[pmax2].f + tcost[pmax2]) {
            pmax2 = fiu;
        }
    }
    for (int i = 0; i < v[nod].size(); i++) {
        int fiu = v[nod][i].f;
        if (fiu == t) {
            continue;
        }
        lantSus[fiu] = make_pair(lantSus[nod].f + tcost[fiu], lantSus[nod].s);
        if (pmax != fiu) {
            if (lantSus[fiu].f <= lantJos[pmax].f + tcost[pmax] + tcost[fiu]) {
                lantSus[fiu].f = lantJos[pmax].f + tcost[pmax] + tcost[fiu];
                lantSus[fiu].s = lantJos[pmax].s;
            }
        }
        else {
            if (lantSus[fiu].f <= lantJos[pmax2].f + tcost[pmax2] + tcost[fiu]) {
                lantSus[fiu].f = lantJos[pmax2].f + tcost[pmax2] + tcost[fiu];
                lantSus[fiu].s = lantJos[pmax2].s;
            }
        }
        dfs2(fiu, nod);
    }
}
void urcmin(int poz) {
    int c = poz, p = c / 2;
    while (p > 0 && valh[ heapmin[c] ] < valh[ heapmin[p] ]) {
        swap(heapmin[c], heapmin[p]);
        pos[ heapmin[c] ] = c;
        pos[ heapmin[p] ] = p;
        c = p;
        p /= 2;
    }
}
void cobmin(int poz) {
    int p = poz, c = 2 * p;
    while (c <= k) {
        if (c + 1 <= k && valh[ heapmin[c + 1] ] < valh[ heapmin[c] ]) {
            c++;
        }
        if (valh[ heapmin[c] ] < valh[ heapmin[p] ]) {
            swap(heapmin[c], heapmin[p]);
            pos[ heapmin[c] ] = c;
            pos[ heapmin[p] ] = p;
            p = c;
            c *= 2;
        }
        else {
            break;
        }
    }
}
void urcmax(int poz) {
    int c = poz, p = c / 2;
    while (p > 0 && valh[ heapmax[c] ] > valh[ heapmax[p] ]) {
        swap(heapmax[c], heapmax[p]);
        pos[ heapmax[c] ] = c;
        pos[ heapmax[p] ] = p;
        c = p;
        p /= 2;
    }
}
void cobmax(int poz) {
    int p = poz, c = 2 * p;
    while (c <= nrfrunze - k) {
        if (c + 1 <= nrfrunze - k && valh[ heapmax[c + 1] ] > valh[ heapmax[c] ]) {
            c++;
        }
        if (valh[ heapmax[c] ] > valh[ heapmax[p] ]) {
            swap(heapmax[c], heapmax[p]);
            pos[ heapmax[c] ] = c;
            pos[ heapmax[p] ] = p;
            p = c;
            c *= 2;
        }
        else {
            break;
        }
    }
}
void inloc(int nod, long long val) {
    if (pheap[nod] == 0) {
        sumk += val - valh[nod];
        valh[nod] = val;
        cobmin(pos[nod]);
        urcmin(pos[nod]);
    }
    else {
        valh[nod] = val;
        cobmax(pos[nod]);
        urcmax(pos[nod]);
    }
    if (valh[heapmin[1]] < valh[heapmax[1]]) {
        sumk += valh[heapmax[1]] - valh[heapmin[1]];
        pheap[ heapmin[1] ] = 1;
        pheap[ heapmax[1] ] = 0;
        swap(heapmin[1], heapmax[1]);
        cobmin(1);
        cobmax(1);
    }
}
void dfs3(int nod, int t) {
    sol[nod] = sumk;
    for (int i = 0; i < v[nod].size(); i++) {
        int fiu = v[nod][i].f;
        if (fiu == t) {
            continue;
        }
        inloc(lantJos[fiu].s, lantJos[fiu].f);
        inloc(lantSus[fiu].s, lantSus[fiu].f);
        dfs3(fiu, nod);
        inloc(lantJos[fiu].s, lantJos[fiu].f + tcost[fiu]);
        inloc(lantSus[fiu].s, lantSus[fiu].f - tcost[fiu]);
    }
}
int main(){
    cin>> n >> k;
    for (i = 1; i < n; i++) {
        cin>> x >> y >> c;
        v[x].push_back(make_pair(y, c));
        v[y].push_back(make_pair(x, c));
    }
    for (i = 1; i <= n; i++) {
        if (v[i].size() == 1) {
            nrfrunze ++;
            if (nrfrunze <= k) {
                pos[i] = nrfrunze;
                heapmin[nrfrunze] = i;
                pheap[i] = 0;
            }
            else {
                pos[i] = nrfrunze - k;
                heapmax[nrfrunze - k] = i;
                pheap[i] = 1;
            }
        }
        else {
            if (root == 0) {
                root = i;
            }
        }
    }
    dfs(root, 0);
    dfs2(root, 0);
    for (i = 1; i <= nrfrunze; i++) {
        inloc(lantInit[i].s, lantInit[i].f);
    }
    dfs3(root, 0);
    for (i = 1; i <= n; i++) {
        cout<< sol[i] <<"\n";
    }
}

Compilation message

Main.cpp: In function 'void dfs(int, int)':
Main.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs2(int, int)':
Main.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs3(int, int)':
Main.cpp:152:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2836 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2800 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2836 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2800 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 4 ms 2900 KB Output is correct
14 Correct 4 ms 3056 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 4 ms 2928 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 16692 KB Output is correct
2 Correct 174 ms 18512 KB Output is correct
3 Correct 129 ms 15308 KB Output is correct
4 Correct 170 ms 16768 KB Output is correct
5 Correct 184 ms 17568 KB Output is correct
6 Correct 172 ms 16688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 3 ms 2836 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2800 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 4 ms 2900 KB Output is correct
14 Correct 4 ms 3056 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 4 ms 2928 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
20 Correct 178 ms 16692 KB Output is correct
21 Correct 174 ms 18512 KB Output is correct
22 Correct 129 ms 15308 KB Output is correct
23 Correct 170 ms 16768 KB Output is correct
24 Correct 184 ms 17568 KB Output is correct
25 Correct 172 ms 16688 KB Output is correct
26 Correct 202 ms 17000 KB Output is correct
27 Correct 187 ms 18400 KB Output is correct
28 Correct 235 ms 18904 KB Output is correct
29 Correct 136 ms 15508 KB Output is correct
30 Correct 199 ms 17108 KB Output is correct
31 Correct 149 ms 15820 KB Output is correct
32 Correct 200 ms 17996 KB Output is correct
33 Correct 184 ms 17056 KB Output is correct
34 Correct 137 ms 15292 KB Output is correct
35 Correct 186 ms 16992 KB Output is correct
36 Correct 181 ms 18592 KB Output is correct