Submission #1021377

#TimeUsernameProblemLanguageResultExecution timeMemory
1021377Nika533Designated Cities (JOI19_designated_cities)C++14
16 / 100
270 ms60808 KiB
#pragma gcc diagnostic "-std=c++1z" #include <bits/stdc++.h> #define int long long #define pb push_back #define f first #define s second #define MOD 1000000007 #define pii pair<int,int> #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() using namespace std; const int N=2e5+5; int n,m,T,k,q,tot,sum,val[N],ans[N]; pii mx={0,0}; vector<pair<int,pii>> g[N]; pii ml[N]; int parent[N]; void dfs_build(int x, int p) { ml[x]={0,x}; parent[x]=p; for (auto A:g[x]) { int y=A.f,w1=A.s.f,w2=A.s.s; if (y==p) continue; dfs_build(y,x); ml[x]=max(ml[x],{ml[y].f+w1,ml[y].s}); } } void solve(int root) { dfs_build(root,0); vector<bool> used(n+1,0); used[0]=1; priority_queue<pii> q; q.push(ml[root]); int cur_ans=tot-val[root],cur_ind=2; while (!q.empty()) { pii AA=q.top(); q.pop(); int v=AA.s,num=AA.f; cur_ans-=num; while (!used[v]) { used[v]=1; for (auto A:g[v]) { int u=A.f; if (!used[u]) q.push(ml[u]); } v=parent[v]; } ans[cur_ind]=cur_ans; cur_ind++; } } void dfs0(int x, int p) { for (auto A:g[x]) { int y=A.f,w1=A.s.f,w2=A.s.s; if (y==p) continue; sum+=w2; dfs0(y,x); } } void dfs1(int x, int p) { val[x]=sum; for (auto A:g[x]) { int y=A.f,w1=A.s.f,w2=A.s.s; if (y==p) continue; sum+=w1-w2; dfs1(y,x); sum-=w1-w2; } } void dfs(int x, int p, int dist) { mx=max(mx,{dist+val[x],x}); for (auto A:g[x]) { int y=A.f,w1=A.s.f,w2=A.s.s; if (y==p) continue; dfs(y,x,dist+w1+w2); } } void test_case() { cin>>n; for (int i=1; i<=n-1; i++) { int a,b,c,d; cin>>a>>b>>c>>d; g[a].pb({b,{c,d}}); g[b].pb({a,{d,c}}); tot+=c+d; } cin>>q; int query[q+1]; for (int i=1; i<=q; i++) cin>>query[i]; dfs0(1,0); dfs1(1,1); dfs(1,0,val[1]); int r1=mx.s; ans[1]=1e18; for (int i=1; i<=n; i++) ans[1]=min(ans[1],tot-val[i]); solve(r1); for (int i=1; i<=q; i++) cout<<ans[query[i]]<<endl; } main () { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); T=1; while (T--) test_case(); }

Compilation message (stderr)

designated_cities.cpp:1: warning: ignoring '#pragma gcc diagnostic' [-Wunknown-pragmas]
    1 | #pragma gcc diagnostic "-std=c++1z"
      | 
designated_cities.cpp: In function 'void dfs_build(long long int, long long int)':
designated_cities.cpp:24:30: warning: unused variable 'w2' [-Wunused-variable]
   24 |           int y=A.f,w1=A.s.f,w2=A.s.s;
      |                              ^~
designated_cities.cpp: In function 'void dfs0(long long int, long long int)':
designated_cities.cpp:53:21: warning: unused variable 'w1' [-Wunused-variable]
   53 |           int y=A.f,w1=A.s.f,w2=A.s.s;
      |                     ^~
designated_cities.cpp: At global scope:
designated_cities.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   96 | main () {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...