CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko (Network)
Sender:tonero
Submission time:2021-10-16 14:43:40
Language:C++17
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.08 s1, 2, 3details
#2--2, 3details
#30.13 s3details

Code

#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#define ll long long
#define ull unsigned long long

struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return ((ll)v.first) << 32 | (ll)(unsigned int)v.second;
    }
};

vector<pair<int, int>>* vecs[200001] = {};
std::unordered_map<pair<int,int>, int, pair_hash> traveled;

ull cur = 0;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    for (int i = 1; i < n; i++)
    {
        int a,b,x;
        cin >> a >> b >> x;

        if(vecs[a] == nullptr) {
            vecs[a] = new vector<pair<int, int>>();
        }
        if(vecs[b] == nullptr) {
            vecs[b] = new vector<pair<int, int>>();
        }

        vecs[a]->emplace_back(b,x);
        vecs[b]->emplace_back(a,x);
    }

    struct queMem {
        queMem(int id, vector<pair<int,int>> hist, int spd) {
            this->id = id;
            this->hist = std::move(hist);
            this->spd = spd;
        }
        int id;
        vector<pair<int,int>> hist;
        int spd;
    };

    traveled.reserve(n*(n/8));
    bitset<200000> visited = {};

    for (int i = 1; i <= n; i++) {
        //f id, s hist
        queue<queMem> bfs_queue = {};
        visited.reset();
        bfs_queue.push({i, {}, __INT32_MAX__});
        while(!bfs_queue.empty()){
            queMem nxt = bfs_queue.front();
            bfs_queue.pop();
            if(visited.test(nxt.id)) continue;
            visited.set(nxt.id);

            int loopSpd = nxt.spd;
            for (auto iter = nxt.hist.end()-1; iter > nxt.hist.begin()-1 ; --iter) {
                const auto fndIter = traveled.find(make_pair(nxt.id, iter->first));
                if(fndIter == traveled.end()){
                    cur += loopSpd;
                    //traveled
                    traveled.emplace(std::make_pair(nxt.id, iter->first), loopSpd);
                    traveled.emplace(std::make_pair(iter->first, nxt.id), loopSpd);
                    //vecs
                    vecs[nxt.id]->emplace_back(iter->first, loopSpd);
                    vecs[iter->first]->emplace_back(nxt.id, loopSpd);
                }else if(fndIter->second < loopSpd){
                    cur -= fndIter->second;
                    cur += loopSpd;
                    //traveled
                    traveled.emplace(std::make_pair(nxt.id, iter->first), loopSpd);
                    traveled.emplace(std::make_pair(iter->first, nxt.id), loopSpd);
                    //vecs
                    vecs[nxt.id]->emplace_back(iter->first, loopSpd);
                    vecs[iter->first]->emplace_back(nxt.id, loopSpd);
                }
                loopSpd = min(loopSpd, iter->second);
            }

            nxt.hist.emplace_back(nxt.id, nxt.spd);

            for (const auto &item : *vecs[nxt.id]){
                bfs_queue.emplace(item.first, nxt.hist, item.second);
            }
        }
    }

    cout << cur;

    flush(std::cout);
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
100
1 2 74
1 3 100
2 4 50
3 5 40
...

correct output
88687

user output
88687

Test 2

Group: 2, 3

Verdict:

input
5000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1103702320243776

user output
(empty)

Test 3

Group: 3

Verdict:

input
200000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1080549209850010931

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc