Submission #376203

#TimeUsernameProblemLanguageResultExecution timeMemory
376203wiwihoDesignated Cities (JOI19_designated_cities)C++14
7 / 100
307 ms32108 KiB
#include <bits/stdc++.h> #define eb emplace_back #define printv(a, b) { \ for(auto pv : a) b << pv << " "; \ b << "\n"; \ } #define mp make_pair #define F first #define S second using namespace std; typedef long long ll; using pll = pair<ll, ll>; struct edge{ int a, b, c, d; ll get(int v){ if(v == b) return c; else return d; } int ano(int v){ return a ^ b ^ v; } }; vector<vector<edge>> g; vector<ll> dp, ans; void dfs(int now, int p){ //cerr << "dfs " << now << "\n"; for(edge i : g[now]){ int v = i.ano(now); if(v == p) continue; dfs(v, now); dp[now] += i.get(v) + dp[v]; } } void dfs2(int now, int p){ for(edge i : g[now]){ int v = i.ano(now); if(v == p){ //cerr << "dfs2 " << now << " " << p << " " << ans[p] << " " << i.get(now) << " " << i.get(p) << "\n"; ans[now] = ans[p] - i.get(now) + i.get(p); continue; } } for(edge i : g[now]){ int v = i.ano(now); if(v != p) dfs2(v, now); } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; g.resize(n + 1); dp.resize(n + 1); ans.resize(n + 1); for(int i = 0; i < n - 1; i++){ int u, v, c, d; cin >> u >> v >> c >> d; g[u].eb(edge({u, v, c, d})); g[v].eb(edge({u, v, c, d})); } dfs(1, 1); ans[1] = dp[1]; dfs2(1, 1); ll mn = 1LL << 60; for(int i = 1; i <= n; i++) mn = min(mn, ans[i]); //printv(dp, cerr); //printv(ans, cerr); int q; cin >> q; while(q--){ int e; cin >> e; cout << mn << "\n"; } return 0; }
#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...