Submission #1086421

#TimeUsernameProblemLanguageResultExecution timeMemory
1086421ehab_rafatRace (IOI11_race)C++17
100 / 100
380 ms101200 KiB
#include <bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define LSB(i) ((i) & (-i)) #define ll long long const int dx[]{-1,1,0,0,-1,-1,1,1}; const int dy[]{0,0,1,-1,-1,1,-1,1}; const int MOD = 1e9+7; const int N = 2e5+5; std::vector<array<int,2>> G[N]; map<ll, ll> vec[N]; ll sum[N], dist[N], n, k, ans; void dfs0(int u, int p, int s = 0, int d = 0){ sum[u] = s; dist[u] = d; vec[u][s] = d; for(auto& nei : G[u]){ int v = nei[0], w = nei[1]; if(v==p) continue; dfs0(v, u, s+w, d+1); } } void dfs(int u, int p){ ll target = k + 2*sum[u]; for(auto& nei : G[u]){ int v = nei[0]; if(v==p) continue; dfs(v, u); if(vec[v].size() > vec[u].size()) swap(vec[u], vec[v]); for(auto& p : vec[v]){ if(vec[u].count(target-p.first)){ ans = min(ans, vec[u][target-p.first]+p.second-2*dist[u]); } } for(auto& p : vec[v]){ if(!vec[u].count(p.first)) vec[u].insert(p); else vec[u][p.first] = min(vec[u][p.first], p.second); } } } int best_path(int _n, int _k, int edges[][2], int weights[]) { // if (k == 1) { return 0; } n = _n; k = _k; ans = INT_MAX; for (int i = 0; i < n - 1; i++) { int u = edges[i][0]; int v = edges[i][1]; G[u].push_back({v, weights[i]}); G[v].push_back({u, weights[i]}); } dfs0(0, -1); dfs(0, -1); return ans == INT_MAX ? -1 : ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...