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 <bits/stdc++.h>
using namespace std;
vector <pair <int, int>> ke[200001];
vector <int> child[200001][2];
long long d[200001][2];
int p[200001][2],ch[200001][2],waiting[200001][2];
priority_queue <pair <int, pair <int, int>>, vector <pair <int, pair <int, int>>>, greater <pair <int, pair <int, int>>>> q;
void dfs(int u, int par, int i){
for (auto [v,w]:ke[u])
if (v!=par){
child[u][i].push_back(v);
d[v][i]=d[u][i]+w;
p[v][i]=u;
dfs(v,u,i);
}
}
void dfs2(int u, int i){
for (int v:child[u][i])
if (waiting[v][i]){
waiting[v][i]=0;
q.push({d[v][i]-d[v][i^1],{v,i}});
dfs2(v,i);
}
}
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W){
for (int i=0;i<N-1;i++){
ke[U[i]].push_back({V[i],W[i]});
ke[V[i]].push_back({U[i],W[i]});
}
d[X][0]=d[Y][1]=0;
memset(ch,0,sizeof(ch));
memset(waiting,0,sizeof(waiting));
dfs(X,X,0);
dfs(Y,Y,1);
for (int i=0;i<N;i++)
if (d[i][0]<d[i][1])
q.push({d[i][0],{i,0}});
else
q.push({d[i][1],{i,1}});
long long sum=0;
int res=0;
while (!q.empty()){
auto [w,v]=q.top();
q.pop();
if (sum+w>K)
break;
sum+=w;
res++;
auto [u,i]=v;
ch[u][i]=1;
if (!ch[u][i^1]){
if (!ch[p[u][i^1]][i^1])
waiting[u][i^1]=1;
else{
q.push({d[u][i^1]-d[u][i],{u,i^1}});
dfs2(u,i^1);
}
}
}
for (int i=0;i<N;i++){
ke[i].clear();
child[i][0].clear();
child[i][1].clear();
}
while (!q.empty())
q.pop();
return res;
}
# | 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... |