Submission #839689

#TimeUsernameProblemLanguageResultExecution timeMemory
839689AlfraganusRace (IOI11_race)C++17
9 / 100
59 ms16196 KiB
#include "race.h" // #include "grader.cpp" #include <bits/stdc++.h> using namespace std; struct node { int sum = 0, node = 0, nodes = 0; }; int best_path(int n, int k, int h[][2], int l[]) { if(n == 1){ return -1; } vector<vector<pair<int, int>>> graph(n); int root; vector<int> cnt(n); for(int i = 0; i < n - 1; i ++){ graph[h[i][0]].push_back({h[i][1], l[i]}); graph[h[i][1]].push_back({h[i][0], l[i]}); cnt[h[i][0]] ++; cnt[h[i][1]] ++; } for(int i = 0; i < n; i ++){ if(cnt[i] == 1){ root = i; break; } } queue<node> q; vector<bool> used(n); vector<vector<int>> a(n); q.push({0, root, 0}); used[root] = true; int ans = 1e7; while(!q.empty()){ node x = q.front(); q.pop(); while(x.sum >= k){ if(x.sum == k) ans = min(ans, x.nodes); int cur = x.node, y = x.nodes, j = 0, cur1 = x.node; while(y){ if((y & 1)) cur = a[cur][j]; y /= 2; j ++; } y = x.nodes - 1; j = 0; while(y){ if((y & 1)) cur1 = a[cur1][j]; y /= 2; j ++; } for(auto n : graph[cur]){ if(n.first == cur1){ x.sum -= n.second; break; } } x.nodes --; } for(auto neighbour : graph[x.node]){ if(!used[neighbour.first]){ q.push({x.sum + neighbour.second, neighbour.first, x.nodes + 1}); used[neighbour.first] = true; a[neighbour.first].push_back(x.node); for(int i = 1, y = 2; y <= x.nodes + 1; i ++, y <<= 1){ a[neighbour.first].push_back(a[a[neighbour.first][i - 1]][i - 1]); } } } } if(ans == 1e7) return -1; else return ans; }

Compilation message (stderr)

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:33:11: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |     q.push({0, root, 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...