# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
581071 | 2022-06-22T08:58:27 Z | 조영욱(#8362) | Toll (APIO13_toll) | C++17 | 2500 ms | 18528 KB |
#include <bits/stdc++.h> using namespace std; int n,m,k; typedef pair<int,int> P; typedef pair<int,P> iP; vector<iP> edge; vector<P> vec; int arr[20]; int p[100000]; vector<P> e; vector<int> adj[100000]; int depth[100000]; long long sz[100000]; int people[100000]; int find(int a) { return p[a]<0?a:p[a]=find(p[a]); } void merge(int a,int b) { a=find(a); b=find(b); if (a==b) { return; } p[b]=a; } void dfs(int v,int prev) { sz[v]=people[v]; for(int i=0;i<adj[v].size();i++) { int nt=adj[v][i]; if (nt!=prev) { depth[nt]=depth[v]+1; dfs(nt,v); sz[v]+=sz[nt]; } } } int main(void) { scanf("%d %d %d",&n,&m,&k); for(int i=0;i<m;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); a--; b--; edge.push_back(iP(c,P(a,b))); } for(int i=0;i<k;i++) { int u,v; scanf("%d %d",&u,&v); u--; v--; vec.push_back(P(u,v)); } for(int i=0;i<n;i++) { scanf("%d",&people[i]); } sort(edge.begin(),edge.end()); long long ret=0; for(int bit=0;bit<(1<<k);bit++) { e.clear(); memset(p,-1,sizeof(p)); memset(arr,-1,sizeof(arr)); for(int i=0;i<k;i++) { if (bit&(1<<i)) { merge(vec[i].first,vec[i].second); } } for(int i=0;i<edge.size();i++) { if (find(edge[i].second.first)!=find(edge[i].second.second)) { merge(edge[i].second.first,edge[i].second.second); bool flag=true; if (flag) { e.push_back(edge[i].second); } } } for(int i=0;i<k;i++) { if (bit&(1<<i)) { memset(p,-1,sizeof(p)); for(int j=0;j<k;j++) { if (bit&(1<<j)) { if (i!=j) { merge(vec[j].first,vec[j].second); } } } for(int j=0;j<edge.size();j++) { merge(edge[j].second.first,edge[j].second.second); if (find(vec[i].first)==find(vec[i].second)) { arr[i]=edge[j].first; break; } } } } int s=0; for(int i=0;i<k;i++) { if (bit&(1<<i)) { s++; } } if (e.size()+s!=n-1) { continue; } for(int i=0;i<n;i++) { adj[i].clear(); } memset(p,-1,sizeof(p)); bool flag=true; for(int i=0;i<e.size();i++) { if (find(e[i].first)==find(e[i].second)) { flag=false; } adj[e[i].first].push_back(e[i].second); adj[e[i].second].push_back(e[i].first); merge(e[i].first,e[i].second); } for(int i=0;i<k;i++) { if (bit&(1<<i)) { if (find(vec[i].first)==find(vec[i].second)) { flag=false; } adj[vec[i].first].push_back(vec[i].second); adj[vec[i].second].push_back(vec[i].first); merge(vec[i].first,vec[i].second); } } if (!flag) { continue; } dfs(0,-1); long long val=0; for(int i=0;i<k;i++) { if (bit&(1<<i)) { int u=vec[i].first; if (depth[vec[i].second]>depth[vec[i].first]) { u=vec[i].second; } val+=1LL*sz[u]*arr[i]; } } ret=max(ret,val); } printf("%lld",ret); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3028 KB | Output is correct |
2 | Correct | 2 ms | 3028 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3028 KB | Output is correct |
2 | Correct | 2 ms | 3028 KB | Output is correct |
3 | Correct | 76 ms | 3028 KB | Output is correct |
4 | Correct | 102 ms | 3044 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3028 KB | Output is correct |
2 | Correct | 2 ms | 3028 KB | Output is correct |
3 | Correct | 76 ms | 3028 KB | Output is correct |
4 | Correct | 102 ms | 3044 KB | Output is correct |
5 | Correct | 385 ms | 3288 KB | Output is correct |
6 | Correct | 138 ms | 3296 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3028 KB | Output is correct |
2 | Correct | 2 ms | 3028 KB | Output is correct |
3 | Correct | 76 ms | 3028 KB | Output is correct |
4 | Correct | 102 ms | 3044 KB | Output is correct |
5 | Correct | 385 ms | 3288 KB | Output is correct |
6 | Correct | 138 ms | 3296 KB | Output is correct |
7 | Execution timed out | 2559 ms | 18528 KB | Time limit exceeded |
8 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 3028 KB | Output is correct |
2 | Correct | 2 ms | 3028 KB | Output is correct |
3 | Correct | 76 ms | 3028 KB | Output is correct |
4 | Correct | 102 ms | 3044 KB | Output is correct |
5 | Correct | 385 ms | 3288 KB | Output is correct |
6 | Correct | 138 ms | 3296 KB | Output is correct |
7 | Execution timed out | 2559 ms | 18528 KB | Time limit exceeded |
8 | Halted | 0 ms | 0 KB | - |