Submission #498654

# Submission time Handle Problem Language Result Execution time Memory
498654 2021-12-26T04:44:27 Z model_code Paths (RMI21_paths) C++17
100 / 100
285 ms 16768 KB
// paths_100_alexandra.cpp

//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:22: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]
   22 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs2(int, int)':
Main.cpp:41: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]
   41 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp:54: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]
   54 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs3(int, int)':
Main.cpp:154: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]
  154 |     for (int i = 0; i < v[nod].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 5 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 5 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 6 ms 2892 KB Output is correct
14 Correct 6 ms 2892 KB Output is correct
15 Correct 5 ms 2892 KB Output is correct
16 Correct 4 ms 2892 KB Output is correct
17 Correct 4 ms 2892 KB Output is correct
18 Correct 4 ms 2892 KB Output is correct
19 Correct 4 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 14488 KB Output is correct
2 Correct 273 ms 16384 KB Output is correct
3 Correct 168 ms 13168 KB Output is correct
4 Correct 213 ms 14500 KB Output is correct
5 Correct 186 ms 15340 KB Output is correct
6 Correct 192 ms 14544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 3 ms 2764 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 5 ms 2764 KB Output is correct
12 Correct 3 ms 2764 KB Output is correct
13 Correct 6 ms 2892 KB Output is correct
14 Correct 6 ms 2892 KB Output is correct
15 Correct 5 ms 2892 KB Output is correct
16 Correct 4 ms 2892 KB Output is correct
17 Correct 4 ms 2892 KB Output is correct
18 Correct 4 ms 2892 KB Output is correct
19 Correct 4 ms 2832 KB Output is correct
20 Correct 241 ms 14488 KB Output is correct
21 Correct 273 ms 16384 KB Output is correct
22 Correct 168 ms 13168 KB Output is correct
23 Correct 213 ms 14500 KB Output is correct
24 Correct 186 ms 15340 KB Output is correct
25 Correct 192 ms 14544 KB Output is correct
26 Correct 209 ms 14972 KB Output is correct
27 Correct 203 ms 16360 KB Output is correct
28 Correct 228 ms 16768 KB Output is correct
29 Correct 170 ms 13320 KB Output is correct
30 Correct 221 ms 14872 KB Output is correct
31 Correct 203 ms 14268 KB Output is correct
32 Correct 264 ms 15992 KB Output is correct
33 Correct 285 ms 14952 KB Output is correct
34 Correct 152 ms 13204 KB Output is correct
35 Correct 256 ms 14884 KB Output is correct
36 Correct 203 ms 16424 KB Output is correct