Submission #370846

#TimeUsernameProblemLanguageResultExecution timeMemory
370846azberjibiouDesignated Cities (JOI19_designated_cities)C++17
7 / 100
533 ms65388 KiB
#include <bits/stdc++.h> #define fir first #define sec second #define ll long long #define pll pair<ll, ll> using namespace std; const int mxN=200020; const ll INF=1000000000000000001; typedef struct line{ int s, e; ll val; }line; int N, Q; map <int, pair<int, ll>> v[mxN]; vector <pll> rv[mxN]; ll ans[mxN]; ll tot, rtot; ll dep[mxN], rdep[mxN]; void dfs(int now, int pre) { for(auto iter=v[now].begin();iter!=v[now].end();iter++) { if(iter->fir==pre) continue; int nxt=iter->fir; dep[nxt]=dep[now]+iter->sec.sec; tot+=iter->sec.sec; dfs(nxt, now); } } void rdfs(int now, int pre) { for(pll ele : rv[now]) { if(ele.fir==pre) continue; rdep[ele.fir]=rdep[now]+ele.sec; rdfs(ele.fir, now); } } void solv1() { dfs(1, -1); rdfs(1, -1); for(int i=1;i<=N;i++) { ans[1]=max(ans[1], tot-dep[i]+rdep[i]); } ans[1]=rtot-ans[1]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; for(int i=0;i<N-1;i++) { int a, b, c, d; cin >> a >> b >> c >> d; v[a].insert({b, {2*i, d}}); v[b].insert({a, {2*i+1, c}}); rv[a].push_back({b, c}); rv[b].push_back({a, d}); rtot+=c; rtot+=d; } solv1(); cin >> Q; while(Q--) { int a; cin >> a; cout << ans[a] << '\n'; } }
#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...