CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:mooc.fi~486604
Submission time:2021-10-06 00:22:45 +0300
Language:C++ (C++11)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#20.09 s2, 3details
#3--3details

Code

 
#include <iostream>
#include <vector>
#include <map>

using namespace std; 
int N;
vector<pair<int,int>> v[200100]; 
long long int sum;
int vasen=0; 
int oikea= 0; 
bool tama=false; 
map<pair<int, int>, int> juu;
vector<int> kerro;

void haku(int s, int e, int target){
    vasen++; 
    oikea++;

    for(auto u: v[s]){
        if(u.first!=e&&u.second==target){
            //cout<<"luvut "<<s<<" "<<u.first<<endl;
            
            juu[{s, u.first}]=1;
            int temp1=vasen;
            int temp2= oikea; 
            vasen=0;
            haku(u.first, s, target);
            kerro.push_back(vasen);
            vasen=temp1;
            oikea=temp2;

            tama=true; }
        if(u.first!=e&&u.second>target) haku(u.first, s, target);
}
}
int main()
{
    int i, a,b,c;
    sum=0; 
    cin>>N; 
    vector<vector<int>> di; 
    for(i=0; i<N-1; i++){
        cin>>a>>b>>c;
        di.push_back({c,a,b});
        v[a].push_back({b,c});
        v[b].push_back({a,c});
    }
    for(auto kl: di){
        if(juu[{kl[2],kl[1]}]||juu[{kl[1],kl[2]}])
            continue;
        vasen=0;
        haku(kl[1], kl[2] , kl[0]);
        int kpp=vasen;
        oikea=0;
        haku(kl[2], kl[1], kl[0]);
       // cout<<"arvot"<<kpp<<" "<<oikea<<" "<<tama<<endl;
        juu[{kl[1], kl[2]}]=1;
    
        if(tama){
            kerro.push_back(kpp);
            kerro.push_back(oikea);
            int maara=kerro.size();
            for(int ri=0; ri<maara; ri++){
               // cout<<kerro[ri]<<" ";
                for( int rj=ri; rj<maara; rj++){
                    if(ri==rj) continue;
                    //cout<<"lisaa "<<kl[0]*kerro[ri]*kerro[rj]<<endl;
                    sum+=kl[0]*kerro[ri]*kerro[rj];  }
                     }
            tama=false;
            kerro.clear();
            continue;
        }
        sum+= kl[0]*kpp*oikea;
    }


    cout<<sum;

  
    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
1794100716608

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)