CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:jusola
Submission time:2021-10-12 21:32:45 +0300
Language:C++ (C++11)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.60 s1, 2, 3details
#2--2, 3details
#3--3details

Code

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

#define ll long long

using namespace std;


void s2(int ov, int v, vector<vector<pair<int, int>>> g, set<pair<int, int>> &cr, int msp, int &res, set<int> &vis2) {
    if (find(cr.begin(), cr.end(), pair<int,int>(v,ov)) == cr.end() && find(cr.begin(), cr.end(), pair<int,int>(ov,v)) == cr.end() && ov != v){
        res += msp;
        //cout << ov << " to " << v << " s " << msp << " tot " << res << "\n";
        cr.insert({ov, v});   
    }
    for (auto p: g[v]) {
        int u = p.first;
        int sp = p.second;
        if(find(vis2.begin(), vis2.end(), u) == vis2.end() && ov != u){
            vis2.insert(u);
            int nmsp = msp;
            if(sp < nmsp || msp == -1) {
                nmsp = sp;
            }
            s2(ov, u, g, cr, nmsp, res, vis2);
        }
    }
}


void s(int v, vector<vector<pair<int, int>>> g, set<pair<int, int>> &cr, int &res, set<int> &vis){
    set<int> vis2;
    s2(v, v, g, cr, -1, res, vis2);
    for (auto p: g[v]) {
        int u = p.first;
        if(find(vis.begin(), vis.end(), u) == vis.end()){
            vis.insert(u);
            s(u, g, cr, res, vis);
        }
    }
}

int main() {
    int n;
    cin >> n;
    vector<vector<pair<int, int>>> g(n+1, vector<pair<int, int>>());

    for(int i=0; i<(n-1); i++){
        int a,b,sp;
        cin >> a >> b >> sp;
        g[a].push_back({b,sp});
        g[b].push_back({a,sp});
    }



    //for(int si = 1; si <= n; si++){
        set<pair<int, int>> cr;
        set<int> vis;
        int res = 0;
        s(1, g, cr, res, vis);
        cout << res << "\n";
    //}

    

    return 0;

}

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)