Submission #839931

#TimeUsernameProblemLanguageResultExecution timeMemory
839931mohammad_kilaniClosing Time (IOI23_closing)C++17
8 / 100
121 ms40360 KiB
#include "closing.h" #include <vector> #include <bits/stdc++.h> using namespace std; int n , x , y; long long k; const int N = 200010; vector< pair< int , int > > g[N]; long long a[N] , b[N]; void DFS(int node,int prnt,long long *a,long long dist){ a[node] = dist; for(int i = 0 ;i < (int)g[node].size();i++){ if(g[node][i].first != prnt) DFS(g[node][i].first , node , a , dist + g[node][i].second); } } vector< int > path; bool DFS(int node,int prnt){ path.push_back(node); if(node == y) return true; for(int i = 0 ;i < (int)g[node].size();i++){ if(g[node][i].first == prnt) continue; if(DFS(g[node][i].first , node)) return true; } path.pop_back(); return false; } int max_score(int N, int X, int Y, long long K,std::vector<int> U, std::vector<int> V, std::vector<int> W) { for(int i = 0;i < n;i++) g[i].clear(); path.clear(); n = N, x = X , y = Y , k = K; for(int i = 0 ;i < (int)U.size();i++){ g[U[i]].push_back(make_pair(V[i] , W[i])); g[V[i]].push_back(make_pair(U[i] , W[i])); } DFS(X , -1 , a , 0); DFS(Y, - 1, b , 0); priority_queue < long long > q; for(int i = 0 ;i < n;i++){ q.push(-min(a[i] , b[i])); } int ans = 0; while(!q.empty()){ k += q.top(); q.pop(); if(k < 0) break; ans++; } DFS(X , -1); long long cur = 0; priority_queue < pair< long long , pair< int , bool > > > q2; long long val; vector< int > done(n , 0); int ans2 = (int)path.size(); for(int i = 0 ;i < (int)path.size();i++){ if(a[path[i]] > b[path[i]]){ swap(a[path[i]] , b[path[i]]); } cur += a[path[i]]; done[path[i]] = 1; val = b[path[i]] - a[path[i]]; q2.push(make_pair(-val * 2, make_pair(0 , path[i]))); } for(int i = 0 ;i < n;i++){ if(done[i]) continue; if(a[i] > b[i]) swap(a[i] , b[i]); if(a[i] * 2 < b[i]) q2.push(make_pair(-a[i] * 2 , make_pair(0 , i))); else q2.push(make_pair(-b[i] , make_pair(1 , i))); } k = K; if(cur > k) return ans; int idx; while((int)q2.size() > 0){ idx = q2.top().second.second; if(q2.top().second.first == 1){ q2.pop(); if(cur + b[idx] > k){ q2.push(make_pair(-a[idx] * 2, make_pair(0 , idx))); continue; } cur += b[idx]; ans2 += 2; done[idx] = 2; continue; } q2.pop(); if(done[idx] == 0){ if(cur + a[idx] <= k){ cur += a[idx]; done[idx] = 1; q2.push(make_pair((a[idx] - b[idx]) * 2 , make_pair(0 , idx))); ans2++; } } else{ if(cur + b[idx] - a[idx] <= k){ cur += b[idx] - a[idx]; done[idx] = 2; ans2++; } } } //cout << ans2 << " " << cur << endl; return max(ans , ans2); }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...