Submission #764918

#TimeUsernameProblemLanguageResultExecution timeMemory
764918vjudge1Roadside Advertisements (NOI17_roadsideadverts)C++17
0 / 100
2 ms980 KiB
#include <bits/stdc++.h> #define ibase ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define pb push_back #define ff first #define int long long #define ss second using namespace std; const int N = 1e3 + 3; const int MOD = 1e9 + 7; int a[N], ans[N], pos[N], cnt = 1; vector < pair < int, int > > g[N]; void dfs(int v){ cnt++; for(auto to : g[v]){ a[cnt] = to.ss; pos[to.ff] = cnt; dfs(to.ff); } } void solve(){ int n, sum = 0; cin >> n; set < int > s; for(int i=1; i<n; i++){ int x, y, cost; cin >> x >> y >> cost; s.insert(x); s.insert(y); sum += cost; g[x].pb(make_pair(y, cost)); } int z; bool ok = 0; for(int i=0; i<=n; i++){ if(g[i].size() == 0) continue; if(g[i].size() == 1){ if(ok == 0){ ok = 1; z = i; pos[i] = 1; } else{ pos[i] = s.size(); } } } dfs(z); for(int i=1; i<=n; i++){ ans[i] = ans[i - 1] + a[i]; } int q; cin >> q; while(q--){ int a, b, c, d, e; cin >> a >> b >> c >> d >> e; int l, r; l = min({pos[a], pos[b], pos[c], pos[d], pos[e]}); r = max({pos[a], pos[b], pos[c], pos[d], pos[e]}); cout << ans[r] - ans[l - 1] << endl; } } signed main() { ibase; int t = 1; //freopen("floor4.in", "r", stdin); //freopen("floor4.out", "w", stdout); //cin >> t; while (t--) { solve(); } }

Compilation message (stderr)

roadsideadverts.cpp: In function 'void solve()':
roadsideadverts.cpp:47:8: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
   47 |     dfs(z);
      |     ~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...