Submission #2666904


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,int> ii;

typedef pair<int,ii> iii;

int n,par[100005];
long long lz[100005];
vector<iii> edge;
long long ans[100005];
vector<int> st[100005];

int findSet(int u){
    if(u == par[u]) return u;
    return par[u] = findSet(par[u]);
}
void UnionSet(int u,int v,int val){
    int pu = findSet(u), pv = findSet(v);
    if(st[pu].size()>st[pv].size()) swap(pu,pv);
    for(int i = 0;i<st[pu].size();i++){
        ans[st[pu][i]]+=val*st[pv].size();
        ans[st[pu][i]]+=lz[pu];
        ans[st[pu][i]]-=val*st[pu].size();
        ans[st[pu][i]]-=lz[pv];
    }
    lz[pu] = 0;
    for(int i = 0;i<st[pu].size();i++)
        st[pv].pb(st[pu][i]);
    lz[pv]+=val*st[pu].size();
    par[pu] = pv;
}

int main(){
    cin.tie(0),ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i<=n;i++){
        par[i] = i;
        st[i].pb(i);
    }
    int u,v,w;
    for(int i = 1;i<=n-1;i++){
        cin >> u >> v >> w;
        edge.pb(mp(w,mp(u,v)));
    }
    sort(edge.begin(),edge.end());
    for(int i = edge.size()-1;i>=0;i--){
        u = edge[i].se.fi; v = edge[i].se.se;
        w = edge[i].fi;
        UnionSet(u,v,w);
    }
    for(int i = 1;i<=n;i++)
        cout<<ans[i]+lz[findSet(i)]<<"\n";
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User Flying_Dragon_02
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1399 Byte
Status AC
Exec Time 71 ms
Memory 14068 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 4352 KB
s1_02.txt AC 3 ms 4352 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 3 ms 4352 KB
s2_08.txt AC 55 ms 10228 KB
s2_09.txt AC 29 ms 7284 KB
s2_10.txt AC 16 ms 6012 KB
s2_11.txt AC 60 ms 10996 KB
s2_12.txt AC 62 ms 11380 KB
s2_13.txt AC 66 ms 13300 KB
s2_14.txt AC 64 ms 13428 KB
s3_15.txt AC 57 ms 10996 KB
s3_16.txt AC 30 ms 7668 KB
s3_17.txt AC 18 ms 6140 KB
s3_18.txt AC 29 ms 7416 KB
s3_19.txt AC 67 ms 11764 KB
s3_20.txt AC 68 ms 12276 KB
s3_21.txt AC 66 ms 11636 KB
s3_22.txt AC 66 ms 11636 KB
s3_23.txt AC 71 ms 14068 KB
s3_24.txt AC 65 ms 13556 KB
s3_25.txt AC 51 ms 11252 KB
s3_26.txt AC 64 ms 13428 KB
s3_27.txt AC 51 ms 11252 KB
s3_28.txt AC 65 ms 13300 KB
s3_29.txt AC 52 ms 11252 KB