Submission #2510220
Source Code Expand
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; typedef long long ll; struct edge{ int x,y; ll v; edge(int a=0,int b=0,ll c=0){x=a;y=b;v=c;} }; bool operator<(edge a,edge b){return a.v<b.v;} vector<edge>e; int h[200010],nex[400010],to[400010],w[400010],v[200010],M; void add(int a,int b,int c){ M++; to[M]=b; w[M]=c; nex[M]=h[a]; h[a]=M; } bool vis[200010]; int siz[200010],n; void dfs1(int fa,int x){ siz[x]=1; n++; for(int i=h[x];i;i=nex[i]){ if(!vis[to[i]]&&to[i]!=fa){ dfs1(x,to[i]); siz[x]+=siz[to[i]]; } } } int mn,cn; ll d[200010]; void dfs2(int fa,int x){ int i,k; k=0; for(i=h[x];i;i=nex[i]){ if(!vis[to[i]]&&to[i]!=fa){ dfs2(x,to[i]); k=max(k,siz[to[i]]); } } k=max(k,n-siz[x]); if(k<mn){ mn=k; cn=x; } } vector<int>q; void dfs3(int fa,int x){ q.push_back(x); for(int i=h[x];i;i=nex[i]){ if(!vis[to[i]]&&to[i]!=fa){ d[to[i]]=d[x]+w[i]; dfs3(x,to[i]); } } } void solve(int x){ int i,t; ll mn; n=0; dfs1(0,x); ::mn=2147483647; dfs2(0,x); x=cn; q.clear(); d[x]=0; dfs3(0,x); mn=9223372036854775807ll; for(int x:q){ d[x]+=v[x]; if(d[x]<mn){ mn=d[x]; t=x; } } for(int x:q)e.push_back(edge(t,x,d[t]+d[x])); vis[x]=1; for(i=h[x];i;i=nex[i]){ if(!vis[to[i]])solve(to[i]); } } int fa[200010]; int get(int x){return fa[x]==x?x:(fa[x]=get(fa[x]));} int main(){ int n,i,a,b,c; ll ans; scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",v+i); for(i=1;i<n;i++){ scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } solve(1); sort(e.begin(),e.end()); for(i=1;i<=n;i++)fa[i]=i; ans=0; for(edge t:e){ a=get(t.x); b=get(t.y); if(a!=b){ fa[a]=b; ans+=t.v; } } printf("%lld",ans); }
Submission Info
Submission Time | |
---|---|
Task | J - Tree MST |
User | jefflyy |
Language | C++14 (GCC 5.4.1) |
Score | 1900 |
Code Size | 1821 Byte |
Status | AC |
Exec Time | 788 ms |
Memory | 86624 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:90:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); ^ ./Main.cpp:91:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] for(i=1;i<=n;i++)scanf("%d",v+i); ^ ./Main.cpp:93:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d%d",&a,&b,&c); ^
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 | 2 ms | 6400 KB |
01-02.txt | AC | 2 ms | 6400 KB |
01-03.txt | AC | 2 ms | 6400 KB |
01-04.txt | AC | 22 ms | 8760 KB |
01-05.txt | AC | 731 ms | 76640 KB |
01-06.txt | AC | 644 ms | 77024 KB |
01-07.txt | AC | 655 ms | 75744 KB |
01-08.txt | AC | 656 ms | 75616 KB |
01-09.txt | AC | 666 ms | 76128 KB |
01-10.txt | AC | 688 ms | 77152 KB |
01-11.txt | AC | 721 ms | 77280 KB |
01-12.txt | AC | 706 ms | 75488 KB |
01-13.txt | AC | 720 ms | 75744 KB |
01-14.txt | AC | 735 ms | 75616 KB |
01-15.txt | AC | 738 ms | 76384 KB |
01-16.txt | AC | 715 ms | 76896 KB |
01-17.txt | AC | 718 ms | 76000 KB |
01-18.txt | AC | 697 ms | 76256 KB |
01-19.txt | AC | 687 ms | 77408 KB |
01-20.txt | AC | 643 ms | 78176 KB |
01-21.txt | AC | 627 ms | 77280 KB |
01-22.txt | AC | 661 ms | 77920 KB |
01-23.txt | AC | 673 ms | 78048 KB |
01-24.txt | AC | 686 ms | 76640 KB |
01-25.txt | AC | 699 ms | 76896 KB |
01-26.txt | AC | 699 ms | 78176 KB |
01-27.txt | AC | 726 ms | 77152 KB |
01-28.txt | AC | 740 ms | 78176 KB |
01-29.txt | AC | 736 ms | 77280 KB |
01-30.txt | AC | 233 ms | 26856 KB |
01-31.txt | AC | 239 ms | 27496 KB |
01-32.txt | AC | 239 ms | 28136 KB |
01-33.txt | AC | 235 ms | 27240 KB |
01-34.txt | AC | 228 ms | 27112 KB |
01-35.txt | AC | 685 ms | 85088 KB |
01-36.txt | AC | 689 ms | 86240 KB |
01-37.txt | AC | 707 ms | 86496 KB |
01-38.txt | AC | 735 ms | 86496 KB |
01-39.txt | AC | 764 ms | 86368 KB |
01-40.txt | AC | 788 ms | 85344 KB |
01-41.txt | AC | 138 ms | 19052 KB |
01-42.txt | AC | 139 ms | 19692 KB |
01-43.txt | AC | 139 ms | 19692 KB |
01-44.txt | AC | 138 ms | 19180 KB |
01-45.txt | AC | 688 ms | 76000 KB |
01-46.txt | AC | 705 ms | 76640 KB |
01-47.txt | AC | 702 ms | 77280 KB |
01-48.txt | AC | 618 ms | 86496 KB |
01-49.txt | AC | 617 ms | 86368 KB |
01-50.txt | AC | 611 ms | 85984 KB |
01-51.txt | AC | 604 ms | 86624 KB |
01-52.txt | AC | 658 ms | 85216 KB |
01-53.txt | AC | 646 ms | 85600 KB |
01-54.txt | AC | 634 ms | 86496 KB |
01-55.txt | AC | 548 ms | 77408 KB |
01-56.txt | AC | 533 ms | 77152 KB |
01-57.txt | AC | 488 ms | 76128 KB |
01-58.txt | AC | 554 ms | 77408 KB |
01-59.txt | AC | 577 ms | 77408 KB |
01-60.txt | AC | 470 ms | 75744 KB |
01-61.txt | AC | 499 ms | 76768 KB |
01-62.txt | AC | 508 ms | 76768 KB |
01-63.txt | AC | 498 ms | 76896 KB |
01-64.txt | AC | 576 ms | 78432 KB |
01-65.txt | AC | 569 ms | 78560 KB |
01-66.txt | AC | 574 ms | 77920 KB |
01-67.txt | AC | 503 ms | 76896 KB |
01-68.txt | AC | 503 ms | 77408 KB |
01-69.txt | AC | 512 ms | 75616 KB |
01-70.txt | AC | 520 ms | 77792 KB |
01-71.txt | AC | 514 ms | 78560 KB |
01-72.txt | AC | 497 ms | 78432 KB |
01-73.txt | AC | 439 ms | 77408 KB |
01-74.txt | AC | 423 ms | 76768 KB |
01-75.txt | AC | 413 ms | 75872 KB |
01-76.txt | AC | 460 ms | 75872 KB |
01-77.txt | AC | 487 ms | 76256 KB |
01-78.txt | AC | 462 ms | 76640 KB |
01-79.txt | AC | 430 ms | 77536 KB |
01-80.txt | AC | 451 ms | 76256 KB |
01-81.txt | AC | 448 ms | 76896 KB |
01-82.txt | AC | 477 ms | 78304 KB |
01-83.txt | AC | 511 ms | 78048 KB |
01-84.txt | AC | 483 ms | 78304 KB |
01-85.txt | AC | 430 ms | 77280 KB |
01-86.txt | AC | 443 ms | 77792 KB |
01-87.txt | AC | 434 ms | 77408 KB |
01-88.txt | AC | 510 ms | 78944 KB |
01-89.txt | AC | 473 ms | 78432 KB |
01-90.txt | AC | 480 ms | 78048 KB |
sample-01.txt | AC | 2 ms | 6400 KB |
sample-02.txt | AC | 2 ms | 6400 KB |
sample-03.txt | AC | 2 ms | 6400 KB |