Submission #1804911
Source Code Expand
#include <bits/stdc++.h> using namespace std; typedef long long lint; typedef long double llf; typedef pair<lint, int> pi; const int MAXN = 200005; struct disj{ int pa[MAXN]; void init(int n){ iota(pa, pa + n + 1, 0); } int find(int x){ return pa[x] = (pa[x] == x ? x : find(pa[x])); } bool uni(int p, int q){ p = find(p); q = find(q); if(p == q) return 0; pa[q] = p; return 1; } }disj; struct edg{ int s, e; lint x; bool operator<(const edg &ee)const{ return x < ee.x; } }; int n, a[MAXN], proc[MAXN]; int sz[MAXN], msz[MAXN]; vector<edg> ans; vector<pi> gph[MAXN]; vector<int> cdfn; void dfs(int x, int p){ cdfn.push_back(x); sz[x] = 1; msz[x] = 0; for(auto &i : gph[x]){ if(!proc[i.second] && i.second != p){ dfs(i.second, x); sz[x] += sz[i.second]; msz[x] = max(msz[x], sz[i.second]); } } } int get_center(int x){ cdfn.clear(); dfs(x, -1); pi ans(1e9, x); for(auto &i : cdfn){ int w = max(msz[i], sz[x] - sz[i]); ans = min(ans, pi(w, i)); } return ans.second; } vector<pi> dfn; lint dis[MAXN]; void dfs2(int x, int p){ dfn.push_back(pi(a[x] + dis[x], x)); for(auto &i : gph[x]){ if(proc[i.second] || i.second == p) continue; dis[i.second] = dis[x] + i.first; dfs2(i.second, x); } } int main(){ scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); for(int i=1; i<n; i++){ int s, e, x; scanf("%d %d %d",&s,&e,&x); gph[s].push_back(pi(x, e)); gph[e].push_back(pi(x, s)); } queue<int> que; que.push(1); while(!que.empty()){ int x = que.front(); que.pop(); x = get_center(x); dfn.clear(); dis[x] = 0; dfs2(x, -1); sort(dfn.begin(), dfn.end()); for(int i=1; i<dfn.size(); i++){ ans.push_back({dfn[0].second, dfn[i].second, dfn[0].first + dfn[i].first}); } proc[x] = 1; for(auto &i : gph[x]){ if(!proc[i.second]){ que.push(i.second); } } } sort(ans.begin(), ans.end()); disj.init(n); lint dap = 0; for(auto &i : ans){ if(disj.uni(i.s, i.e)) dap += i.x; } cout << dap << endl; }
Submission Info
Submission Time | |
---|---|
Task | J - Tree MST |
User | koosaga |
Language | C++14 (GCC 5.4.1) |
Score | 1900 |
Code Size | 2129 Byte |
Status | AC |
Exec Time | 1069 ms |
Memory | 101856 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:75:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); ^ ./Main.cpp:76:43: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] for(int i=1; i<=n; i++) scanf("%d",&a[i]); ^ ./Main.cpp:79:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d",&s,&e,&x); ^
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 | 4 ms | 6912 KB |
01-02.txt | AC | 3 ms | 6912 KB |
01-03.txt | AC | 4 ms | 6912 KB |
01-04.txt | AC | 25 ms | 9908 KB |
01-05.txt | AC | 822 ms | 90592 KB |
01-06.txt | AC | 795 ms | 89824 KB |
01-07.txt | AC | 843 ms | 89940 KB |
01-08.txt | AC | 875 ms | 92128 KB |
01-09.txt | AC | 787 ms | 90336 KB |
01-10.txt | AC | 817 ms | 90848 KB |
01-11.txt | AC | 892 ms | 91360 KB |
01-12.txt | AC | 792 ms | 91360 KB |
01-13.txt | AC | 818 ms | 89696 KB |
01-14.txt | AC | 847 ms | 90080 KB |
01-15.txt | AC | 821 ms | 91744 KB |
01-16.txt | AC | 874 ms | 91104 KB |
01-17.txt | AC | 905 ms | 91872 KB |
01-18.txt | AC | 775 ms | 89952 KB |
01-19.txt | AC | 786 ms | 90464 KB |
01-20.txt | AC | 798 ms | 92256 KB |
01-21.txt | AC | 824 ms | 91488 KB |
01-22.txt | AC | 800 ms | 90464 KB |
01-23.txt | AC | 799 ms | 90848 KB |
01-24.txt | AC | 812 ms | 92896 KB |
01-25.txt | AC | 821 ms | 92384 KB |
01-26.txt | AC | 833 ms | 93152 KB |
01-27.txt | AC | 858 ms | 92000 KB |
01-28.txt | AC | 855 ms | 92640 KB |
01-29.txt | AC | 847 ms | 92768 KB |
01-30.txt | AC | 266 ms | 44136 KB |
01-31.txt | AC | 277 ms | 42220 KB |
01-32.txt | AC | 263 ms | 41580 KB |
01-33.txt | AC | 276 ms | 44008 KB |
01-34.txt | AC | 277 ms | 42856 KB |
01-35.txt | AC | 924 ms | 99936 KB |
01-36.txt | AC | 978 ms | 101728 KB |
01-37.txt | AC | 919 ms | 101216 KB |
01-38.txt | AC | 926 ms | 101856 KB |
01-39.txt | AC | 920 ms | 100448 KB |
01-40.txt | AC | 910 ms | 99424 KB |
01-41.txt | AC | 150 ms | 31468 KB |
01-42.txt | AC | 150 ms | 30956 KB |
01-43.txt | AC | 152 ms | 31980 KB |
01-44.txt | AC | 149 ms | 30956 KB |
01-45.txt | AC | 1069 ms | 93152 KB |
01-46.txt | AC | 940 ms | 92000 KB |
01-47.txt | AC | 985 ms | 91360 KB |
01-48.txt | AC | 837 ms | 100960 KB |
01-49.txt | AC | 868 ms | 101216 KB |
01-50.txt | AC | 832 ms | 99424 KB |
01-51.txt | AC | 845 ms | 99808 KB |
01-52.txt | AC | 841 ms | 99808 KB |
01-53.txt | AC | 860 ms | 100064 KB |
01-54.txt | AC | 862 ms | 99168 KB |
01-55.txt | AC | 659 ms | 89572 KB |
01-56.txt | AC | 691 ms | 92004 KB |
01-57.txt | AC | 691 ms | 89312 KB |
01-58.txt | AC | 794 ms | 89828 KB |
01-59.txt | AC | 723 ms | 89824 KB |
01-60.txt | AC | 739 ms | 89312 KB |
01-61.txt | AC | 718 ms | 90724 KB |
01-62.txt | AC | 741 ms | 91744 KB |
01-63.txt | AC | 725 ms | 90212 KB |
01-64.txt | AC | 730 ms | 90340 KB |
01-65.txt | AC | 756 ms | 91232 KB |
01-66.txt | AC | 759 ms | 92000 KB |
01-67.txt | AC | 694 ms | 91236 KB |
01-68.txt | AC | 734 ms | 89696 KB |
01-69.txt | AC | 756 ms | 91876 KB |
01-70.txt | AC | 762 ms | 92644 KB |
01-71.txt | AC | 765 ms | 93412 KB |
01-72.txt | AC | 852 ms | 94048 KB |
01-73.txt | AC | 525 ms | 89568 KB |
01-74.txt | AC | 506 ms | 90080 KB |
01-75.txt | AC | 501 ms | 90084 KB |
01-76.txt | AC | 570 ms | 90980 KB |
01-77.txt | AC | 590 ms | 90976 KB |
01-78.txt | AC | 563 ms | 89444 KB |
01-79.txt | AC | 515 ms | 91616 KB |
01-80.txt | AC | 530 ms | 90336 KB |
01-81.txt | AC | 549 ms | 90312 KB |
01-82.txt | AC | 585 ms | 90980 KB |
01-83.txt | AC | 611 ms | 91104 KB |
01-84.txt | AC | 587 ms | 92132 KB |
01-85.txt | AC | 515 ms | 91236 KB |
01-86.txt | AC | 534 ms | 89568 KB |
01-87.txt | AC | 524 ms | 90340 KB |
01-88.txt | AC | 617 ms | 94012 KB |
01-89.txt | AC | 598 ms | 90724 KB |
01-90.txt | AC | 581 ms | 93028 KB |
sample-01.txt | AC | 4 ms | 6912 KB |
sample-02.txt | AC | 4 ms | 6912 KB |
sample-03.txt | AC | 3 ms | 6912 KB |