Submission #2169999


Source Code Expand

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


struct QuickFind{
  Int n;
  vector<Int> r,p,dat,laz;
  vector<vector<Int> > v;
  QuickFind(){}
  QuickFind(Int sz):n(sz),r(sz),p(sz),dat(n,0),laz(n,0),v(sz){
    for(Int i=0;i<n;i++){
      r[i]=1,p[i]=i;
      v[i].resize(1,i);
    }
  }
  bool same(Int x,Int y){
    return p[x]==p[y];
  }
  void unite(Int x,Int y,Int w){
    x=p[x];y=p[y];
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    //cout<<x<<" "<<r[x]<<":"<<y<<" "<<r[y]<<":"<<w<<endl;
    for(Int i=0;i<(Int)v[y].size();i++){
      p[v[y][i]]=x;
      v[x].push_back(v[y][i]);
      dat[v[y][i]]-=laz[x];
      dat[v[y][i]]+=laz[y];
      dat[v[y][i]]+=(r[x]-r[y])*w;
    }
    laz[x]+=r[y]*w;
    r[x]+=r[y];
    v[y].clear();
    laz[y]=0;
  }
  Int get(Int x){
    //cout<<x<<" "<<dat[x]<<" "<<laz[p[x]]<<endl;
    return dat[x]+laz[p[x]];
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  using T = tuple<Int, Int, Int>;
  vector<T> v;
  for(Int i=1;i<n;i++){
    Int a,b,c;
    cin>>a>>b>>c;
    v.emplace_back(c,--a,--b);
  }
  sort(v.rbegin(),v.rend());
  QuickFind uf(n);
  for(Int i=1;i<n;i++){
    Int c,a,b;
    tie(c,a,b)=v[i-1];
    uf.unite(a,b,c);
  }
  for(Int i=0;i<n;i++) cout<<uf.get(i)<<endl;
  return 0;
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User beet
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1343 Byte
Status AC
Exec Time 309 ms
Memory 20464 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 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
s1_01.txt AC 3 ms 384 KB
s1_02.txt AC 3 ms 384 KB
s1_03.txt AC 2 ms 256 KB
s1_04.txt AC 4 ms 384 KB
s1_05.txt AC 4 ms 384 KB
s1_06.txt AC 4 ms 384 KB
s1_07.txt AC 1 ms 256 KB
s2_08.txt AC 238 ms 13680 KB
s2_09.txt AC 114 ms 7028 KB
s2_10.txt AC 66 ms 3960 KB
s2_11.txt AC 269 ms 15344 KB
s2_12.txt AC 270 ms 16112 KB
s2_13.txt AC 293 ms 19824 KB
s2_14.txt AC 278 ms 19952 KB
s3_15.txt AC 265 ms 14064 KB
s3_16.txt AC 128 ms 7284 KB
s3_17.txt AC 73 ms 4088 KB
s3_18.txt AC 127 ms 6900 KB
s3_19.txt AC 293 ms 15984 KB
s3_20.txt AC 309 ms 16752 KB
s3_21.txt AC 302 ms 15600 KB
s3_22.txt AC 292 ms 15856 KB
s3_23.txt AC 297 ms 20464 KB
s3_24.txt AC 303 ms 19440 KB
s3_25.txt AC 287 ms 14448 KB
s3_26.txt AC 299 ms 19056 KB
s3_27.txt AC 276 ms 14832 KB
s3_28.txt AC 289 ms 18800 KB
s3_29.txt AC 278 ms 14576 KB