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;
using ll = long long;
//#define int long long
#define forn(i,n) for(int i=0; i<(n); ++i)
#define pb push_back
#define pi pair<ll,ll>
#define f first
#define s second
#define vii(a,n) vector<int> a(n); forn(i,n) cin>>a[i];
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
int max_score(int n, int x, int y, ll k, vector<int>u, vector<int>v, vector<int>w) {
#define int ll
vector<vector<pi>> adj(n);
vector<map<int,int>> vis(n+1);
vector<map<int,int>> dist(n+1);
forn(i,n-1) adj[u[i]].pb({v[i],w[i]}), adj[v[i]].pb({u[i],w[i]});
if (1) {
queue<int> q;
dist[x][x]=dist[y][y]=0;
q.push(x);
while (q.size()) {
auto u = q.front(); q.pop();
for(auto&e:adj[u]) {
int v=e.f, w=e.s;
if (dist[x].count(v)) continue;
dist[x][v]=dist[x][u]+w;
q.push(v);
}
}
swap(x,y);
q.push(x);
while (q.size()) {
auto u = q.front(); q.pop();
for(auto&e:adj[u]) {
int v=e.f, w=e.s;
if (dist[x].count(v)) continue;
dist[x][v]=dist[x][u]+w;
q.push(v);
}
}
swap(x,y);
}
int ans=0;
set<pair<int,pi>> q;
q.insert({0,{x,x}});
q.insert({0,{y,y}});
vector<int> c(n);
vector<map<int,pair<int,pi>>> last(n);
vector<map<int,int>> in(n);
last[x][x]={0,{x,x}};
last[y][y]={0,{y,y}};
in[x][x]=1;
in[y][y]=1;
while (q.size()) {
auto it = *q.begin(); q.erase(q.begin());
if (it.f > k) return ans;
int d = it.f, u = it.s.f, z = it.s.s;
++ans;
k-=d;
c[u] = max(c[u], dist[z][u]);
vis[z][u]=1;
in[z][u]=0;
if (in[x][u]) {
q.erase(last[x][u]);
q.insert({max(dist[x][u]-c[u],0ll),{u,x}});
last[x][u] = {max(dist[x][u]-c[u],0ll),{u,x}};
}
if (in[y][u]) {
q.erase(last[y][u]);
q.insert({max(dist[y][u]-c[u],0ll),{u,y}});
last[y][u] = {max(dist[y][u]-c[u],0ll),{u,y}};
}
for(auto&e:adj[u]) {
int v=e.f;
if (vis[z][v]) continue;
int t = dist[z][v];
t = max(t-c[v],0ll);
q.insert({t,{v,z}});
in[z][v]=1;
last[z][v]={t,{v,z}};
}
}
return ans;
#undef int
}
# | 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... |