Submission #2148281
Source Code Expand
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,ll> Q; typedef pair<ll,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) vector<Q>G[200005]; bool rem[200005]={}; int sub[200005]={},n,q; vector<P1>edge; ll a[200005]; int make(int v,int u) { int cnt=1; for(int i=0;i<G[v].size();i++) { int v2=G[v][i].fi; if(rem[v2] || v2==u) continue; cnt+=make(v2,v); } return sub[v]=cnt; } P search(int v,int u,int siz) { //size and index P res=mp(INF,-1); int s=1,m=0; for(int i=0;i<G[v].size();i++) { int v2=G[v][i].fi; if(rem[v2] || v2==u) continue; res=min(res,search(v2,v,siz)); m=max(m,sub[v2]); s+=sub[v2]; } m=max(m,siz-s); res=min(res,mp(m,v)); return res; } vector<int>L; pair<ll,int>p; void dfs(int v,int u,ll d) { p = min(p,mp(d+a[v],v)); for(int i=0;i<G[v].size();i++) { int v2=G[v][i].fi; if(rem[v2] || v2==u) continue; dfs(v2,v,d+G[v][i].sc); } } void dfs2(int v,int u,ll d) { edge.pb(mp(d+a[v]+p.fi,mp(v,p.sc))); for(int i=0;i<G[v].size();i++) { int v2=G[v][i].fi; if(rem[v2] || v2==u) continue; dfs2(v2,v,d+G[v][i].sc); } } void sol(int v) { make(v,-1); int sz = sub[v]; int cent=search(v,-1,sub[v]).sc; rem[cent]=true; L.clear(); L.pb(cent); p = mp(a[cent],cent); for(int i=0;i<G[cent].size();i++){ if(!rem[G[cent][i].fi]) dfs(G[cent][i].fi,cent,G[cent][i].sc); } edge.pb(mp(a[cent]+p.fi,mp(cent,p.sc))); for(int i=0;i<G[cent].size();i++){ if(!rem[G[cent][i].fi]) dfs2(G[cent][i].fi,cent,G[cent][i].sc); } for(int i=0;i<G[cent].size();i++) if(!rem[G[cent][i].fi]) sol(G[cent][i].fi); rem[cent]=false; } struct uf{ int par[200005]; void init(){ rep(i,200005) par[i]=i; } int find(int x){ if(x==par[x]) return x; else return par[x] = find(par[x]); } void unite(int x,int y){ x = find(x); y = find(y); if(x == y) return; par[x] = y; } bool same(int x,int y){ return find(x) == find(y); } }uf; int main(){ scanf("%d",&n); rep(i,n){ scanf("%lld",&a[i]); } rep(i,n-1){ int a,b; ll c; scanf("%d%d%lld",&a,&b,&c); a--; b--; G[a].pb(mp(b,c)); G[b].pb(mp(a,c)); } sol(0); ll ans = 0; SORT(edge); uf.init(); rep(i,edge.size()){ ll v = edge[i].fi; int p = edge[i].sc.fi, q = edge[i].sc.sc; if(uf.same(p,q)) continue; uf.unite(p,q); ans+=v; } cout<<ans<<endl; }
Submission Info
Submission Time | |
---|---|
Task | J - Tree MST |
User | IH19980412 |
Language | C++14 (GCC 5.4.1) |
Score | 1900 |
Code Size | 3188 Byte |
Status | AC |
Exec Time | 949 ms |
Memory | 110560 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:106:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); ^ ./Main.cpp:108:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%lld",&a[i]); ^ ./Main.cpp:111:50: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] int a,b; ll c; scanf("%d%d%lld",&a,&b,&c); a--; b--; ^
Judge Result
Set Name | sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1900 / 1900 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt, sample-03.txt |
All | sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt, 01-57.txt, 01-58.txt, 01-59.txt, 01-60.txt, 01-61.txt, 01-62.txt, 01-63.txt, 01-64.txt, 01-65.txt, 01-66.txt, 01-67.txt, 01-68.txt, 01-69.txt, 01-70.txt, 01-71.txt, 01-72.txt, 01-73.txt, 01-74.txt, 01-75.txt, 01-76.txt, 01-77.txt, 01-78.txt, 01-79.txt, 01-80.txt, 01-81.txt, 01-82.txt, 01-83.txt, 01-84.txt, 01-85.txt, 01-86.txt, 01-87.txt, 01-88.txt, 01-89.txt, 01-90.txt, sample-01.txt, sample-02.txt, sample-03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | AC | 3 ms | 7040 KB |
01-02.txt | AC | 3 ms | 7040 KB |
01-03.txt | AC | 4 ms | 7040 KB |
01-04.txt | AC | 27 ms | 9200 KB |
01-05.txt | AC | 813 ms | 83296 KB |
01-06.txt | AC | 808 ms | 83936 KB |
01-07.txt | AC | 815 ms | 85344 KB |
01-08.txt | AC | 819 ms | 85216 KB |
01-09.txt | AC | 797 ms | 84448 KB |
01-10.txt | AC | 816 ms | 83552 KB |
01-11.txt | AC | 817 ms | 84320 KB |
01-12.txt | AC | 802 ms | 84320 KB |
01-13.txt | AC | 812 ms | 83424 KB |
01-14.txt | AC | 842 ms | 84320 KB |
01-15.txt | AC | 833 ms | 85216 KB |
01-16.txt | AC | 804 ms | 84192 KB |
01-17.txt | AC | 815 ms | 84576 KB |
01-18.txt | AC | 802 ms | 83808 KB |
01-19.txt | AC | 785 ms | 85216 KB |
01-20.txt | AC | 798 ms | 86240 KB |
01-21.txt | AC | 797 ms | 86752 KB |
01-22.txt | AC | 815 ms | 86496 KB |
01-23.txt | AC | 810 ms | 86752 KB |
01-24.txt | AC | 807 ms | 86752 KB |
01-25.txt | AC | 820 ms | 87648 KB |
01-26.txt | AC | 820 ms | 86624 KB |
01-27.txt | AC | 832 ms | 87136 KB |
01-28.txt | AC | 829 ms | 86876 KB |
01-29.txt | AC | 846 ms | 87004 KB |
01-30.txt | AC | 275 ms | 35944 KB |
01-31.txt | AC | 283 ms | 36204 KB |
01-32.txt | AC | 269 ms | 36844 KB |
01-33.txt | AC | 276 ms | 36200 KB |
01-34.txt | AC | 274 ms | 36712 KB |
01-35.txt | AC | 891 ms | 108768 KB |
01-36.txt | AC | 901 ms | 109664 KB |
01-37.txt | AC | 912 ms | 109792 KB |
01-38.txt | AC | 923 ms | 110560 KB |
01-39.txt | AC | 927 ms | 110432 KB |
01-40.txt | AC | 938 ms | 110432 KB |
01-41.txt | AC | 173 ms | 28124 KB |
01-42.txt | AC | 170 ms | 26604 KB |
01-43.txt | AC | 170 ms | 26988 KB |
01-44.txt | AC | 170 ms | 25324 KB |
01-45.txt | AC | 900 ms | 85088 KB |
01-46.txt | AC | 920 ms | 86112 KB |
01-47.txt | AC | 915 ms | 85216 KB |
01-48.txt | AC | 941 ms | 109024 KB |
01-49.txt | AC | 941 ms | 110432 KB |
01-50.txt | AC | 942 ms | 109024 KB |
01-51.txt | AC | 949 ms | 109024 KB |
01-52.txt | AC | 938 ms | 109920 KB |
01-53.txt | AC | 934 ms | 110432 KB |
01-54.txt | AC | 945 ms | 109280 KB |
01-55.txt | AC | 684 ms | 84836 KB |
01-56.txt | AC | 679 ms | 85220 KB |
01-57.txt | AC | 653 ms | 84704 KB |
01-58.txt | AC | 735 ms | 83172 KB |
01-59.txt | AC | 705 ms | 84192 KB |
01-60.txt | AC | 611 ms | 83808 KB |
01-61.txt | AC | 643 ms | 83172 KB |
01-62.txt | AC | 654 ms | 84576 KB |
01-63.txt | AC | 658 ms | 83684 KB |
01-64.txt | AC | 734 ms | 87140 KB |
01-65.txt | AC | 752 ms | 87136 KB |
01-66.txt | AC | 732 ms | 87136 KB |
01-67.txt | AC | 647 ms | 84452 KB |
01-68.txt | AC | 654 ms | 84064 KB |
01-69.txt | AC | 667 ms | 84452 KB |
01-70.txt | AC | 671 ms | 87268 KB |
01-71.txt | AC | 668 ms | 88420 KB |
01-72.txt | AC | 649 ms | 89312 KB |
01-73.txt | AC | 552 ms | 84448 KB |
01-74.txt | AC | 541 ms | 84448 KB |
01-75.txt | AC | 524 ms | 83940 KB |
01-76.txt | AC | 573 ms | 84580 KB |
01-77.txt | AC | 615 ms | 85216 KB |
01-78.txt | AC | 592 ms | 84708 KB |
01-79.txt | AC | 540 ms | 84064 KB |
01-80.txt | AC | 564 ms | 85216 KB |
01-81.txt | AC | 576 ms | 85728 KB |
01-82.txt | AC | 602 ms | 88036 KB |
01-83.txt | AC | 641 ms | 87136 KB |
01-84.txt | AC | 626 ms | 88292 KB |
01-85.txt | AC | 554 ms | 84964 KB |
01-86.txt | AC | 565 ms | 85600 KB |
01-87.txt | AC | 558 ms | 85476 KB |
01-88.txt | AC | 652 ms | 89824 KB |
01-89.txt | AC | 618 ms | 88676 KB |
01-90.txt | AC | 605 ms | 87012 KB |
sample-01.txt | AC | 3 ms | 7040 KB |
sample-02.txt | AC | 4 ms | 7040 KB |
sample-03.txt | AC | 4 ms | 7040 KB |