Submission #2666694


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N=1e5+5;
int n,pset[N],lazy[N],ans[N];
vector<int> child[N];
vector< pair<int, pair<int,int> > > edge;

void joinset(int u, int v, int w){
    if(child[ pset[u] ].size() > child[ pset[v] ].size()) swap(u, v);
    int pau = pset[u];
    lazy[ pset[v] ] += w * (child[ pset[u] ].size());
    for(int cu : child[ pset[u] ]){
        ans[cu] += lazy[ pset[u] ] + w * child[ pset[v] ].size() - lazy[ pset[v] ];
    }
    for(int cu : child[ pset[u] ]) child[ pset[v] ].push_back(cu), pset[cu] = pset[v];
    child[ pau ].clear();
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<n;++i){
        int u,v,w; cin>>u>>v>>w;
        edge.push_back(make_pair(w,make_pair(u,v)));
    }
    sort(edge.begin(), edge.end());

    for(int i=1;i<=n; ++i) pset[i] = i, child[i].push_back(i);

    for(int i = edge.size()-1; i >= 0; --i){
        joinset(edge[i].second.first, edge[i].second.second, edge[i].first);
    }

    for(int i = 1; i <= n; ++i) cout << ans[i] + lazy[ pset[i] ] << "\n";

    return 0;
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User vjudge3
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1126 Byte
Status AC
Exec Time 79 ms
Memory 19696 KB

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 200 / 200 200 / 200 400 / 400
Status
AC × 3
AC × 10
AC × 9
AC × 32
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt
Subtask2 00_example_02.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 3 ms 4352 KB
00_example_02.txt AC 3 ms 4352 KB
00_example_03.txt AC 3 ms 4352 KB
s1_01.txt AC 3 ms 4480 KB
s1_02.txt AC 3 ms 4480 KB
s1_03.txt AC 3 ms 4352 KB
s1_04.txt AC 3 ms 4480 KB
s1_05.txt AC 3 ms 4480 KB
s1_06.txt AC 3 ms 4480 KB
s1_07.txt AC 2 ms 4352 KB
s2_08.txt AC 53 ms 13296 KB
s2_09.txt AC 28 ms 8948 KB
s2_10.txt AC 16 ms 6648 KB
s2_11.txt AC 61 ms 14064 KB
s2_12.txt AC 65 ms 15216 KB
s2_13.txt AC 73 ms 19440 KB
s2_14.txt AC 72 ms 19184 KB
s3_15.txt AC 60 ms 13936 KB
s3_16.txt AC 31 ms 9204 KB
s3_17.txt AC 18 ms 6904 KB
s3_18.txt AC 30 ms 8820 KB
s3_19.txt AC 69 ms 15088 KB
s3_20.txt AC 74 ms 16752 KB
s3_21.txt AC 68 ms 14960 KB
s3_22.txt AC 68 ms 14704 KB
s3_23.txt AC 79 ms 19696 KB
s3_24.txt AC 72 ms 18672 KB
s3_25.txt AC 53 ms 13680 KB
s3_26.txt AC 71 ms 18288 KB
s3_27.txt AC 53 ms 13936 KB
s3_28.txt AC 71 ms 18160 KB
s3_29.txt AC 54 ms 13936 KB