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 <iostream>
#include <complex>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <bitset>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#include <cstdint>
#include <cassert>
using namespace std;
#define ll long long
#define fs first
#define sd second
const ll Inf = 1e18 + 7;
vector <ll> bfs(int n, int x, vector <vector<pair<ll, ll >>> v)
{
vector <ll> D(n + 1, Inf); queue <int> q;
D[x] = 0; q.push(x);
while (!q.empty())
{
int x = q.front(); q.pop();
for(auto& u : v[x]) if(D[u.fs] == Inf)
{
D[u.fs] = D[x] + u.sd;
q.push(u.fs);
}
}
return D;
}
vector <int> line(int n, int x, int y, vector <vector<pair<ll, ll>>> v)
{
vector <int> p(n, -1), s;
queue <int> q; q.push(x); p[x] = x;
x = y = -1;
for(int i = 0; i < n; i++) if(v[i].size() == 1){
if(x == -1) x = i;
else y = i;
}
while (!q.empty())
{
int x = q.front(); q.pop();
for(auto& u : v[x]) if(p[u.fs] == -1)
{
p[u.fs] = x;
q.push(u.fs);
}
}
while (true)
{
s.push_back(y);
if(y == x) break;
y = p[y];
}
reverse(s.begin(), s.end());
return s;
}
int max_score(int n, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
vector <vector<pair<ll, ll>>> v(n + 1, vector <pair<ll, ll>> ());
for(int i = 0; i < n - 1; i++)
{
v[U[i]].push_back({ V[i], W[i] });
v[V[i]].push_back({ U[i], W[i] });
}
vector <ll> d1 = bfs(n, X, v), d2 = bfs(n, Y, v);
vector <int> s = line(n, X, Y, v);
int ans = 0, m = s.size() - 1;
for(int i = 0; i <= m; i++)
{
for(int j = m; j >= 0; j--)
{
ll t = K; int tmp = 0;
priority_queue <pair<ll, pair<ll, ll>>> pq;
vector <int> p(n);
for(int k = j; k <= i; k++){
t -= max(d1[s[k]], d2[s[k]]);
tmp += 2;
}
for(int k = 0; k <= min(i, j-1); k++) if(p[s[k]] == 0){
t -= d1[s[k]]; p[s[k]] = 1;
tmp++;
} else break;
for(int k = m; k >= max(j, i+1); k--) if(p[s[k]] == 0){
t -= d2[s[k]]; p[s[k]] = 1;
tmp++;
} else break;
if(t < 0) continue;
p[X] = p[Y] = 0;
pq.push({ 0, { X, 1}});
pq.push({ 0, { Y, 2} }); //cerr << "NEW: " << tmp << endl;
while (!pq.empty())
{
int x = pq.top().sd.fs, q = pq.top().sd.sd, w = -pq.top().fs; pq.pop();
if(p[x]) continue;
p[x] = 1; //cerr << x << " " << t << " " << w << endl;
if(t - w >= 0){
t -= w;
tmp++;
}
for(auto& u : v[x]) if(!p[u.fs]){
if(q == 1){
pq.push({ -d1[u.fs], { u.fs, q }});
}
else{
pq.push({ -d2[u.fs], { u.fs, q }});
}
}
}
// cerr << tmp << endl;
ans = max(ans, tmp - 2);
}
}
return ans;
}
# | 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... |