This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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< bool > onpath;
bool DFS(int node,int prnt){
onpath[node] = true;
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;
}
onpath[node] = false;
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)
{
n = N, x = X , y = Y , k = K;
for(int i = 0;i < n;i++) g[i].clear();
onpath = vector< bool > (n , 0);
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);
vector< long long > v;
for(int i = 0 ;i < n;i++){
if(a[i] > b[i]) swap(a[i] , b[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
int ans = 0;
long long cur = 0;
while(ans < (int)v.size() && v[ans] + cur <= k) cur += v[ans++];
cur = 0;
int ans2 = 0;
DFS(X , -1);
vector< long long > v1 , v3 , v4;
vector< pair< long long , long long > > v2;
int has = 0;
for(int i = 0 ;i < n;i++){
if(onpath[i]){
cur += a[i];
v1.push_back(b[i] - a[i]);
has++;
}
else{
v2.push_back(make_pair(a[i] , b[i]));
}
}
ans2 = has;
if(cur > k) return ans;
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
for(int i = 0 ;i <= (int)v2.size();i++){
v3.clear();
v4 = v1;
long long tmpcur = cur;
for(int j = i ;j < (int)v2.size();j++){
v3.push_back(v2[j].second);
}
for(int j = 0 ;j < i;j++){
tmpcur += v2[j].first;
v4.push_back(v2[j].second - v2[j].first);
}
if(tmpcur > k) break;
sort(v3.begin(),v3.end());
sort(v4.begin(),v4.end());
long long tmptake = 0;
int idx = 0;
for(idx = 0; idx < (int)v4.size();idx++) tmptake += v4[idx];
for(int j = 0 ;j <= (int)v3.size();j++){
while(tmptake + tmpcur > k && idx > 0){
tmptake -= v4[--idx];
}
if(tmpcur + tmptake > k) break;
ans2 = max(ans2 , has + i + idx + j * 2);
if(j < (int)v3.size())
tmpcur += v3[j];
}
}
return max(ans , ans2);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |